forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr_type.go
352 lines (311 loc) · 8.85 KB
/
expr_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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package actionlint
import (
"fmt"
"strings"
)
// Types
// ExprType is interface for types of values in expression.
type ExprType interface {
// String returns string representation of the type.
String() string
// Assignable returns if other type can be assignable to the type.
Assignable(other ExprType) bool
// Equals returns if the type is equal to the other type.
Equals(other ExprType) bool
// Fuse merges other type into this type. When other type conflicts with this type, fused
// result is any type as fallback.
Fuse(other ExprType) ExprType
}
// AnyType represents type which can be any type. It also indicates that a value of the type cannot
// be type-checked since it's type cannot be known statically.
type AnyType struct{}
func (ty AnyType) String() string {
return "any"
}
// Assignable returns if other type can be assignable to the type.
func (ty AnyType) Assignable(_ ExprType) bool {
return true
}
// Equals returns if the type is equal to the other type.
func (ty AnyType) Equals(other ExprType) bool {
return true
}
// Fuse merges other type into this type. When other type conflicts with this type, fused result is
// any type as fallback.
func (ty AnyType) Fuse(other ExprType) ExprType {
return ty
}
// NullType is type for null value.
type NullType struct{}
func (ty NullType) String() string {
return "null"
}
// Assignable returns if other type can be assignable to the type.
func (ty NullType) Assignable(other ExprType) bool {
switch other.(type) {
case NullType, AnyType:
return true
default:
return false
}
}
// Equals returns if the type is equal to the other type.
func (ty NullType) Equals(other ExprType) bool {
switch other.(type) {
case NullType, AnyType:
return true
default:
return false
}
}
// Fuse merges other type into this type. When other type conflicts with this type, fused result is
// any type as fallback.
func (ty NullType) Fuse(other ExprType) ExprType {
if _, ok := other.(NullType); ok {
return ty
}
return AnyType{}
}
// NumberType is type for number values such as integer or float.
type NumberType struct{}
func (ty NumberType) String() string {
return "number"
}
// Equals returns if the type is equal to the other type.
func (ty NumberType) Equals(other ExprType) bool {
switch other.(type) {
case NumberType, AnyType:
return true
default:
return false
}
}
// Assignable returns if other type can be assignable to the type.
func (ty NumberType) Assignable(other ExprType) bool {
// TODO: Is string of numbers corced into number?
switch other.(type) {
case NumberType, AnyType:
return true
default:
return false
}
}
// Fuse merges other type into this type. When other type conflicts with this type, fused result is
// any type as fallback.
func (ty NumberType) Fuse(other ExprType) ExprType {
if _, ok := other.(NumberType); ok {
return ty
}
return AnyType{}
}
// BoolType is type for boolean values.
type BoolType struct{}
func (ty BoolType) String() string {
return "bool"
}
// Assignable returns if other type can be assignable to the type.
func (ty BoolType) Assignable(other ExprType) bool {
switch other.(type) {
case BoolType, StringType, NullType, NumberType, AnyType:
return true
default:
return false
}
}
// Equals returns if the type is equal to the other type.
func (ty BoolType) Equals(other ExprType) bool {
switch other.(type) {
case BoolType, AnyType:
return true
default:
return false
}
}
// Fuse merges other type into this type. When other type conflicts with this type, fused result is
// any type as fallback.
func (ty BoolType) Fuse(other ExprType) ExprType {
if _, ok := other.(BoolType); ok {
return ty
}
return AnyType{}
}
// StringType is type for string values.
type StringType struct{}
func (ty StringType) String() string {
return "string"
}
// Assignable returns if other type can be assignable to the type.
func (ty StringType) Assignable(other ExprType) bool {
// Bool and null types also can be coerced into string. But in almost all case, those coercing
// would be mistakes.
switch other.(type) {
case StringType, NumberType, AnyType:
return true
default:
return false
}
}
// Equals returns if the type is equal to the other type.
func (ty StringType) Equals(other ExprType) bool {
switch other.(type) {
case StringType, AnyType:
return true
default:
return false
}
}
// Fuse merges other type into this type. When other type conflicts with this type, fused result is
// any type as fallback.
func (ty StringType) Fuse(other ExprType) ExprType {
switch other.(type) {
case StringType, NumberType:
return ty // Consider assignability
default:
return AnyType{}
}
}
// ObjectType is type for objects, which can hold key-values.
type ObjectType struct {
// Props is map from properties name to their type.
Props map[string]ExprType
// StrictProps is flag to check if the properties should be checked strictly. When this flag
// is set to true, it means that other than properties defined in Props field are not permitted
// and will cause type error. When this flag is set to false, accessing to unknown properties
// does not cause type error and will be deducted to any type.
StrictProps bool
}
// NewObjectType creates new ObjectType instance which allows unknown props. When accessing to
// unknown props, their values will fall back to any.
func NewObjectType() *ObjectType {
return &ObjectType{map[string]ExprType{}, false}
}
// NewStrictObjectType creates new ObjectType instance which does not allow unknown props.
func NewStrictObjectType() *ObjectType {
return &ObjectType{map[string]ExprType{}, true}
}
func (ty *ObjectType) String() string {
len := len(ty.Props)
if len == 0 && !ty.StrictProps {
return "object"
}
ps := make([]string, 0, len)
for n, t := range ty.Props {
ps = append(ps, fmt.Sprintf("%s: %s", n, t.String()))
}
return fmt.Sprintf("{%s}", strings.Join(ps, "; "))
}
// Assignable returns if other type can be assignable to the type.
func (ty *ObjectType) Assignable(other ExprType) bool {
switch other := other.(type) {
case AnyType:
return true
case *ObjectType:
for n, p1 := range ty.Props {
if p2, ok := other.Props[n]; ok && !p1.Assignable(p2) {
return false
}
}
return true
default:
return false
}
}
// Equals returns if the type is equal to the other type.
func (ty *ObjectType) Equals(other ExprType) bool {
switch other := other.(type) {
case AnyType:
return true
case *ObjectType:
if !ty.StrictProps || !other.StrictProps {
return true
}
for n, t := range ty.Props {
o, ok := other.Props[n]
if !ok || !t.Equals(o) {
return false
}
}
return true
default:
return false
}
}
// Fuse merges two object types into one. When other object has unknown props, they are merged into
// current object. When both have same property, when they are assignable, it remains as-is.
// Otherwise, the property falls back to any type.
// Note that this method modifies itself destructively for efficiency.
func (ty *ObjectType) Fuse(other ExprType) ExprType {
switch other := other.(type) {
case *ObjectType:
for n, ot := range other.Props {
if t, ok := ty.Props[n]; ok {
ty.Props[n] = t.Fuse(ot)
} else {
ty.Props[n] = ot // New prop
}
}
if !other.StrictProps {
ty.StrictProps = false
}
return ty
default:
return AnyType{}
}
}
// ArrayType is type for arrays.
type ArrayType struct {
// Elem is type of element of the array.
Elem ExprType
// Deref is true when this type was derived from array filter syntax (foo.*).
Deref bool
}
func (ty *ArrayType) String() string {
return fmt.Sprintf("array<%s>", ty.Elem.String())
}
// Equals returns if the type is equal to the other type.
func (ty *ArrayType) Equals(other ExprType) bool {
switch other := other.(type) {
case AnyType:
return true
case *ArrayType:
return ty.Elem.Equals(other.Elem)
default:
return false
}
}
// Assignable returns if other type can be assignable to the type.
func (ty *ArrayType) Assignable(other ExprType) bool {
switch other := other.(type) {
case AnyType:
return true
case *ArrayType:
return ty.Elem.Assignable(other.Elem)
default:
return false
}
}
// Fuse merges two object types into one. When other object has unknown props, they are merged into
// current object. When both have same property, when they are assignable, it remains as-is.
// Otherwise, the property falls back to any type.
// Note that this method modifies itself destructively for efficiency.
func (ty *ArrayType) Fuse(other ExprType) ExprType {
switch other := other.(type) {
case *ArrayType:
ty.Elem = ty.Elem.Fuse(other.Elem)
return ty
default:
return AnyType{}
}
}
// ElemTypeOf returns element type of given type when it is array type.
// When it is any type, it returns any type. Otherwise ti returns nil.
func ElemTypeOf(ty ExprType) (ExprType, bool) {
switch ty := ty.(type) {
case AnyType:
return AnyType{}, true
case *ArrayType:
return ty.Elem, true
default:
return nil, false
}
}