dnscrypt-proxy/vendor/github.com/BurntSushi/toml/README.md

121 lines
2.7 KiB
Markdown
Raw Normal View History

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.
2021-08-08 10:21:25 +02:00
Compatible with TOML version [v1.0.0](https://toml.io/en/v1.0.0).
2021-08-08 10:21:25 +02:00
Documentation: https://godocs.io/github.com/BurntSushi/toml
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`).
2022-07-21 19:02:28 +02:00
This library requires Go 1.13 or newer; add it to your go.mod with:
2022-01-13 09:22:22 +01:00
% go get github.com/BurntSushi/toml@latest
2021-08-08 10:21:25 +02:00
It also comes with a TOML validator CLI tool:
2022-01-13 09:22:22 +01:00
% go install github.com/BurntSushi/toml/cmd/tomlv@latest
% tomlv some-toml-file.toml
### 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:
```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:
```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
}
var conf Config
2022-04-05 14:05:53 +02:00
_, err := toml.Decode(tomlData, &conf)
```
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:
```toml
some_key_NAME = "wat"
```
```go
type TOML struct {
2022-01-13 09:22:22 +01:00
ObscureKey string `toml:"some_key_NAME"`
}
```
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`:
```toml
2022-07-21 19:02:28 +02:00
contacts = [
"Donald Duck <donald@duckburg.com>",
"Scrooge McDuck <scrooge@duckburg.com>",
]
```
2022-07-21 19:02:28 +02:00
Can be decoded with:
```go
2022-07-21 19:02:28 +02:00
// Create address type which satisfies the encoding.TextUnmarshaler interface.
type address struct {
*mail.Address
}
2022-07-21 19:02:28 +02:00
func (a *address) UnmarshalText(text []byte) error {
var err error
2022-07-21 19:02:28 +02:00
a.Address, err = mail.ParseAddress(string(text))
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"}
}
```
2021-08-08 10:21:25 +02:00
To target TOML specifically you can implement `UnmarshalTOML` TOML interface in
a similar way.
### More complex usage
2022-07-21 19:02:28 +02:00
See the [`_example/`](/_example) directory for a more complex example.