-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_chain.go
62 lines (54 loc) · 1.32 KB
/
convert_chain.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 tlv
import (
"reflect"
"sync"
)
var (
initConverterOnce sync.Once
converter _NextConverter
)
func initConverter() {
initConverterOnce.Do(func() {
chain := []_Converter{
_ConverterFunc(_ConvertPointer),
_ConverterFunc(_ConvertUnmarshaler),
_ConverterFunc(_ConvertTLV),
_ConverterFunc(_ConvertTL),
_ConverterFunc(_ConvertString),
_ConverterFunc(_ConvertInt),
_ConverterFunc(_ConvertUInt64),
_ConverterFunc(_ConvertSlice),
_ConverterFunc(_ConvertInterface),
_ConvertStruct{},
_ConvertMap{},
_ConverterFunc(_ConvertByte),
}
converter = _BaseConverter
for i := len(chain) - 1; i >= 0; i-- {
converter = _AddLinkToChain(chain[i], converter)
}
})
}
func _AddLinkToChain(converter _Converter, next _NextConverter) _NextConverter {
return func(ctx _ConvertContext) (reflect.Value, error) {
result, err := converter.Convert(ctx, next)
return result, err
}
}
func _BaseConverter(ctx _ConvertContext) (reflect.Value, error) {
return reflect.Value{}, _ErrInvalidKind
}
func convert(rt reflect.Type, values TaggedValues, tagopts []string) (reflect.Value, error) {
initConverter()
ctx := _ConvertContext{
rt: rt,
values: values,
tagopts: tagopts,
}
return converter(ctx)
}
type _ConvertContext struct {
rt reflect.Type
values TaggedValues
tagopts []string
}