2018-01-11 22:38:50 +01:00
|
|
|
TOML stands for Tom's Obvious, Minimal Language. This Go package provides a
|
2022-07-21 19:02:28 +02:00
|
|
|
reflection interface similar to Go's standard library `json` and `xml` packages.
|
2018-01-11 22:38:50 +01:00
|
|
|
|
2021-08-08 10:21:25 +02:00
|
|
|
Compatible with TOML version [v1.0.0](https://toml.io/en/v1.0.0).
|
2018-01-11 22:38:50 +01:00
|
|
|
|
2021-08-08 10:21:25 +02:00
|
|
|
Documentation: https://godocs.io/github.com/BurntSushi/toml
|
2018-01-11 22:38:50 +01:00
|
|
|
|
2021-08-08 10:21:25 +02:00
|
|
|
See the [releases page](https://github.com/BurntSushi/toml/releases) for a
|
|
|
|
changelog; this information is also in the git tag annotations (e.g. `git show
|
|
|
|
v0.4.0`).
|
2018-01-11 22:38:50 +01:00
|
|
|
|
2022-07-21 19:02:28 +02:00
|
|
|
This library requires Go 1.13 or newer; add it to your go.mod with:
|
2018-01-11 22:38:50 +01:00
|
|
|
|
2022-01-13 09:22:22 +01:00
|
|
|
% go get github.com/BurntSushi/toml@latest
|
2018-01-11 22:38:50 +01:00
|
|
|
|
2021-08-08 10:21:25 +02:00
|
|
|
It also comes with a TOML validator CLI tool:
|
2018-01-11 22:38:50 +01:00
|
|
|
|
2022-01-13 09:22:22 +01:00
|
|
|
% go install github.com/BurntSushi/toml/cmd/tomlv@latest
|
|
|
|
% tomlv some-toml-file.toml
|
2018-01-11 22:38:50 +01:00
|
|
|
|
|
|
|
### Examples
|
2022-01-13 09:22:22 +01:00
|
|
|
For the simplest example, consider some TOML file as just a list of keys and
|
|
|
|
values:
|
2018-01-11 22:38:50 +01:00
|
|
|
|
|
|
|
```toml
|
|
|
|
Age = 25
|
|
|
|
Cats = [ "Cauchy", "Plato" ]
|
|
|
|
Pi = 3.14
|
|
|
|
Perfection = [ 6, 28, 496, 8128 ]
|
|
|
|
DOB = 1987-07-05T05:45:00Z
|
|
|
|
```
|
|
|
|
|
2022-07-21 19:02:28 +02:00
|
|
|
Which can be decoded with:
|
2018-01-11 22:38:50 +01:00
|
|
|
|
|
|
|
```go
|
|
|
|
type Config struct {
|
2021-08-08 10:21:25 +02:00
|
|
|
Age int
|
|
|
|
Cats []string
|
|
|
|
Pi float64
|
|
|
|
Perfection []int
|
2022-07-21 19:02:28 +02:00
|
|
|
DOB time.Time
|
2018-01-11 22:38:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var conf Config
|
2022-04-05 14:05:53 +02:00
|
|
|
_, err := toml.Decode(tomlData, &conf)
|
2018-01-11 22:38:50 +01:00
|
|
|
```
|
|
|
|
|
2022-07-21 19:02:28 +02:00
|
|
|
You can also use struct tags if your struct field name doesn't map to a TOML key
|
|
|
|
value directly:
|
2018-01-11 22:38:50 +01:00
|
|
|
|
|
|
|
```toml
|
|
|
|
some_key_NAME = "wat"
|
|
|
|
```
|
|
|
|
|
|
|
|
```go
|
|
|
|
type TOML struct {
|
2022-01-13 09:22:22 +01:00
|
|
|
ObscureKey string `toml:"some_key_NAME"`
|
2018-01-11 22:38:50 +01:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2022-07-21 19:02:28 +02:00
|
|
|
Beware that like other decoders **only exported fields** are considered when
|
|
|
|
encoding and decoding; private fields are silently ignored.
|
2021-08-08 10:21:25 +02:00
|
|
|
|
2022-01-13 09:22:22 +01:00
|
|
|
### Using the `Marshaler` and `encoding.TextUnmarshaler` interfaces
|
2022-07-21 19:02:28 +02:00
|
|
|
Here's an example that automatically parses values in a `mail.Address`:
|
2018-01-11 22:38:50 +01:00
|
|
|
|
|
|
|
```toml
|
2022-07-21 19:02:28 +02:00
|
|
|
contacts = [
|
|
|
|
"Donald Duck <donald@duckburg.com>",
|
|
|
|
"Scrooge McDuck <scrooge@duckburg.com>",
|
|
|
|
]
|
2018-01-11 22:38:50 +01:00
|
|
|
```
|
|
|
|
|
2022-07-21 19:02:28 +02:00
|
|
|
Can be decoded with:
|
2018-01-11 22:38:50 +01:00
|
|
|
|
|
|
|
```go
|
2022-07-21 19:02:28 +02:00
|
|
|
// Create address type which satisfies the encoding.TextUnmarshaler interface.
|
|
|
|
type address struct {
|
|
|
|
*mail.Address
|
2018-01-11 22:38:50 +01:00
|
|
|
}
|
|
|
|
|
2022-07-21 19:02:28 +02:00
|
|
|
func (a *address) UnmarshalText(text []byte) error {
|
2018-01-11 22:38:50 +01:00
|
|
|
var err error
|
2022-07-21 19:02:28 +02:00
|
|
|
a.Address, err = mail.ParseAddress(string(text))
|
2018-01-11 22:38:50 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-07-21 19:02:28 +02:00
|
|
|
|
|
|
|
// Decode it.
|
|
|
|
func decode() {
|
|
|
|
blob := `
|
|
|
|
contacts = [
|
|
|
|
"Donald Duck <donald@duckburg.com>",
|
|
|
|
"Scrooge McDuck <scrooge@duckburg.com>",
|
|
|
|
]
|
|
|
|
`
|
|
|
|
|
|
|
|
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:"donald@duckburg.com"}
|
|
|
|
// &mail.Address{Name:"Scrooge McDuck", Address:"scrooge@duckburg.com"}
|
|
|
|
}
|
2018-01-11 22:38:50 +01:00
|
|
|
```
|
|
|
|
|
2021-08-08 10:21:25 +02:00
|
|
|
To target TOML specifically you can implement `UnmarshalTOML` TOML interface in
|
|
|
|
a similar way.
|
|
|
|
|
2018-01-11 22:38:50 +01:00
|
|
|
### More complex usage
|
2022-07-21 19:02:28 +02:00
|
|
|
See the [`_example/`](/_example) directory for a more complex example.
|