-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvaluateBool.go
30 lines (24 loc) · 986 Bytes
/
EvaluateBool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
import (
"fmt"
"reflect"
"github.com/pkg/errors"
)
// EvaluateBool returns the boolean value of a node given a context. If the result is not a bool, then returns an error.
func EvaluateBool(n Node, vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, bool, error) {
vars, result, err := n.Evaluate(vars, ctx, funcs, quotes)
if err != nil {
return vars, false, errors.Wrap(err, "Error evaluating expression "+n.Dfl(quotes, false, 0))
}
switch result.(type) {
case bool:
return vars, result.(bool), nil
}
return vars, false, errors.New("Evaluation returned a " + fmt.Sprint(reflect.TypeOf(result)) + " instead of bool")
}