File tree Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff line change 2828 Nil = nilType {}
2929)
3030
31+ // Type is a type that can be used to represent a value.
3132type 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.
4952type Map map [string ]Type
5053
5154func (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.
6267type StrictMap map [string ]Type
6368
6469func (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+ }
You can’t perform that action at this time.
0 commit comments