package
0.10.8
Repository: https://github.com/kcl-lang/kcl-go.git
Documentation: pkg.go.dev

# README

This is a fork of toml lib, which adds the yaml.MapSlice data structure support.

TOML stands for Tom's Obvious, Minimal Language. This Go package provides a reflection interface similar to Go's standard library json and xml packages.

Compatible with TOML version v1.0.0.

Examples

For the simplest example, consider some TOML file as just a list of keys and values:

Age = 25
Cats = [ "Cauchy", "Plato" ]
Pi = 3.14
Perfection = [ 6, 28, 496, 8128 ]
DOB = 1987-07-05T05:45:00Z

Which can be decoded with:

type Config struct {
	Age        int
	Cats       []string
	Pi         float64
	Perfection []int
	DOB        time.Time
}

var conf Config
_, err := toml.Decode(tomlData, &conf)

You can also use struct tags if your struct field name doesn't map to a TOML key value directly:

some_key_NAME = "wat"
type TOML struct {
    ObscureKey string `toml:"some_key_NAME"`
}

Beware that like other decoders only exported fields are considered when encoding and decoding; private fields are silently ignored.

Using the Marshaler and encoding.TextUnmarshaler interfaces

Here's an example that automatically parses values in a mail.Address:

contacts = [
    "Donald Duck <[email protected]>",
    "Scrooge McDuck <[email protected]>",
]

Can be decoded with:

// Create address type which satisfies the encoding.TextUnmarshaler interface.
type address struct {
	*mail.Address
}

func (a *address) UnmarshalText(text []byte) error {
	var err error
	a.Address, err = mail.ParseAddress(string(text))
	return err
}

// Decode it.
func decode() {
	blob := `
		contacts = [
			"Donald Duck <[email protected]>",
			"Scrooge McDuck <[email protected]>",
		]
	`

	var contacts struct {
		Contacts []address
	}

	_, err := toml.Decode(blob, &contacts)
	if err != nil {
		log.Fatal(err)
	}

	for _, c := range contacts.Contacts {
		fmt.Printf("%#v\n", c.Address)
	}

	// Output:
	// &mail.Address{Name:"Donald Duck", Address:"[email protected]"}
	// &mail.Address{Name:"Scrooge McDuck", Address:"[email protected]"}
}

To target TOML specifically you can implement UnmarshalTOML TOML interface in a similar way.

More complex usage

See the _example/ directory for a more complex example.

# Functions

Decode the TOML data in to the pointer v.
DecodeFile reads the contents of a file and decodes it with [Decode].
DecodeFS reads the contents of a file from [fs.FS] and decodes it with [Decode].
DecodeReader is an alias for NewDecoder(r).Decode(v).
Marshal returns a TOML representation of the Go value.
NewDecoder creates a new Decoder.
NewEncoder create a new Encoder.
PrimitiveDecode is an alias for MetaData.PrimitiveDecode().
Unmarshal decodes the contents of data in TOML format into a pointer v.

# Structs

Decoder decodes TOML data.
Encoder encodes a Go to a TOML document.
MetaData allows access to meta information about TOML data that's not accessible otherwise.
ParseError is returned when there is an error parsing the TOML syntax such as invalid syntax, duplicate keys, etc.
Position of an error.
Primitive is a TOML value that hasn't been decoded into a Go value.

# Interfaces

Marshaler is the interface implemented by types that can marshal themselves into valid TOML.
Unmarshaler is the interface implemented by objects that can unmarshal a TOML description of themselves.

# Type aliases

Key represents any TOML key, including key groups.
TextMarshaler is an alias for encoding.TextMarshaler.
TextUnmarshaler is an alias for encoding.TextUnmarshaler.