Skip to content

Commit 993aeba

Browse files
committed
EvaluateBool usability function
1 parent 8cb700e commit 993aeba

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

dfl/EvaluateBool.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package dfl
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
)
7+
8+
import (
9+
"github.com/pkg/errors"
10+
)
11+
12+
// EvaluateBool returns the boolean value of a node given a context. If the result is not a bool, then returns an error.
13+
func EvaluateBool(n Node, ctx Context, funcs FunctionMap) (bool, error) {
14+
result, err := n.Evaluate(ctx, funcs)
15+
if err != nil {
16+
return false, errors.Wrap(err, "Error evaluating expression")
17+
}
18+
19+
switch result.(type) {
20+
case bool:
21+
return result.(bool), nil
22+
}
23+
24+
return false, errors.New("Evaluation returned a "+fmt.Sprint(reflect.TypeOf(result))+" instead of bool")
25+
}

0 commit comments

Comments
 (0)