Fix nil pointer map values not being marshaled (#1025)

When marshaling a map with nil pointer values, the keys were being
silently dropped, breaking round-trip fidelity. For example:

    map[string]*struct{}{"foo": nil}

Would produce an empty TOML document instead of "[foo]".

This change converts nil pointer values in maps to their zero values
(consistent with how nil pointers in slices are handled), allowing the
keys to be preserved as empty tables.

Nil interface values (map[string]any{"foo": nil}) are still skipped
since there's no type information to derive a zero value.

Fixes #975

Also, pin golangci-lint version to v2.8.0 in CI and document in AGENTS.md

- Explicitly set golangci-lint version in lint.yml to ensure consistent
  behavior across CI runs
- Update AGENTS.md with instructions to use the same linter version locally

---------

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Thomas Pelletier
2026-01-09 11:08:31 -05:00
committed by GitHub
parent 84d730b6c4
commit 003aa0993b
6 changed files with 66 additions and 4 deletions
+2
View File
@@ -18,3 +18,5 @@ jobs:
go-version: "1.24" go-version: "1.24"
- name: Run golangci-lint - name: Run golangci-lint
uses: golangci/golangci-lint-action@v9 uses: golangci/golangci-lint-action@v9
with:
version: v2.8.0
+7
View File
@@ -40,6 +40,13 @@ go-toml is a TOML library for Go. The goal is to provide an easy-to-use and effi
- Follow existing code format and structure - Follow existing code format and structure
- Code must pass `go fmt` - Code must pass `go fmt`
- Code must pass linting with the same golangci-lint version as CI (see version in `.github/workflows/lint.yml`):
```bash
# Install specific version (check lint.yml for current version)
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s -- -b $(go env GOPATH)/bin <version>
# Run linter
golangci-lint run ./...
```
### Commit Messages ### Commit Messages
+1 -1
View File
@@ -260,7 +260,7 @@ func positionAtEnd(b []byte) (row int, column int) {
} }
} }
return return row, column
} }
// subsliceOffset returns the byte offset of subslice within data. // subsliceOffset returns the byte offset of subslice within data.
@@ -1996,7 +1996,7 @@ func TestDecoderStrict(t *testing.T) {
var se *toml.StrictMissingError var se *toml.StrictMissingError
assert.True(t, errors.As(err, &se)) assert.True(t, errors.As(err, &se))
keys := []toml.Key{} keys := make([]toml.Key, 0, len(se.Errors))
for _, e := range se.Errors { for _, e := range se.Errors {
keys = append(keys, e.Key()) keys = append(keys, e.Key())
+8 -1
View File
@@ -705,7 +705,14 @@ func (enc *Encoder) encodeMap(b []byte, ctx encoderCtx, v reflect.Value) ([]byte
v := iter.Value() v := iter.Value()
if isNil(v) { if isNil(v) {
continue // For nil pointers, convert to zero value of the element type.
// This allows round-trip marshaling of maps with nil pointer values.
// For nil interfaces and nil maps, skip since we can't derive a type.
if v.Kind() == reflect.Ptr {
v = reflect.Zero(v.Type().Elem())
} else {
continue
}
} }
k, err := enc.keyToString(iter.Key()) k, err := enc.keyToString(iter.Key())
+47 -1
View File
@@ -619,12 +619,36 @@ hello = 'world'
expected: ``, expected: ``,
}, },
{ {
desc: "nil value in map is ignored", desc: "nil interface value in map is ignored",
v: map[string]interface{}{ v: map[string]interface{}{
"A": nil, "A": nil,
}, },
expected: ``, expected: ``,
}, },
{
desc: "nil pointer to struct in map produces empty table",
v: map[string]*struct{}{
"A": nil,
},
expected: `[A]
`,
},
{
desc: "nil pointer to int in map produces zero value",
v: map[string]*int{
"A": nil,
},
expected: `A = 0
`,
},
{
desc: "nil pointer to string in map produces empty string",
v: map[string]*string{
"A": nil,
},
expected: `A = ''
`,
},
{ {
desc: "new line in table key", desc: "new line in table key",
v: map[string]interface{}{ v: map[string]interface{}{
@@ -2193,3 +2217,25 @@ port = 4242
` `
assert.Equal(t, expected, string(out)) assert.Equal(t, expected, string(out))
} }
// TestMarshalIssue975 tests that nil pointer values in maps are marshaled as
// empty tables, allowing round-trip marshaling to work correctly.
// See https://github.com/pelletier/go-toml/issues/975
func TestMarshalIssue975(t *testing.T) {
// Test case from the issue: map[string]*struct{}
oldMap := map[string]*struct{}{
"foo": nil,
}
doc, err := toml.Marshal(&oldMap)
assert.NoError(t, err)
assert.Equal(t, "[foo]\n", string(doc))
var newMap map[string]*struct{}
err = toml.Unmarshal(doc, &newMap)
assert.NoError(t, err)
// Verify the key is preserved after round-trip
_, exists := newMap["foo"]
assert.True(t, exists, "key 'foo' should exist after round-trip")
}