Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

omitempty ignores types implementing TextMarshaler #955

Open
iSchluff opened this issue Jun 19, 2024 · 1 comment
Open

omitempty ignores types implementing TextMarshaler #955

iSchluff opened this issue Jun 19, 2024 · 1 comment
Labels
bug Issues describing a bug in go-toml.

Comments

@iSchluff
Copy link

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
@pelletier
Copy link
Owner

Thanks for reporting this bug! Runnable example: https://go.dev/play/p/dUguDjMvVwe

I think you're right that at the very least, an type implementing MarshalText should always be marshaled, even with omitempty, as the goal is to mimic the behavior of encoding/json.

For skipping a value completely, I think we providing a toml.Marshaler interface to implement would be the right way to go.

@pelletier pelletier added the bug Issues describing a bug in go-toml. label Aug 17, 2024
@pelletier pelletier changed the title Broken omitempty behavior with netip.Addr type omitempty ignores types implementing TextMarshaler Aug 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues describing a bug in go-toml.
Projects
None yet
Development

No branches or pull requests

2 participants