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

omitempty ignores types implementing TextMarshaler

Open iSchluff opened this issue 8 months ago • 1 comments

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

iSchluff avatar Jun 19 '24 13:06 iSchluff