-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
71 lines (65 loc) · 1.25 KB
/
type.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
package types
import "github.com/pkg/errors"
// TypeIndex defines the index of runtime type
type TypeIndex int16
const (
TypeEmpty TypeIndex = iota // wrong type, no handler for empty type
TypeInt8
TypeInt16
TypeInt32
TypeInt64
TypeUint8
TypeUint16
TypeUint32
TypeUint64
TypeBool
TypeString // string with utf-8 encoder
TypeByteArray
)
func AssertType(v interface{}) TypeIndex {
switch v.(type) {
case int8:
return TypeInt8
case int16:
return TypeInt16
case int32:
return TypeInt32
case int64:
return TypeInt64
case uint8:
return TypeUint8
case uint16:
return TypeUint16
case uint32:
return TypeUint32
case uint64:
return TypeUint64
case bool:
return TypeBool
case string:
return TypeString
case []byte:
return TypeByteArray
// for struct
// case MyStruct, *MyStruct:
// return TypeMyStruct
}
return TypeEmpty
}
func TypeObjectMapping(index TypeIndex) (IType, error) {
switch index {
case TypeByteArray:
return NewByteArrary(), nil
case TypeString:
return NewString(), nil
case TypeBool:
return NewBool(), nil
case TypeInt32:
return NewInt32(), nil
case TypeInt64:
return NewInt64(), nil
case TypeUint64:
return NewUint64(), nil
}
return nil, errors.Errorf("type of index %d is not valid", index)
}