Skip to content

Commit

Permalink
Add types.Array()
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jun 5, 2024
1 parent ac6e5a3 commit 55f453b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,9 @@ func TestCheck_types(t *testing.T) {
"baz": types.String,
},
},
"arr": types.Array(types.StrictMap{
"value": types.String,
}),
}

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

for _, test := range tests {
Expand Down
23 changes: 23 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
Nil = nilType{}
)

// Type is a type that can be used to represent a value.
type Type interface {
Nature() Nature
}
Expand All @@ -46,6 +47,8 @@ func (r rtype) Nature() Nature {
return Nature{Type: r.t}
}

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

func (m Map) Nature() Nature {
Expand All @@ -59,6 +62,8 @@ func (m Map) Nature() Nature {
return nt
}

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

func (m StrictMap) Nature() Nature {
Expand All @@ -72,3 +77,21 @@ func (m StrictMap) Nature() Nature {
}
return nt
}

// Array returns a type that represents an array of the given type.
func Array(of Type) Type {
return array{of}
}

type array struct {
of Type
}

func (a array) Nature() Nature {
of := a.of.Nature()
return Nature{
Type: reflect.TypeOf([]any{}),
Fields: make(map[string]Nature, 1),
ArrayOf: &of,
}
}

0 comments on commit 55f453b

Please sign in to comment.