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

Map entries with nil values are not marshaled #975

Open
burgerdev opened this issue Nov 29, 2024 · 0 comments
Open

Map entries with nil values are not marshaled #975

burgerdev opened this issue Nov 29, 2024 · 0 comments

Comments

@burgerdev
Copy link

Describe the bug

I'm trying to marshal/unmarshal the type map[string]any. Marshaling an instance like {"key": nil} results in an entirely empty doc, but I expected the presence of "key" to be reflected in the doc. In particular, the key is lost in a round-trip to toml and back.

To Reproduce

package test

import (
	"testing"

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

const key = "foo"

func TestEncode(t *testing.T) {
	oldMap := map[string]any{key: nil}
	doc, err := toml.Marshal(&oldMap)
	if err != nil {
		t.Fatal(err)
	}

	var newMap map[string]any
	if err := toml.Unmarshal(doc, &newMap); err != nil {
		t.Fatal(err)
	}

	if _, ok := newMap["foo"]; !ok {
		t.Fatalf("%v does not contain key %q", newMap, key)
	}
}

This fails with

=== RUN   TestEncode
    kata_runtime_test.go:27: map[] does not contain key "foo"
--- FAIL: TestEncode (0.00s)

Expected behavior

I expected the Marshal function to produce an empty table at foo:

[foo]

Versions

  • go-toml: v2.2.3
  • go: go version go1.23.3 linux/amd64
  • operating system: Linux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant