-
Notifications
You must be signed in to change notification settings - Fork 3
/
type.go
279 lines (248 loc) · 4.43 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package gofire
import (
"fmt"
"strings"
)
// Kind holds information about supported data kinds.
type Kind uint8
const (
Invalid Kind = iota
Bool
Int
Int8
Int16
Int32
Int64
Uint
Uint8
Uint16
Uint32
Uint64
Float32
Float64
Complex64
Complex128
String
Array
Slice
Map
Ptr
Struct
// Kinds bellow are not parsed and not processed by generators
// but still defined here for visibility and potentially could be
// processed in the future.
UnsafePointer
Chan
Func
Interface
)
func (k Kind) Type() string {
switch k {
case Bool:
return "bool"
case Int:
return "int"
case Int8:
return "int8"
case Int16:
return "int16"
case Int32:
return "int32"
case Int64:
return "int64"
case Uint:
return "uint"
case Uint8:
return "uint8"
case Uint16:
return "uint16"
case Uint32:
return "uint32"
case Uint64:
return "uint64"
case Float32:
return "float32"
case Float64:
return "float64"
case Complex64:
return "complex64"
case Complex128:
return "complex128"
case String:
return "string"
case Array:
return "array"
case Slice:
return "slice"
case Map:
return "map"
case Ptr:
return "ptr"
default:
return "invalid"
}
}
func (k Kind) Default() interface{} {
switch k {
case Bool:
return false
case Int, Int8, Int16, Int32, Int64:
return int64(0)
case Uint, Uint8, Uint16, Uint32, Uint64:
return uint64(0)
case Float32, Float64:
return float64(0.0)
case Complex64, Complex128:
return complex128(0.0)
case String:
return ""
case Array, Slice:
return []interface{}{}
case Map:
return map[interface{}]interface{}{}
default:
return nil
}
}
func (k Kind) Base() int16 {
switch k {
case Int:
return 64
case Int8:
return 8
case Int16:
return 16
case Int32:
return 32
case Int64:
return 64
case Uint:
return 64
case Uint8:
return 8
case Uint16:
return 16
case Uint32:
return 32
case Uint64:
return 64
case Float32:
return 32
case Float64:
return 64
case Complex64:
return 64
case Complex128:
return 128
default:
return 0
}
}
// Typ defines an abstraction for a data type.
type Typ interface {
Kind() Kind
Type() string
Format(interface{}) string
}
type TPrimitive struct {
TKind Kind
}
func (t TPrimitive) Kind() Kind {
return t.TKind
}
func (t TPrimitive) Type() string {
return t.TKind.Type()
}
func (t TPrimitive) Format(v interface{}) string {
switch t.TKind {
case Bool:
return fmt.Sprintf("%t", v)
case Int, Int8, Int16, Int32, Int64:
return fmt.Sprintf("%d", v)
case Uint, Uint8, Uint16, Uint32, Uint64:
return fmt.Sprintf("%d", v)
case Float32, Float64:
return fmt.Sprintf("%f", v)
case String:
return fmt.Sprintf("%q", v)
default:
return fmt.Sprintf("%v", v)
}
}
type TArray struct {
ETyp Typ
Size int64
}
func (TArray) Kind() Kind {
return Array
}
func (t TArray) Type() string {
return fmt.Sprintf("[%d]%s", t.Size, t.ETyp.Type())
}
func (t TArray) Format(v interface{}) string {
vs := v.([]interface{})
fmts := make([]string, 0, len(vs))
for _, v := range v.([]interface{}) {
fmts = append(fmts, t.ETyp.Format(v))
}
return fmt.Sprintf("%s{%s}", t.Type(), strings.Join(fmts, ","))
}
type TSlice struct {
ETyp Typ
}
func (TSlice) Kind() Kind {
return Slice
}
func (t TSlice) Type() string {
return fmt.Sprintf("[]%s", t.ETyp.Type())
}
func (t TSlice) Format(v interface{}) string {
vs := v.([]interface{})
fmts := make([]string, 0, len(vs))
for _, v := range v.([]interface{}) {
fmts = append(fmts, t.ETyp.Format(v))
}
return fmt.Sprintf("%s{%s}", t.Type(), strings.Join(fmts, ","))
}
type TMap struct {
KTyp, VTyp Typ
}
func (TMap) Kind() Kind {
return Map
}
func (t TMap) Type() string {
return fmt.Sprintf("map[%s]%s", t.KTyp.Type(), t.VTyp.Type())
}
func (t TMap) Format(v interface{}) string {
vs := v.(map[interface{}]interface{})
fmts := make([]string, 0, len(vs))
for k, v := range v.(map[interface{}]interface{}) {
kv := fmt.Sprintf("%s:%s", t.KTyp.Format(k), t.VTyp.Format(v))
fmts = append(fmts, kv)
}
return fmt.Sprintf("%s{%s}", t.Type(), strings.Join(fmts, ","))
}
type TPtr struct {
ETyp Typ
}
func (TPtr) Kind() Kind {
return Ptr
}
func (t TPtr) Type() string {
return fmt.Sprintf("*%s", t.ETyp.Type())
}
func (t TPtr) Format(v interface{}) string {
return "nil"
}
type TStruct struct {
Typ string
}
func (TStruct) Kind() Kind {
return Struct
}
func (t TStruct) Type() string {
return t.Typ
}
func (t TStruct) Format(v interface{}) string {
return fmt.Sprintf("%s{}", t.Typ)
}