forked from duke-git/lancet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move typemap.go to maputil package and add document for it
- Loading branch information
Showing
2 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
package maputil | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
var mapHandlers = map[reflect.Kind]func(reflect.Value, reflect.Value) error{ | ||
reflect.String: convertNormal, | ||
reflect.Int: convertNormal, | ||
reflect.Int16: convertNormal, | ||
reflect.Int32: convertNormal, | ||
reflect.Int64: convertNormal, | ||
reflect.Uint: convertNormal, | ||
reflect.Uint16: convertNormal, | ||
reflect.Uint32: convertNormal, | ||
reflect.Uint64: convertNormal, | ||
reflect.Float32: convertNormal, | ||
reflect.Float64: convertNormal, | ||
reflect.Uint8: convertNormal, | ||
reflect.Int8: convertNormal, | ||
reflect.Struct: convertNormal, | ||
reflect.Complex64: convertNormal, | ||
reflect.Complex128: convertNormal, | ||
} | ||
|
||
var _ = func() struct{} { | ||
mapHandlers[reflect.Map] = convertMap | ||
mapHandlers[reflect.Array] = convertSlice | ||
mapHandlers[reflect.Slice] = convertSlice | ||
return struct{}{} | ||
}() | ||
|
||
// MapTo try to map any interface to struct or base type | ||
/* | ||
Eg: | ||
v := map[string]interface{}{ | ||
"service":map[string]interface{}{ | ||
"ip":"127.0.0.1", | ||
"port":1234, | ||
}, | ||
version:"v1.0.01" | ||
} | ||
type Target struct { | ||
Service struct { | ||
Ip string `json:"ip"` | ||
Port int `json:"port"` | ||
} `json:"service"` | ||
Ver string `json:"version"` | ||
} | ||
var dist Target | ||
if err := typemap.MapTo(v,&dist); err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
log.Println(dist) | ||
*/ | ||
func MapTo(src any, dst any) error { | ||
|
||
dstRef := reflect.ValueOf(dst) | ||
if dstRef.Kind() != reflect.Ptr { | ||
return fmt.Errorf("dst is not ptr") | ||
} | ||
|
||
dstRef = reflect.Indirect(dstRef) | ||
|
||
srcRef := reflect.ValueOf(src) | ||
if srcRef.Kind() == reflect.Ptr || srcRef.Kind() == reflect.Interface { | ||
srcRef = srcRef.Elem() | ||
} | ||
if f, ok := mapHandlers[srcRef.Kind()]; ok { | ||
return f(srcRef, dstRef) | ||
} | ||
|
||
return fmt.Errorf("no implemented:%s", srcRef.Type()) | ||
} | ||
|
||
func convertNormal(src reflect.Value, dst reflect.Value) error { | ||
if dst.CanSet() { | ||
if src.Type() == dst.Type() { | ||
dst.Set(src) | ||
} else if src.CanConvert(dst.Type()) { | ||
dst.Set(src.Convert(dst.Type())) | ||
} else { | ||
return fmt.Errorf("can not convert:%s:%s", src.Type().String(), dst.Type().String()) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func convertSlice(src reflect.Value, dst reflect.Value) error { | ||
if dst.Kind() != reflect.Array && dst.Kind() != reflect.Slice { | ||
return fmt.Errorf("error type:%s", dst.Type().String()) | ||
} | ||
l := src.Len() | ||
target := reflect.MakeSlice(dst.Type(), l, l) | ||
if dst.CanSet() { | ||
dst.Set(target) | ||
} | ||
for i := 0; i < l; i++ { | ||
srcValue := src.Index(i) | ||
if srcValue.Kind() == reflect.Ptr || srcValue.Kind() == reflect.Interface { | ||
srcValue = srcValue.Elem() | ||
} | ||
if f, ok := mapHandlers[srcValue.Kind()]; ok { | ||
err := f(srcValue, dst.Index(i)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func convertMap(src reflect.Value, dst reflect.Value) error { | ||
if src.Kind() != reflect.Map || dst.Kind() != reflect.Struct { | ||
if src.Kind() == reflect.Interface { | ||
return convertMap(src.Elem(), dst) | ||
} else { | ||
return fmt.Errorf("src or dst type error,%s,%s", src.Type().String(), dst.Type().String()) | ||
} | ||
} | ||
dstType := dst.Type() | ||
num := dstType.NumField() | ||
exist := map[string]int{} | ||
for i := 0; i < num; i++ { | ||
k := dstType.Field(i).Tag.Get("json") | ||
if k == "" { | ||
k = dstType.Field(i).Name | ||
} | ||
if strings.Contains(k, ",") { | ||
taglist := strings.Split(k, ",") | ||
if taglist[0] == "" { | ||
|
||
k = dstType.Field(i).Name | ||
} else { | ||
k = taglist[0] | ||
|
||
} | ||
|
||
} | ||
exist[k] = i | ||
} | ||
|
||
keys := src.MapKeys() | ||
for _, key := range keys { | ||
if index, ok := exist[key.String()]; ok { | ||
v := dst.Field(index) | ||
if v.Kind() == reflect.Struct { | ||
err := convertMap(src.MapIndex(key), v) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
if v.CanSet() { | ||
if v.Type() == src.MapIndex(key).Elem().Type() { | ||
v.Set(src.MapIndex(key).Elem()) | ||
} else if src.MapIndex(key).Elem().CanConvert(v.Type()) { | ||
v.Set(src.MapIndex(key).Elem().Convert(v.Type())) | ||
} else if f, ok := mapHandlers[src.MapIndex(key).Elem().Kind()]; ok && f != nil { | ||
err := f(src.MapIndex(key).Elem(), v) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
return fmt.Errorf("error type:d(%s)s(%s)", v.Type(), src.Type()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package maputil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/duke-git/lancet/v2/internal" | ||
) | ||
|
||
type ( | ||
Person struct { | ||
Name string `json:"name"` | ||
Age int `json:"age"` | ||
Phone string `json:"phone"` | ||
Addr Address `json:"address"` | ||
} | ||
|
||
Address struct { | ||
Street string `json:"street"` | ||
Number int `json:"number"` | ||
} | ||
) | ||
|
||
func TestStructType(t *testing.T) { | ||
assert := internal.NewAssert(t, "TestStructType") | ||
|
||
src := map[string]interface{}{ | ||
"name": "Nothin", | ||
"age": 28, | ||
"phone": "123456789", | ||
"address": map[string]interface{}{ | ||
"street": "test", | ||
"number": 1, | ||
}, | ||
} | ||
|
||
var p Person | ||
err := MapTo(src, &p) | ||
assert.IsNil(err) | ||
assert.Equal(src["name"], p.Name) | ||
assert.Equal(src["age"], p.Age) | ||
assert.Equal(src["phone"], p.Phone) | ||
assert.Equal("test", p.Addr.Street) | ||
assert.Equal(1, p.Addr.Number) | ||
} | ||
|
||
func TestBaseType(t *testing.T) { | ||
assert := internal.NewAssert(t, "TestBaseType") | ||
|
||
tc := map[string]interface{}{ | ||
"i32": -32, | ||
"i8": -8, | ||
"i16": -16, | ||
"i64": -64, | ||
"i": -1, | ||
"u32": 32, | ||
"u8": 8, | ||
"u16": 16, | ||
"u64": 64, | ||
"u": 1, | ||
"tf": true, | ||
"f32": 1.32, | ||
"f64": 1.64, | ||
"str": "hello mapto", | ||
"complex": 1 + 3i, | ||
} | ||
|
||
type BaseType struct { | ||
I int `json:"i"` | ||
I8 int8 `json:"i8"` | ||
I16 int16 `json:"i16"` | ||
I32 int32 `json:"i32"` | ||
I64 int64 `json:"i64"` | ||
U uint `json:"u"` | ||
U8 uint8 `json:"u8"` | ||
U16 uint16 `json:"u16"` | ||
U32 uint32 `json:"u32"` | ||
U64 uint64 `json:"u64"` | ||
F32 float32 `json:"f32"` | ||
F64 float64 `json:"f64"` | ||
Tf bool `json:"tf"` | ||
Str string `json:"str"` | ||
Comp complex128 `json:"complex"` | ||
} | ||
|
||
var dist BaseType | ||
err := MapTo(tc, &dist) | ||
assert.IsNil(err) | ||
|
||
var number float64 | ||
|
||
MapTo(tc["i"], &number) | ||
assert.EqualValues(-1, number) | ||
MapTo(tc["i8"], &number) | ||
assert.EqualValues(-8, number) | ||
MapTo(tc["i16"], &number) | ||
assert.EqualValues(-16, number) | ||
MapTo(tc["i32"], &number) | ||
assert.EqualValues(-32, number) | ||
MapTo(tc["i64"], &number) | ||
assert.EqualValues(-64, number) | ||
MapTo(tc["u"], &number) | ||
assert.EqualValues(1, number) | ||
MapTo(tc["u8"], &number) | ||
assert.EqualValues(8, number) | ||
MapTo(tc["u16"], &number) | ||
assert.EqualValues(16, number) | ||
MapTo(tc["u32"], &number) | ||
assert.EqualValues(32, number) | ||
MapTo(tc["u64"], &number) | ||
assert.EqualValues(64, number) | ||
} |