-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathexp.go
39 lines (34 loc) · 886 Bytes
/
mathexp.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
33
34
35
36
37
38
39
package mathexp
import (
"encoding/json"
"errors"
)
type MathExp struct {
ExpWrapper *ConditionGroupSpec
}
func New(expJson []byte) (*MathExp, error) {
var rootCondGrpSpec ConditionGroupSpec
if err := json.Unmarshal(expJson, &rootCondGrpSpec); err != nil {
return nil, err
}
if err := rootCondGrpSpec.sanititize(); err != nil {
return nil, errors.New("unable to format expressions")
}
if ok, err := rootCondGrpSpec.isValid(); !ok {
return nil, err
}
return &MathExp{ExpWrapper: &rootCondGrpSpec}, nil
}
func (mexp *MathExp) Evaluate(args map[string]interface{}) (map[string]interface{}, error) {
vars := allVars(mexp.ExpWrapper)
next := verifyBeforeEvalute(vars, args)
if !next {
return nil, ErrArgsMismatch
}
vals, err := getEffectiveValues(vars, args)
if err != nil {
return nil, err
}
outs, err := mexp.ExpWrapper.evaluate(vals)
return outs, err
}