util/reflect: fix StructFieldByTag

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-05-04 13:16:31 +03:00
parent 4e99680c30
commit 1a1459dd0e
2 changed files with 10 additions and 12 deletions

View File

@ -46,9 +46,6 @@ func StructFieldByTag(src interface{}, tkey string, tval string) (interface{}, e
if ts, ok := fld.Tag.Lookup(tkey); ok { if ts, ok := fld.Tag.Lookup(tkey); ok {
for _, p := range strings.Split(ts, ",") { for _, p := range strings.Split(ts, ",") {
if p == tval { if p == tval {
if val.Kind() != reflect.Ptr && val.CanAddr() {
val = val.Addr()
}
return val.Interface(), nil return val.Interface(), nil
} }
} }
@ -493,6 +490,7 @@ func btSplitter(str string) []string {
} }
// queryToMap turns something like a[b][c]=4 into // queryToMap turns something like a[b][c]=4 into
//
// map[string]interface{}{ // map[string]interface{}{
// "a": map[string]interface{}{ // "a": map[string]interface{}{
// "b": map[string]interface{}{ // "b": map[string]interface{}{

View File

@ -190,9 +190,9 @@ func TestStructFieldByTag(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if v, ok := iface.(*[]string); !ok { if v, ok := iface.([]string); !ok {
t.Fatalf("not *[]string %v", iface) t.Fatalf("not *[]string %v", iface)
} else if len(*v) != 2 { } else if len(v) != 2 {
t.Fatalf("invalid number %v", iface) t.Fatalf("invalid number %v", iface)
} }
} }