util/reflect: return pointer from helper funcs
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
34f0b209cc
commit
60a5e737f8
@ -30,17 +30,19 @@ func StructFieldByTag(src interface{}, tkey string, tval string) (interface{}, e
|
|||||||
if !val.CanSet() || len(fld.PkgPath) != 0 {
|
if !val.CanSet() || len(fld.PkgPath) != 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
switch val.Kind() {
|
|
||||||
default:
|
if ts, ok := fld.Tag.Lookup(tkey); ok {
|
||||||
ts, ok := fld.Tag.Lookup(tkey)
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch val.Kind() {
|
||||||
case reflect.Ptr:
|
case reflect.Ptr:
|
||||||
if val = val.Elem(); val.Kind() == reflect.Struct {
|
if val = val.Elem(); val.Kind() == reflect.Struct {
|
||||||
if iface, err := StructFieldByTag(val.Interface(), tkey, tval); err == nil {
|
if iface, err := StructFieldByTag(val.Interface(), tkey, tval); err == nil {
|
||||||
@ -72,11 +74,14 @@ func StructFieldByName(src interface{}, tkey string) (interface{}, error) {
|
|||||||
if !val.CanSet() || len(fld.PkgPath) != 0 {
|
if !val.CanSet() || len(fld.PkgPath) != 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
switch val.Kind() {
|
if fld.Name == tkey {
|
||||||
default:
|
if val.Kind() != reflect.Ptr && val.CanAddr() {
|
||||||
if fld.Name == tkey {
|
val = val.Addr()
|
||||||
return val.Interface(), nil
|
|
||||||
}
|
}
|
||||||
|
return val.Interface(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch val.Kind() {
|
||||||
case reflect.Ptr:
|
case reflect.Ptr:
|
||||||
if val = val.Elem(); val.Kind() == reflect.Struct {
|
if val = val.Elem(); val.Kind() == reflect.Struct {
|
||||||
if iface, err := StructFieldByName(val.Interface(), tkey); err == nil {
|
if iface, err := StructFieldByName(val.Interface(), tkey); err == nil {
|
||||||
|
@ -17,9 +17,9 @@ func TestStructByTag(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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -36,9 +36,9 @@ func TestStructByName(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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user