forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr_type.go
366 lines (318 loc) · 9.31 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
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
// Merge merges other type into this type. When other type conflicts with this type, the merged
// result is any type as fallback.
Merge(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
}
// Merge merges other type into this type. When other type conflicts with this type, the merged
// result is any type as fallback.
func (ty AnyType) Merge(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
}
}
// Merge merges other type into this type. When other type conflicts with this type, the merged
// result is any type as fallback.
func (ty NullType) Merge(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"
}
// 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
}
}
// Merge merges other type into this type. When other type conflicts with this type, the merged
// result is any type as fallback.
func (ty NumberType) Merge(other ExprType) ExprType {
switch other.(type) {
case NumberType:
return ty
case StringType:
return other
default:
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 {
// Any type can be converted into bool..
// e.g.
// if: ${{ steps.foo }}
return true
}
// Merge merges other type into this type. When other type conflicts with this type, the merged
// result is any type as fallback.
func (ty BoolType) Merge(other ExprType) ExprType {
switch other.(type) {
case BoolType:
return ty
case StringType:
return other
default:
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
}
}
// Merge merges other type into this type. When other type conflicts with this type, the merged
// result is any type as fallback.
func (ty StringType) Merge(other ExprType) ExprType {
switch other.(type) {
case StringType, NumberType, BoolType:
return ty
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
// Mapped is an element type of this object. This means all props have the type. For example,
// The element type of env context is string.
// AnyType means its property types can be any type so it shapes a loose object. Setting nil
// means propreties are mapped to no type so it shapes a strict object.
//
// Invariant: All types in Props field must be assignable to this type.
Mapped ExprType
}
// NewEmptyObjectType creates new loose ObjectType instance which allows unknown props. When
// accessing to unknown props, their values will fall back to any.
func NewEmptyObjectType() *ObjectType {
return &ObjectType{map[string]ExprType{}, AnyType{}}
}
// NewObjectType creates new loose ObjectType instance which allows unknown props with given props.
func NewObjectType(props map[string]ExprType) *ObjectType {
return &ObjectType{props, AnyType{}}
}
// NewEmptyStrictObjectType creates new ObjectType instance which does not allow unknown props.
func NewEmptyStrictObjectType() *ObjectType {
return &ObjectType{map[string]ExprType{}, nil}
}
// NewStrictObjectType creates new ObjectType instance which does not allow unknown props with
// given prop types.
func NewStrictObjectType(props map[string]ExprType) *ObjectType {
return &ObjectType{props, nil}
}
// NewMapObjectType creates new ObjectType which maps keys to a specific type value.
func NewMapObjectType(t ExprType) *ObjectType {
return &ObjectType{nil, t}
}
// IsStrict returns if the type is a strict object, which means no unknown prop is allowed.
func (ty *ObjectType) IsStrict() bool {
return ty.Mapped == nil
}
// IsLoose returns if the type is a loose object, which allows any unknown props.
func (ty *ObjectType) IsLoose() bool {
_, ok := ty.Mapped.(AnyType)
return ok
}
// Strict sets the object is strict, which means only known properties are allowed.
func (ty *ObjectType) Strict() {
ty.Mapped = nil
}
// Loose sets the object is loose, which means any properties can be set.
func (ty *ObjectType) Loose() {
ty.Mapped = AnyType{}
}
func (ty *ObjectType) String() string {
if !ty.IsStrict() {
if ty.IsLoose() {
return "object"
}
return fmt.Sprintf("{string => %s}", ty.Mapped.String())
}
ps := make([]string, 0, len(ty.Props))
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.
// In other words, rhs type is more strict than lhs (receiver) type.
func (ty *ObjectType) Assignable(other ExprType) bool {
switch other := other.(type) {
case AnyType:
return true
case *ObjectType:
if !ty.IsStrict() {
if !other.IsStrict() {
return ty.Mapped.Assignable(other.Mapped)
}
for _, t := range other.Props {
if !ty.Mapped.Assignable(t) {
return false
}
}
return true
}
// ty is strict
if !other.IsStrict() {
for _, t := range ty.Props {
if !t.Assignable(other.Mapped) {
return false
}
}
return true
}
// ty and other are strict
for n, r := range other.Props {
if l, ok := ty.Props[n]; !ok || !l.Assignable(r) {
return false
}
}
return true
default:
return false
}
}
// Merge 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.
func (ty *ObjectType) Merge(other ExprType) ExprType {
switch other := other.(type) {
case *ObjectType:
// Shortcuts
if len(ty.Props) == 0 && other.IsLoose() {
return other
}
if len(other.Props) == 0 && ty.IsLoose() {
return ty
}
mapped := ty.Mapped
if mapped == nil {
mapped = other.Mapped
} else if other.Mapped != nil {
mapped = mapped.Merge(other.Mapped)
}
props := make(map[string]ExprType, len(ty.Props))
for n, l := range ty.Props {
props[n] = l
}
for n, r := range other.Props {
if l, ok := props[n]; ok {
props[n] = l.Merge(r)
} else {
props[n] = r
if mapped != nil {
mapped = mapped.Merge(r)
}
}
}
return &ObjectType{
Props: props,
Mapped: mapped,
}
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())
}
// 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
}
}
// Merge 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.
func (ty *ArrayType) Merge(other ExprType) ExprType {
switch other := other.(type) {
case *ArrayType:
if _, ok := ty.Elem.(AnyType); ok {
return ty
}
if _, ok := other.Elem.(AnyType); ok {
return other
}
return &ArrayType{
Elem: ty.Elem.Merge(other.Elem),
Deref: false, // When fusing array deref type, it means prop deref chain breaks
}
default:
return AnyType{}
}
}
// EqualTypes returns if the two types are equal.
func EqualTypes(l, r ExprType) bool {
return l.Assignable(r) && r.Assignable(l)
}