-
Notifications
You must be signed in to change notification settings - Fork 2
/
type.go
131 lines (125 loc) · 3.01 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package gtly
import (
"github.com/viant/toolbox"
"reflect"
"time"
)
const ( //Data type definition
//FieldTypeInt int type
FieldTypeInt = "int"
//FieldTypeInt64 int type
FieldTypeInt64 = "int64"
//FieldTypeFloat32 float type
FieldTypeFloat32 = "float32"
//FieldTypeFloat64 float type
FieldTypeFloat64 = "float64"
//FieldTypeBool bool type
FieldTypeBool = "bool"
//FieldTypeString string type
FieldTypeString = "string"
//FieldTypeTime time type
FieldTypeTime = "time"
//FieldTypeBytes bytes type
FieldTypeBytes = "bytes"
//FieldTypeArray array type
FieldTypeArray = "array"
//FieldTypeObject object type
FieldTypeObject = "object"
)
var (
typeInt = reflect.TypeOf(0)
typeInt64 = reflect.TypeOf(int64(0))
typeFloat = reflect.TypeOf(float32(0))
typeFloat64 = reflect.TypeOf(float64(0))
typeBool = reflect.TypeOf(true)
typeString = reflect.TypeOf("")
typeBytes = reflect.TypeOf([]byte(""))
typeTime = reflect.TypeOf(time.Time{})
typeTimePtr = reflect.TypeOf(&time.Time{})
)
//typeNameForValue returns base type
func typeNameForType(t reflect.Type) string {
switch t.Kind() {
case reflect.Float32:
return FieldTypeFloat32
case reflect.Float64:
return FieldTypeFloat64
case reflect.Int, reflect.Uint64, reflect.Uint:
return FieldTypeInt
case reflect.Int64:
return FieldTypeInt64
case reflect.Bool:
return FieldTypeBool
case reflect.Struct:
if t == typeTime {
return FieldTypeTime
}
case reflect.Slice:
switch t.Elem().Kind() {
case reflect.Uint8:
return FieldTypeBytes
}
case reflect.Ptr:
switch t.Elem().Kind() {
case reflect.Float32:
return FieldTypeFloat32
case reflect.Float64:
return FieldTypeFloat64
case reflect.Int, reflect.Uint64, reflect.Uint:
return FieldTypeInt
case reflect.Int64:
return FieldTypeInt64
case reflect.Bool:
return FieldTypeBool
case reflect.Struct:
if t == typeTimePtr {
return FieldTypeTime
}
}
}
return FieldTypeString
}
//typeNameForValue returns base type
func typeNameForValue(value interface{}) string {
switch val := value.(type) {
case float32, *float32:
return FieldTypeFloat32
case float64, *float64:
return FieldTypeFloat64
case int, int8, int16, int32, uint, uint8, uint16, uint32, uint64, *int, *int8, *int16, *int32, *uint, *uint8, *uint16, *uint32, *uint64:
return FieldTypeInt
case int64, *int64:
return FieldTypeInt64
case time.Time, *time.Time:
return FieldTypeTime
case bool, *bool:
return FieldTypeBool
case []byte:
if _, err := toolbox.ToFloat(val); err == nil {
return FieldTypeFloat32
}
}
return FieldTypeString
}
//getBaseType returns base type for supplied name
func getBaseType(typeName string) reflect.Type {
switch typeName {
case FieldTypeInt:
return typeInt
case FieldTypeInt64:
return typeInt64
case FieldTypeFloat32:
return typeFloat
case FieldTypeFloat64:
return typeFloat64
case FieldTypeBool:
return typeBool
case FieldTypeString:
return typeString
case FieldTypeTime:
return typeTime
case FieldTypeBytes:
return typeBytes
}
return nil
}