Skip to content

Commit 55f453b

Browse files
committed
Add types.Array()
1 parent ac6e5a3 commit 55f453b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

checker/checker_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,9 @@ func TestCheck_types(t *testing.T) {
10771077
"baz": types.String,
10781078
},
10791079
},
1080+
"arr": types.Array(types.StrictMap{
1081+
"value": types.String,
1082+
}),
10801083
}
10811084

10821085
noerr := "no error"
@@ -1090,6 +1093,8 @@ func TestCheck_types(t *testing.T) {
10901093
{`foo.bar.unknown`, noerr},
10911094
{`[foo] | map(.unknown)`, `unknown field unknown`},
10921095
{`[foo] | map(.bar) | filter(.baz)`, `predicate should return boolean (got string)`},
1096+
{`arr | filter(.value > 0)`, `invalid operation: > (mismatched types string and int)`},
1097+
{`arr | filter(.value contains "a") | filter(.value == 0)`, `invalid operation: == (mismatched types string and int)`},
10931098
}
10941099

10951100
for _, test := range tests {

types/types.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var (
2828
Nil = nilType{}
2929
)
3030

31+
// Type is a type that can be used to represent a value.
3132
type Type interface {
3233
Nature() Nature
3334
}
@@ -46,6 +47,8 @@ func (r rtype) Nature() Nature {
4647
return Nature{Type: r.t}
4748
}
4849

50+
// Map returns a type that represents a map of the given type.
51+
// The map is not strict, meaning that it can contain keys not defined in the map.
4952
type Map map[string]Type
5053

5154
func (m Map) Nature() Nature {
@@ -59,6 +62,8 @@ func (m Map) Nature() Nature {
5962
return nt
6063
}
6164

65+
// StrictMap returns a type that represents a map of the given type.
66+
// The map is strict, meaning that it can only contain keys defined in the map.
6267
type StrictMap map[string]Type
6368

6469
func (m StrictMap) Nature() Nature {
@@ -72,3 +77,21 @@ func (m StrictMap) Nature() Nature {
7277
}
7378
return nt
7479
}
80+
81+
// Array returns a type that represents an array of the given type.
82+
func Array(of Type) Type {
83+
return array{of}
84+
}
85+
86+
type array struct {
87+
of Type
88+
}
89+
90+
func (a array) Nature() Nature {
91+
of := a.of.Nature()
92+
return Nature{
93+
Type: reflect.TypeOf([]any{}),
94+
Fields: make(map[string]Nature, 1),
95+
ArrayOf: &of,
96+
}
97+
}

0 commit comments

Comments
 (0)