pelletier / go-toml

Go library for the TOML file format
https://github.com/pelletier/go-toml
Other
1.73k stars 211 forks source link

Multiline comments missing comment characters #768

Closed fho closed 2 years ago

fho commented 2 years ago

Describe the bug When the comment tag of a struct that is marshalled contains newline characters (\n), it is encoded into an invalid TOML document. In the encoded version only the first line of the comment starts with the comment character (#). All other lines of the comments are missing it.

Using \n in comment tags generated valid multiline comments in v1.

How To Reproduce

package main

import (
    "fmt"

    "github.com/pelletier/go-toml/v2"
)

func main() {
    type cfg struct {
        Name string `comment:"This is a multiline comment.\nThis is line 2."`
    }

    out, err := toml.Marshal(&cfg{})
    if err != nil {
        panic(err)
    }

    fmt.Println(string(out))
}

Generated TOML document:

# This is a multiline comment.
This is line 2.
Name = ''

Expected behavior

I expected the following generated TOML document:


# This is a multiline comment.
# This is line 2.
Name = ''

Versions

pelletier commented 2 years ago

Good catch, thank you for reporting it!

pelletier commented 2 years ago

This issue should be fixed with https://github.com/pelletier/go-toml/commit/b2e0231cc93a72b48158c2bc7e01fd46d5fde74f. Feel free to re-open if you are still experiencing the issue!

fho commented 2 years ago

@pelletier thanks a lot! :-)