summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-26 13:59:35 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-29 00:12:37 -0700
commit403c22024921af1d66c6a3de7ee6431043465c39 (patch)
treedc4e998fd8085c0fc6c3bb90cd354a4e32fa56cb
parent8eb33f063af1c744e735b42766ed766b446cac71 (diff)
struct: Rework the arguments of indexStructInner to make more sense
-rw-r--r--struct.go18
1 files changed, 9 insertions, 9 deletions
diff --git a/struct.go b/struct.go
index ee2bbf3..061cef1 100644
--- a/struct.go
+++ b/struct.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -25,7 +25,7 @@ func indexStruct(typ reflect.Type) structIndex {
var byPos []structField
byName := make(map[string][]int)
- indexStructInner(typ, nil, &byPos, byName, map[reflect.Type]struct{}{})
+ indexStructInner(typ, &byPos, byName, nil, map[reflect.Type]struct{}{})
ret := structIndex{
byName: make(map[string]int),
@@ -104,16 +104,16 @@ func indexStruct(typ reflect.Type) structIndex {
return ret
}
-func indexStructInner(typ reflect.Type, prefix []int, byPos *[]structField, byName map[string][]int, seen map[reflect.Type]struct{}) {
- if _, ok := seen[typ]; ok {
+func indexStructInner(typ reflect.Type, byPos *[]structField, byName map[string][]int, stackPath []int, stackSeen map[reflect.Type]struct{}) {
+ if _, ok := stackSeen[typ]; ok {
return
}
- seen[typ] = struct{}{}
- defer delete(seen, typ)
+ stackSeen[typ] = struct{}{}
+ defer delete(stackSeen, typ)
n := typ.NumField()
for i := 0; i < n; i++ {
- path := append(append([]int(nil), prefix...), i)
+ stackPath := append(stackPath, i)
fTyp := typ.Field(i)
var embed bool
@@ -147,12 +147,12 @@ func indexStructInner(typ reflect.Type, prefix []int, byPos *[]structField, byNa
if t.Kind() == reflect.Pointer {
t = t.Elem()
}
- indexStructInner(t, path, byPos, byName, seen)
+ indexStructInner(t, byPos, byName, stackPath, stackSeen)
} else {
byName[name] = append(byName[name], len(*byPos))
*byPos = append(*byPos, structField{
Name: name,
- Path: path,
+ Path: append([]int(nil), stackPath...),
Tagged: tagName != "",
OmitEmpty: opts.Contains("omitempty"),
Quote: opts.Contains("string") && isQuotable(fTyp.Type),