forked from hannamil/mapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.go
51 lines (43 loc) · 1.17 KB
/
mapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package mapper
import (
"errors"
"reflect"
)
var (
ZeroValue reflect.Value
)
func init() {
ZeroValue = reflect.Value{}
}
func Mapper(source, dest interface{}) (interface{}, error) {
sourceElem := reflect.ValueOf(source)
destElem := reflect.ValueOf(dest)
if sourceElem == ZeroValue {
return nil, errors.New("source object is not legal value")
}
if destElem == ZeroValue {
return nil, errors.New("dest object is not legal value")
}
sourceKind := reflect.ValueOf(source).Type().Kind()
destKind := reflect.ValueOf(dest).Type().Kind()
if sourceKind != destKind {
return nil, errors.New("type error")
}
return structMapper(sourceElem, destElem, destKind)
}
func structMapper(source, dest reflect.Value, kind reflect.Kind) (interface{}, error) {
if kind == reflect.Ptr {
return elementToStruct(source.Elem(), dest.Elem())
}
return elementToStruct(source, reflect.New(dest.Type()).Elem())
}
func elementToStruct(source, dest reflect.Value) (interface{}, error) {
for i := 0; i < source.NumField(); i++ {
name := source.Type().Field(i).Name
destField := dest.FieldByName(name)
if destField.IsValid() {
destField.Set(source.Field(i))
}
}
return dest, nil
}