From edc93e8c370fea9720bf50d6ed50bd2207522094 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 4 May 2024 14:43:46 +0300 Subject: [PATCH] util/reflect: update StructFieldNameByTag Signed-off-by: Vasiliy Tolstov --- util/reflect/struct.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/util/reflect/struct.go b/util/reflect/struct.go index 09132b08..5fda8a84 100644 --- a/util/reflect/struct.go +++ b/util/reflect/struct.go @@ -26,13 +26,13 @@ type StructField struct { } // StructFieldNameByTag get struct field name by tag key and its value -func StructFieldNameByTag(src interface{}, tkey string, tval string) (string, error) { +func StructFieldNameByTag(src interface{}, tkey string, tval string) (string, interface{}, error) { sv := reflect.ValueOf(src) if sv.Kind() == reflect.Ptr { sv = sv.Elem() } if sv.Kind() != reflect.Struct { - return "", ErrInvalidStruct + return "", nil, ErrInvalidStruct } typ := sv.Type() @@ -46,7 +46,7 @@ func StructFieldNameByTag(src interface{}, tkey string, tval string) (string, er if ts, ok := fld.Tag.Lookup(tkey); ok { for _, p := range strings.Split(ts, ",") { if p == tval { - return fld.Name, nil + return fld.Name, val.Interface(), nil } } } @@ -54,17 +54,17 @@ func StructFieldNameByTag(src interface{}, tkey string, tval string) (string, er switch val.Kind() { case reflect.Ptr: if val = val.Elem(); val.Kind() == reflect.Struct { - if name, err := StructFieldNameByTag(val.Interface(), tkey, tval); err == nil { - return name, nil + if name, fld, err := StructFieldNameByTag(val.Interface(), tkey, tval); err == nil { + return name, fld, nil } } case reflect.Struct: - if name, err := StructFieldNameByTag(val.Interface(), tkey, tval); err == nil { - return name, nil + if name, fld, err := StructFieldNameByTag(val.Interface(), tkey, tval); err == nil { + return name, fld, nil } } } - return "", ErrNotFound + return "", nil, ErrNotFound } // StructFieldByTag get struct field by tag key and its value