encode: fix commented table with comment (#894)

And updated README.
This commit is contained in:
Thomas Pelletier
2023-08-29 08:46:34 -04:00
committed by GitHub
parent 9dd7f1af78
commit 3175efb395
3 changed files with 61 additions and 7 deletions
+34
View File
@@ -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))
}