Skip to content

Commit

Permalink
encode: fix commented table with comment (#894)
Browse files Browse the repository at this point in the history
And updated README.
  • Loading branch information
pelletier committed Aug 29, 2023
1 parent 9dd7f1a commit 3175efb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
31 changes: 25 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion marshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, '[')
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions marshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 3175efb

Please sign in to comment.