go-toml
go-toml copied to clipboard
omitempty ignores types implementing TextMarshaler
Describe the bug Marshalling netip.Addr with omitempty struct tag always returns empty, because the type doesn't have any exported fields.
To Reproduce Minimal example:
func TestMarshal(t *testing.T) {
tmp := struct {
IP netip.Addr `toml:"ip,omitempty"`
}{
IP: netip.MustParseAddr("192.168.178.35"),
}
res, err := toml.Marshal(&tmp)
if err != nil {
t.Fatal(err)
}
if string(res) != "ip = '192.168.178.35'\n" {
t.Fatalf("unexpected result: '%s'", res)
}
}
Expected behavior At the most basic I would expect a struct without any exported fields, but with a MarshalText implementation, to always be marshalled, just to be on the safe side.
Ideally there should be a way to instruct a custom zero behaviour, e.g. netip.Ip already exposes an IsZero() bool
call, so possibly this could be used.
Versions
- go-toml: 2.2.2
- go: 1.22.4
- operating system: Linux