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) {
1077
1077
"baz" : types .String ,
1078
1078
},
1079
1079
},
1080
+ "arr" : types .Array (types.StrictMap {
1081
+ "value" : types .String ,
1082
+ }),
1080
1083
}
1081
1084
1082
1085
noerr := "no error"
@@ -1090,6 +1093,8 @@ func TestCheck_types(t *testing.T) {
1090
1093
{`foo.bar.unknown` , noerr },
1091
1094
{`[foo] | map(.unknown)` , `unknown field unknown` },
1092
1095
{`[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)` },
1093
1098
}
1094
1099
1095
1100
for _ , test := range tests {
Original file line number Diff line number Diff line change 28
28
Nil = nilType {}
29
29
)
30
30
31
+ // Type is a type that can be used to represent a value.
31
32
type Type interface {
32
33
Nature () Nature
33
34
}
@@ -46,6 +47,8 @@ func (r rtype) Nature() Nature {
46
47
return Nature {Type : r .t }
47
48
}
48
49
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.
49
52
type Map map [string ]Type
50
53
51
54
func (m Map ) Nature () Nature {
@@ -59,6 +62,8 @@ func (m Map) Nature() Nature {
59
62
return nt
60
63
}
61
64
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.
62
67
type StrictMap map [string ]Type
63
68
64
69
func (m StrictMap ) Nature () Nature {
@@ -72,3 +77,21 @@ func (m StrictMap) Nature() Nature {
72
77
}
73
78
return nt
74
79
}
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