...

Source file src/github.com/pelletier/go-toml/v2/internal/testsuite/testsuite.go

Documentation: github.com/pelletier/go-toml/v2/internal/testsuite

     1  // Package testsuite provides helper functions for interoperating with the
     2  // language-agnostic TOML test suite at github.com/BurntSushi/toml-test.
     3  package testsuite
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"os"
     9  
    10  	"github.com/pelletier/go-toml/v2"
    11  )
    12  
    13  // Marshal is a helpfer function for calling toml.Marshal
    14  //
    15  // Only needed to avoid package import loops.
    16  func Marshal(v interface{}) ([]byte, error) {
    17  	return toml.Marshal(v)
    18  }
    19  
    20  // Unmarshal is a helper function for calling toml.Unmarshal.
    21  //
    22  // Only needed to avoid package import loops.
    23  func Unmarshal(data []byte, v interface{}) error {
    24  	return toml.Unmarshal(data, v)
    25  }
    26  
    27  // ValueToTaggedJSON takes a data structure and returns the tagged JSON
    28  // representation.
    29  func ValueToTaggedJSON(doc interface{}) ([]byte, error) {
    30  	return json.MarshalIndent(addTag("", doc), "", "  ")
    31  }
    32  
    33  // DecodeStdin is a helper function for the toml-test binary interface.  TOML input
    34  // is read from STDIN and a resulting tagged JSON representation is written to
    35  // STDOUT.
    36  func DecodeStdin() error {
    37  	var decoded map[string]interface{}
    38  
    39  	if err := toml.NewDecoder(os.Stdin).Decode(&decoded); err != nil {
    40  		return fmt.Errorf("Error decoding TOML: %s", err)
    41  	}
    42  
    43  	j := json.NewEncoder(os.Stdout)
    44  	j.SetIndent("", "  ")
    45  	if err := j.Encode(addTag("", decoded)); err != nil {
    46  		return fmt.Errorf("Error encoding JSON: %s", err)
    47  	}
    48  
    49  	return nil
    50  }
    51  
    52  // EncodeStdin is a helper function for the toml-test binary interface.  Tagged
    53  // JSON is read from STDIN and a resulting TOML representation is written to
    54  // STDOUT.
    55  func EncodeStdin() error {
    56  	var j interface{}
    57  	err := json.NewDecoder(os.Stdin).Decode(&j)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	rm, err := rmTag(j)
    63  	if err != nil {
    64  		return fmt.Errorf("removing tags: %w", err)
    65  	}
    66  
    67  	return toml.NewEncoder(os.Stdout).Encode(rm)
    68  }
    69  

View as plain text