diff --git a/encoding/json/json.go b/encoding/json/json.go index d5f0009..22893b3 100644 --- a/encoding/json/json.go +++ b/encoding/json/json.go @@ -5,7 +5,7 @@ import ( "fmt" "reflect" - cr "github.com/corpix/reflect" + our "git.loafle.net/overflow/util-go/reflect" ) // SetValueWithJSONStringArray set the value of json string array @@ -41,7 +41,7 @@ func SetValueWithJSONStringArray(values []string, targets []interface{}) error { case reflect.Ptr: return fmt.Errorf("Type of target[%d] cannot be double ptr, value=%s", indexI, value) default: - cv, err := cr.ConvertToType(value, reflect.TypeOf(target).Elem()) + cv, err := our.ConvertToType(value, reflect.TypeOf(target).Elem()) if nil != err { return fmt.Errorf("Type conversion of value[%s] has been failed to %s[%d]", value, reflect.TypeOf(target).Elem().Kind(), indexI) } diff --git a/reflect/convert.go b/reflect/convert.go index 3be00c6..be6b7d3 100644 --- a/reflect/convert.go +++ b/reflect/convert.go @@ -5,7 +5,7 @@ import ( "strconv" ) -func ConvertToType(v interface{}, t reflect.Type) (interface{}, error) { +func ConvertToType(v interface{}, t Type) (interface{}, error) { var ( tt = reflect.TypeOf(v) vv = reflect.ValueOf(v) @@ -47,8 +47,11 @@ func ConvertToType(v interface{}, t reflect.Type) (interface{}, error) { switch t.Kind() { case Map: var ( - res = reflect.MakeMapWithSize(t, len(vv.MapKeys())) - ck interface{} + res = reflect.MakeMapWithSize( + t, + vv.Len(), + ) + ck interface{} ) for _, k := range vv.MapKeys() { ck, err = ConvertToType( diff --git a/reflect/error.go b/reflect/error.go index 117d18a..b305cbd 100644 --- a/reflect/error.go +++ b/reflect/error.go @@ -2,14 +2,13 @@ package reflect import ( "fmt" - "reflect" "strings" ) type ErrCanNotConvertType struct { value interface{} - from reflect.Type - to reflect.Type + from Type + to Type reason []string } @@ -31,7 +30,7 @@ func (e ErrCanNotConvertType) Error() string { ) } -func NewErrCanNotConvertType(value interface{}, from reflect.Type, to reflect.Type, reason ...string) ErrCanNotConvertType { +func NewErrCanNotConvertType(value interface{}, from Type, to Type, reason ...string) ErrCanNotConvertType { return ErrCanNotConvertType{ value: value, from: from, diff --git a/reflect/indirect.go b/reflect/indirect.go index 8156141..6fb18ad 100644 --- a/reflect/indirect.go +++ b/reflect/indirect.go @@ -12,14 +12,14 @@ func Indirect(v interface{}) interface{} { return IndirectValue(reflect.ValueOf(v)).Interface() } -func IndirectValue(reflectValue reflect.Value) reflect.Value { +func IndirectValue(reflectValue Value) Value { if reflectValue.Kind() == reflect.Ptr { return reflectValue.Elem() } return reflectValue } -func IndirectType(reflectType reflect.Type) reflect.Type { +func IndirectType(reflectType Type) Type { if reflectType == TypeInvalid { return TypeInvalid } diff --git a/reflect/type.go b/reflect/type.go index 1e070c9..c262a95 100644 --- a/reflect/type.go +++ b/reflect/type.go @@ -8,8 +8,10 @@ var ( TypeOf = reflect.TypeOf ) +type Type = reflect.Type + var ( - Types = []reflect.Type{ + Types = []Type{ TypeBool, TypeInt, TypeInt8, @@ -29,7 +31,7 @@ var ( TypeString, } - TypeInvalid = reflect.Type(nil) + TypeInvalid = Type(nil) TypeBool = reflect.TypeOf(false) TypeInt = reflect.TypeOf(int(0)) TypeInt8 = reflect.TypeOf(int8(0)) diff --git a/reflect/value.go b/reflect/value.go new file mode 100644 index 0000000..8319055 --- /dev/null +++ b/reflect/value.go @@ -0,0 +1,11 @@ +package reflect + +import ( + "reflect" +) + +var ( + ValueOf = reflect.ValueOf +) + +type Value = reflect.Value