util/reflect: update StructFieldNameByTag
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
		| @@ -26,13 +26,13 @@ type StructField struct { | |||||||
| } | } | ||||||
|  |  | ||||||
| // StructFieldNameByTag get struct field name by tag key and its value | // 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) | 	sv := reflect.ValueOf(src) | ||||||
| 	if sv.Kind() == reflect.Ptr { | 	if sv.Kind() == reflect.Ptr { | ||||||
| 		sv = sv.Elem() | 		sv = sv.Elem() | ||||||
| 	} | 	} | ||||||
| 	if sv.Kind() != reflect.Struct { | 	if sv.Kind() != reflect.Struct { | ||||||
| 		return "", ErrInvalidStruct | 		return "", nil, ErrInvalidStruct | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	typ := sv.Type() | 	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 { | 		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 { | ||||||
| 					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() { | 		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 name, err := StructFieldNameByTag(val.Interface(), tkey, tval); err == nil { | 				if name, fld, err := StructFieldNameByTag(val.Interface(), tkey, tval); err == nil { | ||||||
| 					return name, nil | 					return name, fld, nil | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 		case reflect.Struct: | 		case reflect.Struct: | ||||||
| 			if name, err := StructFieldNameByTag(val.Interface(), tkey, tval); err == nil { | 			if name, fld, err := StructFieldNameByTag(val.Interface(), tkey, tval); err == nil { | ||||||
| 				return name, nil | 				return name, fld, nil | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return "", ErrNotFound | 	return "", nil, ErrNotFound | ||||||
| } | } | ||||||
|  |  | ||||||
| // StructFieldByTag get struct field by tag key and its value | // StructFieldByTag get struct field by tag key and its value | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user