-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_type.go
281 lines (254 loc) · 5.97 KB
/
go_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
280
281
package ens
import (
"reflect"
"slices"
"github.com/things-go/ens/rapier"
"github.com/things-go/ens/utils"
"google.golang.org/protobuf/reflect/protoreflect"
)
var typeNames = [...]string{
TypeInvalid: "invalid",
TypeBool: "bool",
TypeInt8: "int8",
TypeInt16: "int16",
TypeInt32: "int32",
TypeInt64: "int64",
TypeInt: "int",
TypeUint8: "uint8",
TypeUint16: "uint16",
TypeUint32: "uint32",
TypeUint64: "uint64",
TypeUint: "uint",
TypeFloat32: "float32",
TypeFloat64: "float64",
TypeDecimal: "string",
TypeString: "string",
TypeEnum: "string",
TypeBytes: "[]byte",
TypeTime: "time.Time",
TypeJSON: "json.RawMessage",
TypeUUID: "[16]byte",
TypeOther: "other",
}
// A Type represents a field type.
type Type uint8
// List of field types.
const (
TypeInvalid Type = iota
TypeBool
TypeInt8
TypeInt16
TypeInt32
TypeInt64
TypeInt
TypeUint8
TypeUint16
TypeUint32
TypeUint64
TypeUint
TypeFloat32
TypeFloat64
TypeDecimal
TypeString
TypeEnum
TypeBytes
TypeTime
TypeJSON
TypeUUID
TypeOther
endTypes
)
// String returns the string representation of a type.
func (t Type) String() string {
if t < endTypes {
return typeNames[t]
}
return typeNames[TypeInvalid]
}
// IsNumeric reports if the given type is a numeric type.
func (t Type) IsNumeric() bool {
return t >= TypeInt8 && t <= TypeFloat64
}
// IsFloat reports if the given type is a float type.
func (t Type) IsFloat() bool {
return t == TypeFloat32 || t == TypeFloat64
}
// IsInteger reports if the given type is an integral type.
func (t Type) IsInteger() bool {
return t.IsNumeric() && !t.IsFloat()
}
// IsBool reports if the given type is an bool type.
func (t Type) IsBool() bool {
return t == TypeBool
}
// IsTime reports if the given type is an time.Time type.
func (t Type) IsTime() bool {
return t == TypeTime
}
// IsValid reports if the given type if known type.
func (t Type) IsValid() bool {
return t > TypeInvalid && t < endTypes
}
func (t Type) IntoProtoKind() (k protoreflect.Kind, n string) {
switch t {
case TypeBool:
k = protoreflect.BoolKind
n = k.String()
case TypeInt8, TypeInt16, TypeInt32, TypeInt:
k = protoreflect.Int32Kind
n = k.String()
case TypeInt64:
k = protoreflect.Int64Kind
n = k.String()
case TypeUint8, TypeUint16, TypeUint32, TypeUint:
k = protoreflect.Uint32Kind
n = k.String()
case TypeUint64:
k = protoreflect.Uint64Kind
n = k.String()
case TypeFloat32:
k = protoreflect.FloatKind
n = k.String()
case TypeFloat64:
k = protoreflect.DoubleKind
n = k.String()
case TypeDecimal, TypeString, TypeEnum, TypeJSON, TypeUUID, TypeOther:
k = protoreflect.StringKind
n = k.String()
case TypeBytes:
k = protoreflect.BytesKind
n = k.String()
case TypeTime:
k = protoreflect.MessageKind
n = "google.protobuf.Timestamp"
default:
k = protoreflect.StringKind
n = k.String()
}
return k, n
}
func (t Type) IntoRapierType() rapier.Type {
switch t {
case TypeBool:
return rapier.Bool
case TypeInt8:
return rapier.Int8
case TypeInt16:
return rapier.Int16
case TypeInt32:
return rapier.Int32
case TypeInt64:
return rapier.Int64
case TypeInt:
return rapier.Int
case TypeUint8:
return rapier.Uint8
case TypeUint16:
return rapier.Uint16
case TypeUint32:
return rapier.Uint32
case TypeUint64:
return rapier.Uint64
case TypeUint:
return rapier.Uint
case TypeFloat32:
return rapier.Float32
case TypeFloat64:
return rapier.Float64
case TypeString:
return rapier.String
case TypeEnum:
return rapier.Enum
case TypeDecimal:
return rapier.Decimal
case TypeBytes:
return rapier.Bytes
case TypeTime:
return rapier.Time
case TypeJSON:
return rapier.JSON
case TypeUUID:
return rapier.UUID
case TypeOther:
fallthrough
default:
return rapier.Field
}
}
type GoType struct {
Type Type // Type enum.
Ident string // Type identifier, e.g. int, time.Time, sql.NullInt64.
PkgPath string // import path. e.g. "", time, database/sql.
PkgQualifier string // a package qualifier. e.g. "", time, sql.
NonPointer bool // pointers or slices, means not need pointer.
}
func NewGoType(t Type, v any) GoType {
return newGoType(t, reflect.TypeOf(v))
}
func newGoType(t Type, tt reflect.Type) GoType {
tv := indirect(tt)
return GoType{
Type: t,
Ident: tt.String(),
PkgPath: tv.PkgPath(),
PkgQualifier: utils.PkgQualifier(tv.String()),
NonPointer: slices.Contains([]reflect.Kind{reflect.Slice, reflect.Ptr, reflect.Map}, tt.Kind()),
}
}
func (t GoType) WithNewType(tp Type) GoType {
t.Type = tp
return t
}
func (t *GoType) Clone() GoType {
tt := *t
return tt
}
// String returns the string representation of a type.
func (t *GoType) String() string {
switch {
case t.Ident != "":
return t.Ident
case t.Type < endTypes:
return typeNames[t.Type]
default:
return typeNames[TypeInvalid]
}
}
// IsNumeric reports if the given type is a numeric type.
func (t *GoType) IsNumeric() bool {
return t.Type.IsNumeric()
}
// IsFloat reports if the given type is a float type.
func (t *GoType) IsFloat() bool {
return t.Type.IsFloat()
}
// IsInteger reports if the given type is an integral type.
func (t *GoType) IsInteger() bool {
return t.Type.IsInteger()
}
// IsBool reports if the given type is an bool type.
func (t *GoType) IsBool() bool {
return t.Type.IsBool()
}
// IsTime reports if the given type is an time.Time type.
func (t *GoType) IsTime() bool {
return t.Type.IsTime()
}
// IsValid reports if the given type if known type.
func (t *GoType) IsValid() bool {
return t.Type.IsValid()
}
// Comparable reports whether values of this type are comparable.
func (t *GoType) Comparable() bool {
switch t.Type {
case TypeBool, TypeTime, TypeUUID, TypeEnum, TypeString:
return true
case TypeOther:
// Always accept custom types as comparable on the database side.
// In the future, we should consider adding an interface to let
// custom types tell if they are comparable or not (see #1304).
return true
default:
return t.Type.IsNumeric()
}
}