util/reflect: fix tests, lint fixes

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-09-29 13:09:48 +03:00
parent 8237e6a08e
commit 542f36cfa5
2 changed files with 27 additions and 19 deletions

View File

@ -13,14 +13,18 @@ import (
var ErrInvalidParam = errors.New("invalid url query param provided")
// var timeKind = reflect.ValueOf(time.Time{}).Kind()
// bracketSplitter
var bracketSplitter = regexp.MustCompile(`\[|\]`)
// StructField contains struct field path its value and field
type StructField struct {
Field reflect.StructField
Value reflect.Value
Path string
Value reflect.Value
Field reflect.StructField
}
// StructFieldByTag get struct field by tag key and its value
func StructFieldByTag(src interface{}, tkey string, tval string) (interface{}, error) {
sv := reflect.ValueOf(src)
if sv.Kind() == reflect.Ptr {
@ -65,6 +69,7 @@ func StructFieldByTag(src interface{}, tkey string, tval string) (interface{}, e
return nil, ErrNotFound
}
// StructFieldByPath get struct field by its path
func StructFieldByPath(src interface{}, path string) (interface{}, error) {
var err error
for _, p := range strings.Split(path, ".") {
@ -76,6 +81,7 @@ func StructFieldByPath(src interface{}, path string) (interface{}, error) {
return src, err
}
// StructFieldByName get struct field by its name
func StructFieldByName(src interface{}, tkey string) (interface{}, error) {
sv := reflect.ValueOf(src)
if sv.Kind() == reflect.Ptr {
@ -115,7 +121,7 @@ func StructFieldByName(src interface{}, tkey string) (interface{}, error) {
return nil, ErrNotFound
}
// StructFieldsMap returns map[string]interface{} or error
// StructFieldsMap returns struct map[string]interface{} or error
func StructFieldsMap(src interface{}) (map[string]interface{}, error) {
fields, err := StructFields(src)
if err != nil {
@ -206,6 +212,7 @@ func CopyFrom(a, b interface{}) {
}
}
// StructURLValues get struct fields via url.Values
func StructURLValues(src interface{}, pref string, tags []string) (url.Values, error) {
data := url.Values{}

View File

@ -1,10 +1,11 @@
package reflect
package reflect_test
import (
"net/url"
rfl "reflect"
rfl "reflect"
"reflect"
"testing"
rutil "github.com/unistack-org/micro/v3/util/reflect"
)
func TestStructFieldsMap(t *testing.T) {
@ -19,7 +20,7 @@ func TestStructFieldsMap(t *testing.T) {
}
val := &Str{Name: []string{"first", "second"}, XXX: "ttt", Nested: NestedStr{BBB: "ddd", CCC: 9}}
fields, err := StructFieldsMap(val)
fields, err := rutil.StructFieldsMap(val)
if err != nil {
t.Fatal(err)
}
@ -40,7 +41,7 @@ func TestStructFields(t *testing.T) {
}
val := &Str{Name: []string{"first", "second"}, XXX: "ttt", Nested: NestedStr{BBB: "ddd", CCC: 9}}
fields, err := StructFields(val)
fields, err := rutil.StructFields(val)
if err != nil {
t.Fatal(err)
}
@ -67,11 +68,11 @@ func TestStructByPath(t *testing.T) {
}
val := &Str{Name: []string{"first", "second"}, XXX: "ttt", Nested: NestedStr{BBB: "ddd", CCC: 9}}
field, err := StructFieldByPath(val, "Nested.CCC")
field, err := rutil.StructFieldByPath(val, "Nested.CCC")
if err != nil {
t.Fatal(err)
}
if rfl.Indirect(reflect.ValueOf(field)).Int() != 9 {
if reflect.Indirect(reflect.ValueOf(field)).Int() != 9 {
t.Fatalf("invalid elem returned: %v", field)
}
}
@ -83,7 +84,7 @@ func TestStructByTag(t *testing.T) {
val := &Str{Name: []string{"first", "second"}}
iface, err := StructFieldByTag(val, "codec", "flatten")
iface, err := rutil.StructFieldByTag(val, "codec", "flatten")
if err != nil {
t.Fatal(err)
}
@ -102,7 +103,7 @@ func TestStructByName(t *testing.T) {
val := &Str{Name: []string{"first", "second"}}
iface, err := StructFieldByName(val, "Name")
iface, err := rutil.StructFieldByName(val, "Name")
if err != nil {
t.Fatal(err)
}
@ -122,7 +123,7 @@ func TestStructURLValues(t *testing.T) {
}
val := &Str{Name: "test_name", Args: []int{1, 2, 3}, Str: &Str{Name: "nested_name"}}
data, err := StructURLValues(val, "", []string{"json"})
data, err := rutil.StructURLValues(val, "", []string{"json"})
if err != nil {
t.Fatal(err)
}
@ -138,7 +139,7 @@ func TestURLSliceVars(t *testing.T) {
t.Fatal(err)
}
mp, err := URLMap(u.RawQuery)
mp, err := rutil.URLMap(u.RawQuery)
if err != nil {
t.Fatal(err)
}
@ -164,7 +165,7 @@ func TestURLVars(t *testing.T) {
t.Fatal(err)
}
mp, err := URLMap(u.RawQuery)
mp, err := rutil.URLMap(u.RawQuery)
if err != nil {
t.Fatal(err)
}
@ -184,14 +185,14 @@ func TestIsZero(t *testing.T) {
}
testStr1.Nested.NestedName = "nested_name"
if ok := IsZero(testStr1); ok {
if ok := rutil.IsZero(testStr1); ok {
t.Fatalf("zero ret on non zero struct: %#+v", testStr1)
}
testStr1.Name = ""
testStr1.Value = ""
testStr1.Nested.NestedName = ""
if ok := IsZero(testStr1); !ok {
if ok := rutil.IsZero(testStr1); !ok {
t.Fatalf("non zero ret on zero struct: %#+v", testStr1)
}
@ -206,12 +207,12 @@ func TestIsZero(t *testing.T) {
Name: "test_name",
Nested: &testStr3{Nested: "nested_name"},
}
if ok := IsZero(vtest); ok {
if ok := rutil.IsZero(vtest); ok {
t.Fatalf("zero ret on non zero struct: %#+v", vtest)
}
vtest.Nested = nil
vtest.Name = ""
if ok := IsZero(vtest); !ok {
if ok := rutil.IsZero(vtest); !ok {
t.Fatalf("non zero ret on zero struct: %#+v", vtest)
}