/
types.go
87 lines (74 loc) · 2.01 KB
/
types.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
package types
import (
"encoding"
"encoding/json"
"reflect"
"time"
)
// Unmarshalers is the list of possible unmarshaler kcd support.
var Unmarshalers = []reflect.Type{
reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem(),
reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem(),
reflect.TypeOf((*json.Unmarshaler)(nil)).Elem(),
}
var (
// TextUnmarshaller is the type of encoding.TextUnmarshaler
TextUnmarshaller = Unmarshalers[0]
// BinaryUnmarshaler is the type of encoding.BinaryUnmarshaler
BinaryUnmarshaler = Unmarshalers[1]
// JSONUnmarshaler is the type of json.Unmarshaler
JSONUnmarshaler = Unmarshalers[2]
)
// IsImplementingUnmarshaler check if the type t implement one of the possible unmarshalers.
func IsImplementingUnmarshaler(t reflect.Type) bool {
if t.Kind() != reflect.Ptr {
t = reflect.New(t).Type()
}
for _, u := range Unmarshalers {
if t.Implements(u) {
return true
}
}
return false
}
var d = time.Duration(1)
// Custom is the list of custom types supported.
var Custom = []reflect.Type{
reflect.TypeOf(time.Duration(1)),
reflect.TypeOf(&d),
}
// IsCustomType check the type is a custom type.
func IsCustomType(t reflect.Type) bool {
for _, c := range Custom {
if t.AssignableTo(c) {
return true
}
}
return false
}
// Native is the list of supported native types.
var Native = map[reflect.Kind]bool{
reflect.String: true,
reflect.Bool: true,
reflect.Int: true,
reflect.Int8: true,
reflect.Int16: true,
reflect.Int32: true,
reflect.Int64: true,
reflect.Uint: true,
reflect.Uint8: true,
reflect.Uint16: true,
reflect.Uint32: true,
reflect.Uint64: true,
reflect.Float32: true,
reflect.Float64: true,
}
// IsNative check if the type t is a native type.
func IsNative(t reflect.Type) bool {
_, ok := Native[t.Kind()]
return ok
}
// IsUnmarshallable check if the type t is either a native, custom type or implement an unmarshaler.
func IsUnmarshallable(t reflect.Type) bool {
return IsNative(t) || IsCustomType(t) || IsImplementingUnmarshaler(t)
}