pelletier / go-toml

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

Add Encoder option to disable printing of super-table header #957

Open katexochen opened 1 month ago

katexochen commented 1 month ago

The following example

type a struct{ B b }
type b struct{ C c }
type c struct{ D d }
type d struct{ S string }

func main() {
    var test a
    test.B.C.D.S = "foo"
    out, err := toml.Marshal(test)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(out))
}

will output the following marshaled toml:

[B]
[B.C]
[B.C.D]
S = 'foo'

Citing the TOML spec

# [x] you
# [x.y] don't
# [x.y.z] need these
[x.y.z.w] # for this to work

So it would be nice if there would be a way to omit the empty super-table headers.

This is especially useful in case your last element in the path (D/w) is known to be unique, and you want to append a toml snippet to an existing toml file. If you marshal the snippet and it contains the empty super-table headers, you might redefine the super-table if it is already present in the file your appending to.

pelletier commented 1 week ago

That would indeed be nice! Thank you for filing an issue!