From 07d408520163b85ea85b182025c8e55e28df0ba7 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 27 Oct 2021 12:55:48 +0300 Subject: [PATCH] util/reflect: fix reflect methods Signed-off-by: Vasiliy Tolstov --- util/reflect/struct.go | 5 +---- util/reflect/struct_test.go | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/util/reflect/struct.go b/util/reflect/struct.go index 54b0d400..3c8d31ca 100644 --- a/util/reflect/struct.go +++ b/util/reflect/struct.go @@ -75,7 +75,7 @@ func ZeroFieldByPath(src interface{}, path string) error { val := reflect.ValueOf(src) for _, p := range strings.Split(path, ".") { - val, err = structValueByName(reflect.ValueOf(val), p) + val, err = structValueByName(val, p) if err != nil { return err } @@ -182,9 +182,6 @@ func StructFieldByName(src interface{}, tkey string) (interface{}, error) { continue } if fld.Name == tkey || strings.EqualFold(strings.ToLower(fld.Name), strings.ToLower(tkey)) { - if val.Kind() != reflect.Ptr && val.CanAddr() { - val = val.Addr() - } return val.Interface(), nil } diff --git a/util/reflect/struct_test.go b/util/reflect/struct_test.go index 48476f88..e9c3b382 100644 --- a/util/reflect/struct_test.go +++ b/util/reflect/struct_test.go @@ -10,17 +10,17 @@ import ( func TestSetFieldByPath(t *testing.T) { type NestedStr struct { - BBB string - CCC int + BBB string `json:"bbb"` + CCC int `json:"ccc"` } type Str1 struct { Name []string `json:"name" codec:"flatten"` XXX string `json:"xxx"` - Nested NestedStr + Nested NestedStr `json:"nested"` } type Str2 struct { XXX string `json:"xxx"` - Nested *NestedStr + Nested *NestedStr `json:"nested"` Name []string `json:"name" codec:"flatten"` } var err error @@ -44,27 +44,27 @@ func TestSetFieldByPath(t *testing.T) { func TestZeroFieldByPath(t *testing.T) { type NestedStr struct { - BBB string - CCC int + BBB string `json:"bbb"` + CCC int `json:"ccc"` } type Str1 struct { Name []string `json:"name" codec:"flatten"` XXX string `json:"xxx"` - Nested NestedStr + Nested NestedStr `json:"nested"` } type Str2 struct { XXX string `json:"xxx"` - Nested *NestedStr + Nested *NestedStr `json:"nested"` Name []string `json:"name" codec:"flatten"` } var err error val1 := &Str1{Name: []string{"first", "second"}, XXX: "ttt", Nested: NestedStr{BBB: "ddd", CCC: 9}} - err = rutil.ZeroFieldByPath(val1, "name.nested.bbb") + err = rutil.ZeroFieldByPath(val1, "Nested.BBB") if err != nil { t.Fatal(err) } - err = rutil.ZeroFieldByPath(val1, "name.nested") + err = rutil.ZeroFieldByPath(val1, "Nested") if err != nil { t.Fatal(err) } @@ -73,7 +73,7 @@ func TestZeroFieldByPath(t *testing.T) { } val2 := &Str2{Name: []string{"first", "second"}, XXX: "ttt", Nested: &NestedStr{BBB: "ddd", CCC: 9}} - err = rutil.ZeroFieldByPath(val2, "name.nested") + err = rutil.ZeroFieldByPath(val2, "Nested") if err != nil { t.Fatal(err) } @@ -182,9 +182,9 @@ func TestStructByName(t *testing.T) { t.Fatal(err) } - if v, ok := iface.(*[]string); !ok { - t.Fatalf("not *[]string %v", iface) - } else if len(*v) != 2 { + if v, ok := iface.([]string); !ok { + t.Fatalf("not []string %v", iface) + } else if len(v) != 2 { t.Fatalf("invalid number %v", iface) } }