fix(encode): emit one key when two fields resolve to one name
Assisted-by: GLM 5.3
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user