-
Notifications
You must be signed in to change notification settings - Fork 2
/
registry.go
114 lines (98 loc) · 3.48 KB
/
registry.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package mapper
import (
"context"
"github.com/rentiansheng/mapper/mtype"
"reflect"
"sync"
)
/***************************
@author: tiansheng.ren
@date: 2022/10/25
@desc:
***************************/
var (
lock sync.RWMutex
typeHandler = make(map[reflect.Type]CopyValueFunc, 0)
kindHandler = make(map[reflect.Kind]CopyValueFunc, 0)
)
type register struct {
sync.RWMutex
typeHandler map[reflect.Type]CopyValueFunc
kindHandler map[reflect.Kind]CopyValueFunc
}
func newRegister() *register {
return ®ister{
typeHandler: make(map[reflect.Type]CopyValueFunc, 0),
kindHandler: make(map[reflect.Kind]CopyValueFunc, 0),
}
}
type CopyValueFunc func(ctx context.Context, src reflect.Value, dst reflect.Value) error
func (dcv *defaultCopyValue) Register(typ reflect.Type, fn CopyValueFunc) {
dcv.register.Lock()
defer dcv.register.Unlock()
dcv.register.typeHandler[typ] = fn
}
func (dcv *defaultCopyValue) RegistryKind(kind reflect.Kind, fn CopyValueFunc) {
dcv.register.Lock()
defer dcv.register.Unlock()
dcv.register.kindHandler[kind] = fn
}
func (dcv *defaultCopyValue) lookupCopyValue(value reflect.Value) (CopyValueFunc, error) {
if fn, ok := dcv.register.typeHandler[value.Type()]; ok {
return fn, nil
}
if fn, ok := dcv.register.kindHandler[value.Kind()]; ok {
return fn, nil
}
return nil, ErrNoCopyValue{value.Type()}
}
func (dcv *defaultCopyValue) buildDefaultRegistry() {
dcv.Register(mtype.Bool, dcv.BooleanCopyValue)
dcv.Register(mtype.Int, dcv.IntCopyValue)
dcv.Register(mtype.Int8, dcv.IntCopyValue)
dcv.Register(mtype.Int16, dcv.IntCopyValue)
dcv.Register(mtype.Int32, dcv.IntCopyValue)
dcv.Register(mtype.Int64, dcv.IntCopyValue)
dcv.Register(mtype.Uint, dcv.UintCopyValue)
dcv.Register(mtype.Uint8, dcv.UintCopyValue)
dcv.Register(mtype.Uint16, dcv.UintCopyValue)
dcv.Register(mtype.Uint32, dcv.UintCopyValue)
dcv.Register(mtype.Uint64, dcv.UintCopyValue)
dcv.Register(mtype.Float32, dcv.FloatCopyValue)
dcv.Register(mtype.Float64, dcv.FloatCopyValue)
dcv.Register(mtype.String, dcv.StringCopyValue)
dcv.RegistryKind(reflect.Bool, dcv.BooleanCopyValue)
dcv.RegistryKind(reflect.Int, dcv.IntCopyValue)
dcv.RegistryKind(reflect.Int8, dcv.IntCopyValue)
dcv.RegistryKind(reflect.Int16, dcv.IntCopyValue)
dcv.RegistryKind(reflect.Int32, dcv.IntCopyValue)
dcv.RegistryKind(reflect.Int64, dcv.IntCopyValue)
dcv.RegistryKind(reflect.Uint, dcv.UintCopyValue)
dcv.RegistryKind(reflect.Uint8, dcv.UintCopyValue)
dcv.RegistryKind(reflect.Uint16, dcv.UintCopyValue)
dcv.RegistryKind(reflect.Uint32, dcv.UintCopyValue)
dcv.RegistryKind(reflect.Uint64, dcv.UintCopyValue)
dcv.RegistryKind(reflect.Float32, dcv.FloatCopyValue)
dcv.RegistryKind(reflect.Float64, dcv.FloatCopyValue)
dcv.RegistryKind(reflect.String, dcv.StringCopyValue)
dcv.RegistryKind(reflect.Array, dcv.ArrayCopyValue)
dcv.RegistryKind(reflect.Map, dcv.MapCopyValue)
dcv.RegistryKind(reflect.Slice, dcv.SliceCopyValue)
dcv.RegistryKind(reflect.Struct, dcv.StructCopyValue)
dcv.RegistryKind(reflect.Ptr, dcv.PtrCopyValue)
dcv.RegistryKind(reflect.Interface, dcv.InterfaceCopyValue)
}
var (
Register = defaultCopyValueHandler.Register
RegisterKind = defaultCopyValueHandler.RegistryKind
)
// ErrNoCopyValue is returned when there wasn't an copy available for a type.
type ErrNoCopyValue struct {
Type reflect.Type
}
func (ene ErrNoCopyValue) Error() string {
if ene.Type == nil {
return "no found copy value handler for <nil>"
}
return "no found copy value handler for " + ene.Type.String()
}