@@ -45,16 +45,15 @@ to check for typos. [See example in the documentation][strict].
|
|||||||
|
|
||||||
### Contextualized errors
|
### Contextualized errors
|
||||||
|
|
||||||
When most decoding errors occur, go-toml returns [`DecodeError`][decode-err]),
|
When most decoding errors occur, go-toml returns [`DecodeError`][decode-err],
|
||||||
which contains a human readable contextualized version of the error. For
|
which contains a human readable contextualized version of the error. For
|
||||||
example:
|
example:
|
||||||
|
|
||||||
```
|
```
|
||||||
2| key1 = "value1"
|
1| [server]
|
||||||
3| key2 = "missing2"
|
2| path = 100
|
||||||
| ~~~~ missing field
|
| ~~~ cannot decode TOML integer into struct field toml_test.Server.Path of type string
|
||||||
4| key3 = "missing3"
|
3| port = 50
|
||||||
5| key4 = "value4"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
[decode-err]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#DecodeError
|
[decode-err]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#DecodeError
|
||||||
@@ -73,6 +72,26 @@ representation.
|
|||||||
[tlt]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#LocalTime
|
[tlt]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#LocalTime
|
||||||
[tldt]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#LocalDateTime
|
[tldt]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#LocalDateTime
|
||||||
|
|
||||||
|
### Commented config
|
||||||
|
|
||||||
|
Since TOML is often used for configuration files, go-toml can emit documents
|
||||||
|
annotated with [comments and commented-out values][comments-example]. For
|
||||||
|
example, it can generate the following file:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
# Host IP to connect to.
|
||||||
|
host = '127.0.0.1'
|
||||||
|
# Port of the remote server.
|
||||||
|
port = 4242
|
||||||
|
|
||||||
|
# Encryption parameters (optional)
|
||||||
|
# [TLS]
|
||||||
|
# cipher = 'AEAD-AES128-GCM-SHA256'
|
||||||
|
# version = 'TLS 1.3'
|
||||||
|
```
|
||||||
|
|
||||||
|
[comments-example]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#example-Marshal-Commented
|
||||||
|
|
||||||
## Getting started
|
## Getting started
|
||||||
|
|
||||||
Given the following struct, let's see how to read it and write it as TOML:
|
Given the following struct, let's see how to read it and write it as TOML:
|
||||||
|
|||||||
+2
-1
@@ -541,6 +541,8 @@ func (enc *Encoder) encodeTableHeader(ctx encoderCtx, b []byte) ([]byte, error)
|
|||||||
|
|
||||||
b = enc.encodeComment(ctx.indent, ctx.options.comment, b)
|
b = enc.encodeComment(ctx.indent, ctx.options.comment, b)
|
||||||
|
|
||||||
|
b = enc.commented(ctx.commented, b)
|
||||||
|
|
||||||
b = enc.indent(ctx.indent, b)
|
b = enc.indent(ctx.indent, b)
|
||||||
|
|
||||||
b = append(b, '[')
|
b = append(b, '[')
|
||||||
@@ -825,7 +827,6 @@ func (enc *Encoder) encodeTable(b []byte, ctx encoderCtx, t table) ([]byte, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !ctx.skipTableHeader {
|
if !ctx.skipTableHeader {
|
||||||
b = enc.commented(ctx.commented, b)
|
|
||||||
b, err = enc.encodeTableHeader(ctx, b)
|
b, err = enc.encodeTableHeader(ctx, b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -1592,3 +1592,37 @@ func ExampleMarshal_commented() {
|
|||||||
// # input-file = ''
|
// # input-file = ''
|
||||||
// # output-file = ''
|
// # output-file = ''
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestReadmeComments(t *testing.T) {
|
||||||
|
type TLS struct {
|
||||||
|
Cipher string `toml:"cipher"`
|
||||||
|
Version string `toml:"version"`
|
||||||
|
}
|
||||||
|
type Config struct {
|
||||||
|
Host string `toml:"host" comment:"Host IP to connect to."`
|
||||||
|
Port int `toml:"port" comment:"Port of the remote server."`
|
||||||
|
Tls TLS `toml:"TLS,commented" comment:"Encryption parameters (optional)"`
|
||||||
|
}
|
||||||
|
example := Config{
|
||||||
|
Host: "127.0.0.1",
|
||||||
|
Port: 4242,
|
||||||
|
Tls: TLS{
|
||||||
|
Cipher: "AEAD-AES128-GCM-SHA256",
|
||||||
|
Version: "TLS 1.3",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
out, err := toml.Marshal(example)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
expected := `# Host IP to connect to.
|
||||||
|
host = '127.0.0.1'
|
||||||
|
# Port of the remote server.
|
||||||
|
port = 4242
|
||||||
|
|
||||||
|
# Encryption parameters (optional)
|
||||||
|
# [TLS]
|
||||||
|
# cipher = 'AEAD-AES128-GCM-SHA256'
|
||||||
|
# version = 'TLS 1.3'
|
||||||
|
`
|
||||||
|
require.Equal(t, expected, string(out))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user