From 403c22024921af1d66c6a3de7ee6431043465c39 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 26 Jan 2023 13:59:35 -0700 Subject: struct: Rework the arguments of indexStructInner to make more sense --- struct.go | 18 +++++++++--------- 1 file 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 +// Copyright (C) 2022-2023 Luke Shumaker // // 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), -- cgit v1.2.3-2-g168b