-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcopy_interface.go
62 lines (54 loc) · 1.33 KB
/
copy_interface.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
52
53
54
55
56
57
58
59
60
61
62
package mapper
import (
"context"
"reflect"
)
/***************************
@author: tiansheng.ren
@date: 2022/10/25
@desc:
***************************/
func (dcv *defaultCopyValue) InterfaceCopyValue(ctx context.Context, src, dst reflect.Value) error {
if !dst.CanSet() {
return CanSetError{Name: "InterfaceCopyValue"}
}
if dst.Kind() != reflect.Interface {
return LookupCopyValueError{Name: "InterfaceCopyValue", Kinds: []reflect.Kind{reflect.Interface}, Received: dst}
}
src = skipElem(src)
// 特殊处理
if src.Kind() == reflect.Invalid {
if dst.Kind() != reflect.Invalid {
// 元数据没有初始化,情况dst
dst.Set(reflect.New(dst.Type()).Elem())
}
return nil
}
if !dst.IsNil() {
tmpVal := reflect.New(dst.Elem().Type()).Elem()
fn, err := dcv.lookupCopyValue(tmpVal)
if err != nil {
return err
}
if err := fn(ctx, src, tmpVal); err != nil {
return err
}
dst.Set(tmpVal)
return nil
}
tmpValPtr := reflect.New(src.Type())
tmpVal := tmpValPtr.Elem()
fn, err := dcv.lookupCopyValue(src)
if err != nil {
return err
}
if err := fn(ctx, src, tmpVal); err != nil {
return err
}
if tmpVal.Kind() != reflect.Invalid && tmpVal.Type().Kind() == reflect.Struct && tmpValPtr.CanConvert(dst.Type()) {
dst.Set(tmpValPtr.Convert(dst.Type()))
} else {
dst.Set(tmpVal)
}
return nil
}