-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvaluateInt.go
32 lines (26 loc) · 1 KB
/
EvaluateInt.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
31
32
// =================================================================
//
// 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"
)
// EvaluateInt returns the int value of a node given a context. If the result is not an int, then returns an error.
func EvaluateInt(n Node, vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, int, error) {
vars, result, err := n.Evaluate(vars, ctx, funcs, quotes)
if err != nil {
return vars, 0, errors.Wrap(err, "Error evaluating expression "+n.Dfl(quotes, false, 0))
}
switch result.(type) {
case int:
return vars, result.(int), nil
case float64:
return vars, int(result.(float64)), nil
}
return vars, 0, errors.New("Evaluation returned a " + fmt.Sprint(reflect.TypeOf(result)) + " instead of int")
}