fix(encode): emit one key when two fields resolve to one name

Assisted-by: GLM 5.3
This commit is contained in:
2026-09-17 23:08:42 +02:00
parent aaea68efc9
commit 30b28fe7fc
4 changed files with 69 additions and 1 deletions
+10
View File
@@ -7,6 +7,7 @@ import (
"fmt"
"math"
"reflect"
"slices"
"strings"
"sync"
"time"
@@ -323,6 +324,15 @@ func newStructSchema(t reflect.Type) structSchema {
return s
}
// ownsKey reports whether the field at path is the one that resolves key.
// The encoder consults it to emit exactly the field the decoder would fill,
// so a struct with two fields mapping to one key does not marshal into a
// duplicate TOML key.
func (s structSchema) ownsKey(key string, path []int) bool {
loc, ok := s.byName[key]
return ok && slices.Equal(loc.index, path)
}
// fieldByIndex walks an index path from a struct value, allocating nil
// pointers along the way so a key can reach through an embedded pointer
// struct. Every field on the path is exported, so each step is settable.