diff --git a/README.md b/README.md index f6204cc1..63b92f3b 100644 --- a/README.md +++ b/README.md @@ -45,16 +45,15 @@ to check for typos. [See example in the documentation][strict]. ### 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 example: ``` -2| key1 = "value1" -3| key2 = "missing2" - | ~~~~ missing field -4| key3 = "missing3" -5| key4 = "value4" +1| [server] +2| path = 100 + | ~~~ cannot decode TOML integer into struct field toml_test.Server.Path of type string +3| port = 50 ``` [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 [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 Given the following struct, let's see how to read it and write it as TOML: diff --git a/marshaler.go b/marshaler.go index d8e3140e..6fe78533 100644 --- a/marshaler.go +++ b/marshaler.go @@ -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.commented(ctx.commented, b) + b = enc.indent(ctx.indent, b) b = append(b, '[') @@ -825,7 +827,6 @@ func (enc *Encoder) encodeTable(b []byte, ctx encoderCtx, t table) ([]byte, erro } if !ctx.skipTableHeader { - b = enc.commented(ctx.commented, b) b, err = enc.encodeTableHeader(ctx, b) if err != nil { return nil, err diff --git a/marshaler_test.go b/marshaler_test.go index a8920a71..c4869c88 100644 --- a/marshaler_test.go +++ b/marshaler_test.go @@ -1592,3 +1592,37 @@ func ExampleMarshal_commented() { // # input-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)) +}