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 returns nil for defined empty sections #878

Closed
dbarrosop opened this issue Jun 3, 2023 · 0 comments · Fixed by #879
Closed

omitempty returns nil for defined empty sections #878

dbarrosop opened this issue Jun 3, 2023 · 0 comments · Fixed by #879
Labels
bug Issues describing a bug in go-toml.

Comments

@dbarrosop
Copy link
Contributor

dbarrosop commented Jun 3, 2023

Describe the bug

When parsing a document into a struct using pointers and omitempty, empty sections are interpreted as nil same as if they weren't defined at all.

The main issue with this behavior is that the action Marshal(Unmarshal(doc)) mutates the document.

To Reproduce

Run the following example:

package main

import (
	"fmt"

	"github.com/pelletier/go-toml/v2"
)

type A struct {
	Nullable *struct {
		A string
	} `toml:"nullable,omitempty"`
}

type Doc struct {
	A *A `toml:"a,omitempty"`
}

func main() {
	doc1 := []byte(`[a]
[a.nullable]
`)
	doc2 := []byte(`[a]`)

	var d1 Doc
	if err := toml.Unmarshal(doc1, &d1); err != nil {
		panic(err)
	}
	fmt.Println(d1.A.Nullable)

	var d2 Doc
	if err := toml.Unmarshal(doc2, &d2); err != nil {
		panic(err)
	}
	fmt.Println(d2.A)
}

which prints:

<nil>
<nil>

Expected behavior

I'd expect it to return &Nullable{} in the first case as the section was defined.

Versions

  • go-toml: 60e4af8
  • go: 1.20
  • operating system: Linux

Additional context

@pelletier pelletier added the bug Issues describing a bug in go-toml. label Jun 6, 2023
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

Successfully merging a pull request may close this issue.

2 participants