-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtypes.go
More file actions
280 lines (236 loc) · 6.48 KB
/
types.go
File metadata and controls
280 lines (236 loc) · 6.48 KB
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
package scaf
import (
"errors"
"fmt"
"strconv"
"strings"
"unicode"
)
// Type parsing errors.
var (
ErrEmptyTypeString = errors.New("empty type string")
ErrInvalidArrayType = errors.New("invalid array type")
ErrInvalidMapType = errors.New("invalid map type")
ErrUnrecognizedType = errors.New("unrecognized type")
)
// TypeKind represents the kind of a type.
type TypeKind string
// Type kind constants.
const (
TypeKindPrimitive TypeKind = "primitive" // string, int, bool, float64, etc.
TypeKindSlice TypeKind = "slice" // []T
TypeKindArray TypeKind = "array" // [N]T
TypeKindMap TypeKind = "map" // map[K]V
TypeKindPointer TypeKind = "pointer" // *T
TypeKindNamed TypeKind = "named" // time.Time, uuid.UUID, etc.
)
// Type represents a type in the schema.
// This is a recursive structure that can represent complex types like []map[string]*Person.
type Type struct {
// Kind is the category of this type.
Kind TypeKind
// Name is the type name.
// For primitives: "string", "int", "bool", "float64", etc.
// For named types: "Time", "UUID", etc.
Name string
// Package is the package path for named types (e.g., "time", "github.com/google/uuid").
// Empty for primitives.
Package string
// Elem is the element type for slices, arrays, pointers, and map values.
Elem *Type
// Key is the key type for maps.
Key *Type
// ArrayLen is the length for array types.
ArrayLen int
}
// String returns a Go-style string representation of the type.
func (t *Type) String() string {
if t == nil {
return ""
}
switch t.Kind {
case TypeKindPrimitive:
return t.Name
case TypeKindSlice:
return "[]" + t.Elem.String()
case TypeKindArray:
return "[" + strconv.Itoa(t.ArrayLen) + "]" + t.Elem.String()
case TypeKindMap:
return "map[" + t.Key.String() + "]" + t.Elem.String()
case TypeKindPointer:
return "*" + t.Elem.String()
case TypeKindNamed:
if t.Package != "" {
return t.Package + "." + t.Name
}
return t.Name
default:
return t.Name
}
}
// ParseTypeString parses a Go-style type string into a Type.
// Supports: string, int, int64, float64, bool, []T, [N]T, *T, map[K]V, pkg.Name
//
// Examples:
//
// "string" -> TypeKindPrimitive, Name="string"
// "[]string" -> TypeKindSlice, Elem=string
// "[5]int" -> TypeKindArray, ArrayLen=5, Elem=int
// "*int" -> TypeKindPointer, Elem=int
// "map[string]int" -> TypeKindMap, Key=string, Elem=int
// "time.Time" -> TypeKindNamed, Package="time", Name="Time"
// "[]map[string]*int" -> nested types
func ParseTypeString(s string) (*Type, error) {
s = strings.TrimSpace(s)
if s == "" {
return nil, ErrEmptyTypeString
}
return parseType(s)
}
// parseType recursively parses a type string.
func parseType(s string) (*Type, error) {
// Check for slice: []T
if strings.HasPrefix(s, "[]") {
elem, err := parseType(s[2:])
if err != nil {
return nil, err
}
return SliceOf(elem), nil
}
// Check for array: [N]T
if strings.HasPrefix(s, "[") {
closeIdx := strings.Index(s, "]")
if closeIdx == -1 {
return nil, fmt.Errorf("%w: %s", ErrInvalidArrayType, s)
}
lenStr := s[1:closeIdx]
if lenStr == "" {
return nil, fmt.Errorf("%w: %s", ErrInvalidArrayType, s)
}
arrayLen, err := strconv.Atoi(lenStr)
if err != nil {
return nil, fmt.Errorf("invalid array length %q: %w", lenStr, err)
}
elem, err := parseType(s[closeIdx+1:])
if err != nil {
return nil, err
}
return &Type{Kind: TypeKindArray, ArrayLen: arrayLen, Elem: elem}, nil
}
// Check for pointer: *T
if strings.HasPrefix(s, "*") {
elem, err := parseType(s[1:])
if err != nil {
return nil, err
}
return PointerTo(elem), nil
}
// Check for map: map[K]V
if strings.HasPrefix(s, "map[") {
// Find the closing bracket for the key type
depth := 0
keyEnd := -1
for i := 4; i < len(s); i++ {
switch s[i] {
case '[':
depth++
case ']':
if depth == 0 {
keyEnd = i
} else {
depth--
}
}
if keyEnd != -1 {
break
}
}
if keyEnd == -1 {
return nil, fmt.Errorf("%w: %s", ErrInvalidMapType, s)
}
keyStr := s[4:keyEnd]
valueStr := s[keyEnd+1:]
key, err := parseType(keyStr)
if err != nil {
return nil, fmt.Errorf("invalid map key type: %w", err)
}
value, err := parseType(valueStr)
if err != nil {
return nil, fmt.Errorf("invalid map value type: %w", err)
}
return MapOf(key, value), nil
}
// Check for named type: pkg.Name
if idx := strings.LastIndex(s, "."); idx > 0 {
pkg := s[:idx]
name := s[idx+1:]
if isValidTypeIdentifier(pkg) && isValidTypeIdentifier(name) {
return NamedType(pkg, name), nil
}
}
// Check for primitive types
if isPrimitiveType(s) {
return &Type{Kind: TypeKindPrimitive, Name: s}, nil
}
// Treat as named type without package (user-defined type in same package)
if isValidTypeIdentifier(s) {
return NamedType("", s), nil
}
return nil, fmt.Errorf("%w: %s", ErrUnrecognizedType, s)
}
// isPrimitiveType returns true if s is a Go primitive type name.
func isPrimitiveType(s string) bool {
switch s {
case "bool", "string",
"int", "int8", "int16", "int32", "int64",
"uint", "uint8", "uint16", "uint32", "uint64", "uintptr",
"byte", "rune",
"float32", "float64",
"complex64", "complex128",
"any", "error":
return true
}
return false
}
// isValidTypeIdentifier returns true if s is a valid Go identifier.
func isValidTypeIdentifier(s string) bool {
if s == "" {
return false
}
for i, r := range s {
if i == 0 {
if !unicode.IsLetter(r) && r != '_' {
return false
}
} else {
if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '_' {
return false
}
}
}
return true
}
// Primitive type constructors for convenience.
var (
TypeString = &Type{Kind: TypeKindPrimitive, Name: "string"}
TypeInt = &Type{Kind: TypeKindPrimitive, Name: "int"}
TypeInt64 = &Type{Kind: TypeKindPrimitive, Name: "int64"}
TypeFloat64 = &Type{Kind: TypeKindPrimitive, Name: "float64"}
TypeBool = &Type{Kind: TypeKindPrimitive, Name: "bool"}
)
// SliceOf creates a slice type.
func SliceOf(elem *Type) *Type {
return &Type{Kind: TypeKindSlice, Elem: elem}
}
// PointerTo creates a pointer type.
func PointerTo(elem *Type) *Type {
return &Type{Kind: TypeKindPointer, Elem: elem}
}
// MapOf creates a map type.
func MapOf(key, value *Type) *Type {
return &Type{Kind: TypeKindMap, Key: key, Elem: value}
}
// NamedType creates a named type.
func NamedType(pkg, name string) *Type {
return &Type{Kind: TypeKindNamed, Package: pkg, Name: name}
}