Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent filter and array from needlessly weakening types #598

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prevent filter and array from needlessly weakening types
  • Loading branch information
SamCymbaluk committed Mar 12, 2024
commit 75ff3a1de675b3b42a77a39233e9830a4ec050ce
10 changes: 5 additions & 5 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,9 @@ func (v *checker) BuiltinNode(node *ast.BuiltinNode) (reflect.Type, info) {
return v.error(node.Arguments[1], "predicate should return boolean (got %v)", closure.Out(0).String())
}
if isAny(collection) {
return arrayType, info{}
return anyArrayType, info{}
}
return arrayType, info{}
return collection, info{}
}
return v.error(node.Arguments[1], "predicate should has one input and one output param")

Expand All @@ -643,7 +643,7 @@ func (v *checker) BuiltinNode(node *ast.BuiltinNode) (reflect.Type, info) {
closure.NumOut() == 1 &&
closure.NumIn() == 1 && isAny(closure.In(0)) {

return arrayType, info{}
return anyArrayType, info{}
}
return v.error(node.Arguments[1], "predicate should has one input and one output param")

Expand Down Expand Up @@ -1122,9 +1122,9 @@ func (v *checker) ArrayNode(node *ast.ArrayNode) (reflect.Type, info) {
prev = curr
}
if allElementsAreSameType && prev != nil {
return arrayType, info{elem: prev}
return arrayType(prev), info{elem: prev}
}
return arrayType, info{}
return anyArrayType, info{}
}

func (v *checker) MapNode(node *ast.MapNode) (reflect.Type, info) {
Expand Down
18 changes: 18 additions & 0 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,3 +974,21 @@ func TestCheck_builtin_without_call(t *testing.T) {
})
}
}

func TestCheck_types(t *testing.T) {
tests := []struct {
input string
nodeType reflect.Type
}{
{`filter([1,2,3], # > 1)`, reflect.TypeOf([]int{})},
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
tree, err := parser.Parse(test.input)
require.NoError(t, err)

nodeType, err := checker.Check(tree, conf.New(nil))
require.Equal(t, test.nodeType.String(), nodeType.String())
})
}
}
6 changes: 5 additions & 1 deletion checker/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ var (
integerType = reflect.TypeOf(0)
floatType = reflect.TypeOf(float64(0))
stringType = reflect.TypeOf("")
arrayType = reflect.TypeOf([]any{})
anyArrayType = reflect.TypeOf([]any{})
mapType = reflect.TypeOf(map[string]any{})
anyType = reflect.TypeOf(new(any)).Elem()
timeType = reflect.TypeOf(time.Time{})
durationType = reflect.TypeOf(time.Duration(0))
)

func arrayType(t reflect.Type) reflect.Type {
return reflect.SliceOf(t)
}

func combined(a, b reflect.Type) reflect.Type {
if a.Kind() == b.Kind() {
return a
Expand Down
Loading