Skip to content

Commit

Permalink
Fix if statement non-boolean values
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffail committed Jan 23, 2024
1 parent f45abbf commit 12ab101
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file.
### Fixed

- The `javascript` processor now handles module imports correctly.
- Bloblang `if` statements now provide explicit errors when query expressions resolve to non-boolean values.

### Changed

Expand Down
13 changes: 11 additions & 2 deletions internal/bloblang/query/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ func NewIfFunction(queryFn, ifFn Function, elseIfs []ElseIf, elseFn Function) Fu
if err != nil {
return nil, fmt.Errorf("failed to check if condition: %w", err)
}
if queryRes, _ := queryVal.(bool); queryRes {

queryRes, isBool := queryVal.(bool)
if !isBool {
return nil, fmt.Errorf("%v resolved to a non-boolean value %v (%T)", queryFn.Annotation(), queryVal, queryVal)
}
if queryRes {
return ifFn.Exec(ctx)
}

Expand All @@ -100,7 +105,11 @@ func NewIfFunction(queryFn, ifFn Function, elseIfs []ElseIf, elseFn Function) Fu
if err != nil {
return nil, fmt.Errorf("failed to check if condition %v: %w", i+1, err)
}
if queryRes, _ := queryVal.(bool); queryRes {
queryRes, isBool := queryVal.(bool)
if !isBool {
return nil, fmt.Errorf("%v resolved to a non-boolean value %v (%T)", eFn.QueryFn.Annotation(), queryVal, queryVal)
}
if queryRes {
return eFn.MapFn.Exec(ctx)
}
}
Expand Down
33 changes: 21 additions & 12 deletions internal/bloblang/query/expression_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package query

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -24,12 +23,12 @@ func TestExpressions(t *testing.T) {
}

tests := map[string]struct {
input Function
value *any
output any
err error
messages []easyMsg
index int
input Function
value *any
output any
errContains string
messages []easyMsg
index int
}{
"if false": {
input: NewIfFunction(
Expand Down Expand Up @@ -107,22 +106,22 @@ func TestExpressions(t *testing.T) {
nil,
NewLiteralFunction("", "bar"),
),
err: errors.New("failed to check if condition: variables were undefined"),
errContains: "failed to check if condition: variables were undefined",
},
"match context fails": {
input: NewMatchFunction(
NewVarFunction("doesnt exist"),
NewMatchCase(NewLiteralFunction("", true), NewLiteralFunction("", "foo")),
),
err: errors.New("variables were undefined"),
errContains: "variables were undefined",
},
"match first case fails": {
input: NewMatchFunction(
NewLiteralFunction("", "context"),
NewMatchCase(NewVarFunction("doesnt exist"), NewLiteralFunction("", "foo")),
NewMatchCase(NewLiteralFunction("", true), NewLiteralFunction("", "bar")),
),
err: errors.New("failed to check match case 0: variables were undefined"),
errContains: "failed to check match case 0: variables were undefined",
},
"match second case fails": {
input: NewMatchFunction(
Expand Down Expand Up @@ -227,6 +226,15 @@ func TestExpressions(t *testing.T) {
}(),
output: []any{23},
},
"non-boolean literal": {
input: NewIfFunction(
NewLiteralFunction("", "hello world"),
NewLiteralFunction("", "foo"),
nil,
NewLiteralFunction("", "buz"),
),
errContains: "string literal resolved to a non-boolean value hello world",
},
}

for name, test := range tests {
Expand All @@ -251,8 +259,9 @@ func TestExpressions(t *testing.T) {
Index: test.index,
MsgBatch: msg,
}.WithValueFunc(func() *any { return test.value }))
if test.err != nil {
require.EqualError(t, err, test.err.Error())
if test.errContains != "" {
require.Error(t, err)
require.Contains(t, err.Error(), test.errContains)
} else {
require.NoError(t, err)
}
Expand Down

0 comments on commit 12ab101

Please sign in to comment.