-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtypecheck.go
325 lines (303 loc) · 8.33 KB
/
typecheck.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package mexpr
import (
"fmt"
"sort"
"strings"
)
type valueType string
const (
typeUnknown valueType = "unknown"
typeBool valueType = "boolean"
typeNumber valueType = "number"
typeString valueType = "string"
typeArray valueType = "array"
typeObject valueType = "object"
)
// mapKeys returns the keys of the map m.
// The keys will be in an indeterminate order.
func mapKeys[M ~map[K]V, K comparable, V any](m M) []K {
r := make([]K, 0, len(m))
for k := range m {
r = append(r, k)
}
return r
}
type schema struct {
typeName valueType
items *schema
properties map[string]*schema
}
func (s *schema) String() string {
if s.isArray() {
return fmt.Sprintf("%s[%s]", s.typeName, s.items)
}
if s.isObject() {
return fmt.Sprintf("%s{%v}", s.typeName, mapKeys(s.properties))
}
return string(s.typeName)
}
func (s *schema) isNumber() bool {
return s != nil && s.typeName == typeNumber
}
func (s *schema) isString() bool {
return s != nil && s.typeName == typeString
}
func (s *schema) isArray() bool {
return s != nil && s.typeName == typeArray
}
func (s *schema) isObject() bool {
return s != nil && s.typeName == typeObject
}
var (
schemaBool = newSchema(typeBool)
schemaNumber = newSchema(typeNumber)
schemaString = newSchema(typeString)
)
func newSchema(t valueType) *schema {
return &schema{typeName: t}
}
func getSchema(v any) *schema {
switch i := v.(type) {
case bool:
return schemaBool
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
return schemaNumber
case string, []byte:
return schemaString
case []any:
s := newSchema(typeArray)
if len(i) > 0 {
s.items = getSchema(i[0])
}
return s
case map[string]any:
m := newSchema(typeObject)
m.properties = make(map[string]*schema, len(i))
for k, v := range i {
m.properties[k] = getSchema(v)
}
return m
case map[any]any:
m := newSchema(typeObject)
m.properties = make(map[string]*schema, len(i))
for k, v := range i {
m.properties[toString(k)] = getSchema(v)
}
return m
}
return newSchema(typeUnknown)
}
// TypeChecker checks to ensure types used for operations will work.
type TypeChecker interface {
Run(value any) Error
}
// NewTypeChecker returns a type checker for the given AST.
func NewTypeChecker(ast *Node, options ...InterpreterOption) TypeChecker {
unquoted := false
for _, opt := range options {
switch opt {
case UnquotedStrings:
unquoted = true
}
}
return &typeChecker{
ast: ast,
unquoted: unquoted,
}
}
type typeChecker struct {
ast *Node
prevFieldSelect bool
unquoted bool
}
func (i *typeChecker) Run(value any) Error {
_, err := i.run(i.ast, value)
return err
}
func (i *typeChecker) runBoth(ast *Node, value any) (*schema, *schema, Error) {
leftType, err := i.run(ast.Left, value)
if err != nil {
return nil, nil, err
}
rightType, err := i.run(ast.Right, value)
if err != nil {
return nil, nil, err
}
return leftType, rightType, nil
}
func (i *typeChecker) run(ast *Node, value any) (*schema, Error) {
fromSelect := i.prevFieldSelect
i.prevFieldSelect = false
switch ast.Type {
case NodeIdentifier:
switch ast.Value.(string) {
case "@":
if s, ok := value.(*schema); ok {
return s, nil
}
return getSchema(value), nil
case "length":
return schemaNumber, nil
case "lower", "upper":
return schemaString, nil
}
errValue := value
if s, ok := value.(*schema); ok {
if v, ok := s.properties[ast.Value.(string)]; ok {
return v, nil
}
keys := []string{}
for k := range s.properties {
keys = append(keys, k)
}
errValue = "map with keys [" + strings.Join(keys, ", ") + "]"
}
if m, ok := value.(map[string]any); ok {
if v, ok := m[ast.Value.(string)]; ok {
return getSchema(v), nil
}
keys := []string{}
for k := range m {
keys = append(keys, k)
}
errValue = "map with keys [" + strings.Join(keys, ", ") + "]"
}
if m, ok := value.(map[any]any); ok {
if v, ok := m[ast.Value]; ok {
return getSchema(v), nil
}
keys := []string{}
for k := range m {
keys = append(keys, toString(k))
}
errValue = "map with keys [" + strings.Join(keys, ", ") + "]"
}
if i.unquoted && !fromSelect {
// Identifiers not found in the map are treated as strings, but only if
// the previous item was not a `.` like `obj.field`.
return schemaString, nil
}
return nil, NewError(ast.Offset, ast.Length, "no property %v in %v", ast.Value, errValue)
case NodeFieldSelect:
i.prevFieldSelect = true
leftType, err := i.run(ast.Left, value)
if err != nil {
return nil, err
}
i.prevFieldSelect = true
return i.run(ast.Right, leftType)
case NodeArrayIndex:
leftType, rightType, err := i.runBoth(ast, value)
if err != nil {
return nil, err
}
if !(leftType.isString() || leftType.isArray()) {
return nil, NewError(ast.Offset, ast.Length, "can only index strings or arrays but got %v", leftType)
}
if rightType.isArray() {
// This is a slice!
return leftType, nil
}
if rightType.isNumber() {
if leftType.isString() {
return leftType, nil
}
return leftType.items, nil
}
return nil, NewError(ast.Offset, ast.Length, "array index must be number or slice but found %v", rightType)
case NodeSlice:
leftType, rightType, err := i.runBoth(ast, value)
if err != nil {
return nil, err
}
if !leftType.isNumber() {
return nil, NewError(ast.Offset, ast.Length, "slice index must be a number but found %s", leftType)
}
if !rightType.isNumber() {
return nil, NewError(ast.Offset, ast.Length, "slice index must be a number but found %s", rightType)
}
s := newSchema(typeArray)
s.items = leftType
return s, nil
case NodeLiteral:
return getSchema(ast.Value), nil
case NodeSign:
rightType, err := i.run(ast.Right, value)
if err != nil {
return nil, err
}
if !rightType.isNumber() {
return nil, NewError(ast.Offset, ast.Length, "expected number but found %s", rightType)
}
return schemaNumber, nil
case NodeAdd, NodeSubtract, NodeMultiply, NodeDivide, NodeModulus, NodePower:
leftType, rightType, err := i.runBoth(ast, value)
if err != nil {
return nil, err
}
if ast.Type == NodeAdd {
if leftType.isString() || rightType.isString() {
return schemaString, nil
}
if leftType.isArray() && rightType.isArray() {
if leftType.items.typeName != rightType.items.typeName {
return nil, NewError(ast.Offset, ast.Length, "array item types don't match: %s vs %s", leftType.items, rightType.items)
}
return leftType, nil
}
}
if leftType.isNumber() && rightType.isNumber() {
return leftType, nil
}
return nil, NewError(ast.Offset, ast.Length, "cannot operate on incompatible types %v and %v", leftType.typeName, rightType.typeName)
case NodeLessThan, NodeLessThanEqual, NodeGreaterThan, NodeGreaterThanEqual:
leftType, rightType, err := i.runBoth(ast, value)
if err != nil {
return nil, err
}
if !leftType.isNumber() || !rightType.isNumber() {
return nil, NewError(ast.Offset, ast.Length, "cannot compare %s with %s", leftType, rightType)
}
return schemaBool, nil
case NodeEqual, NodeNotEqual, NodeAnd, NodeOr, NodeIn, NodeContains, NodeStartsWith, NodeEndsWith, NodeBefore, NodeAfter:
_, _, err := i.runBoth(ast, value)
if err != nil {
return nil, err
}
return schemaBool, nil
case NodeWhere:
leftType, err := i.run(ast.Left, value)
if err != nil {
return nil, err
}
if leftType.isObject() {
keys := mapKeys(leftType.properties)
sort.Strings(keys)
if len(keys) > 0 {
// Pick the first prop as the representative item type.
prop := leftType.properties[keys[0]]
leftType = newSchema(typeArray)
leftType.items = prop
}
}
if !leftType.isArray() || leftType.items == nil {
return nil, NewError(ast.Offset, ast.Length, "where clause requires a non-empty array or object, but found %s", leftType)
}
// In an unquoted string scenario it makes no sense for the first/only
// token after a `where` clause to be treated as a string. Instead we
// treat a `where` the same as a field select `.` in this scenario.
i.prevFieldSelect = true
_, err = i.run(ast.Right, leftType.items)
if err != nil {
return nil, err
}
return leftType, nil
case NodeNot:
_, err := i.run(ast.Right, value)
if err != nil {
return nil, err
}
return schemaBool, nil
}
return nil, NewError(ast.Offset, ast.Length, "unexpected node %v", ast)
}