Skip to content

Commit f45b40a

Browse files
committed
codec: simplify field name precedence support
1 parent 1bd7e92 commit f45b40a

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

codec/helper.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,27 +1904,39 @@ func (ti *typeInfo) siForEncName(name []byte) (si *structFieldInfo) {
19041904

19051905
func (ti *typeInfo) resolve(x []structFieldInfo, ss map[string]uint16) (n int) {
19061906
n = len(x)
1907-
19081907
for i := range x {
19091908
ui := uint16(i)
1910-
xn := x[i].encName
1909+
sf := &x[ui]
1910+
xn := sf.encName
19111911
j, ok := ss[xn]
1912-
if ok {
1913-
i2clear := ui // index to be cleared
1914-
// if x[i].path.depth() < x[j].path.depth() { // this one is shallower
1915-
if len(x[i].parents) < len(x[j].parents) { // this one is shallower
1916-
ss[xn] = ui
1917-
i2clear = j
1918-
}
1919-
if x[i2clear].encName != "" {
1920-
x[i2clear].encName = ""
1921-
n--
1922-
}
1923-
} else {
1912+
if !ok {
19241913
ss[xn] = ui
1914+
continue
1915+
}
1916+
if ui == j {
1917+
continue
19251918
}
1919+
// if x[i].path.depth() < x[j].path.depth() { // this one is shallower
1920+
sf2 := &x[j]
1921+
if len(sf.parents) < len(sf2.parents) { // this one is shallower
1922+
ss[xn] = ui
1923+
sf = sf2
1924+
}
1925+
if sf.encName == "" {
1926+
continue
1927+
}
1928+
// const marker2025Debugf = true
1929+
// if marker2025Debugf {
1930+
// if sf == sf2 && len(sf.parents) > 0 { // updated
1931+
// sin := &sf.parents[len(sf.parents)-1]
1932+
// fName := sin.typ.Field(int(sf.node.index)).Name
1933+
// debugf("resolve: clearing for field # %d->%d: (%v) %s->%s %v",
1934+
// hlYELLOW, j, ui, sin.typ, fName, sf.encName, sf.baseTyp)
1935+
// }
1936+
// }
1937+
sf.encName = ""
1938+
n--
19261939
}
1927-
19281940
return
19291941
}
19301942

@@ -2318,14 +2330,14 @@ LOOP:
23182330
}
23192331

23202332
var numderef uint8 = 0
2321-
for xft := f.Type; xft.Kind() == reflect.Ptr; xft = xft.Elem() {
2333+
ft := f.Type
2334+
for ; ft.Kind() == reflect.Ptr; ft = ft.Elem() {
23222335
numderef++
23232336
}
23242337

23252338
var encName string
23262339
var parsed, omitEmpty bool
23272340

2328-
ft := baseRT(f.Type)
23292341
ftid := rt2id(ft)
23302342
// if anonymous and no struct tag (or it's blank),
23312343
// and a struct (or pointer to struct), inline it.

0 commit comments

Comments
 (0)