util/reflect: improve StructFields func

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-06-17 12:53:23 +03:00
parent 6e55d07636
commit 887b48f1e7

View File

@ -14,6 +14,11 @@ var ErrInvalidParam = errors.New("invalid url query param provided")
var bracketSplitter = regexp.MustCompile(`\[|\]`) var bracketSplitter = regexp.MustCompile(`\[|\]`)
type StructField struct {
Field reflect.StructField
Value reflect.Value
}
func StructFieldByTag(src interface{}, tkey string, tval string) (interface{}, error) { func StructFieldByTag(src interface{}, tkey string, tval string) (interface{}, error) {
sv := reflect.ValueOf(src) sv := reflect.ValueOf(src)
if sv.Kind() == reflect.Ptr { if sv.Kind() == reflect.Ptr {
@ -98,8 +103,8 @@ func StructFieldByName(src interface{}, tkey string) (interface{}, error) {
} }
// StructFields returns slice of struct fields // StructFields returns slice of struct fields
func StructFields(src interface{}) ([]reflect.StructField, error) { func StructFields(src interface{}) ([]StructField, error) {
var fields []reflect.StructField var fields []StructField
sv := reflect.ValueOf(src) sv := reflect.ValueOf(src)
if sv.Kind() == reflect.Ptr { if sv.Kind() == reflect.Ptr {
@ -123,7 +128,7 @@ func StructFields(src interface{}) ([]reflect.StructField, error) {
} }
fields = append(fields, infields...) fields = append(fields, infields...)
} else { } else {
fields = append(fields, fld) fields = append(fields, StructField{Field: fld, Value: val})
} }
} }