go-toml icon indicating copy to clipboard operation
go-toml copied to clipboard

Add Encoder option to disable printing of super-table header

Open katexochen opened this issue 7 months ago • 1 comments

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.

katexochen avatar Jul 15 '24 13:07 katexochen