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:
+8
-1
@@ -705,7 +705,14 @@ func (enc *Encoder) encodeMap(b []byte, ctx encoderCtx, v reflect.Value) ([]byte
|
||||
v := iter.Value()
|
||||
|
||||
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())
|
||||
|
||||
Reference in New Issue
Block a user