pelletier / go-toml

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

Broken omitempty behavior with netip.Addr type #955

Open iSchluff opened 2 weeks ago

iSchluff commented 2 weeks ago

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