99//
1010// Usage
1111//
12- // In you html document simply add dfl as a script and call dfl.EvaluateBool(expression, {"a": 1});
12+ // In you html document, the simplest workflow is to add dfl as a script and call dfl.EvaluateBool(expression, {"a": 1});
13+ // For performance reasons, you can compile your expression as below:
14+ // var exp = "@pop + 10";
15+ // var root = dfl.Parse(exp).Compile();
16+ // var result = root.Evaluate({"pop": 10})
1317//
1418package main
1519
@@ -24,15 +28,51 @@ import (
2428
2529var GO_DFL_VERSION = "0.0.2"
2630
31+ type Node struct {
32+ Node dfl.Node
33+ }
34+
35+ func (n Node ) Compile () * js.Object {
36+ return js .MakeWrapper (Node {Node :n .Node .Compile ()})
37+ }
38+
39+ func (n Node ) Evaluate (options * js.Object ) interface {} {
40+
41+ ctx := map [string ]interface {}{}
42+ for _ , key := range js .Keys (options ) {
43+ ctx [key ] = options .Get (key ).Interface ()
44+ }
45+
46+ result , err := n .Node .Evaluate (ctx , dfl.FunctionMap {})
47+ if err != nil {
48+ console .Log (err .Error ())
49+ return false
50+ }
51+ return result
52+ }
53+
2754func main () {
2855 js .Global .Set ("dfl" , map [string ]interface {}{
29- "Version" : GO_DFL_VERSION ,
56+ "version" : GO_DFL_VERSION ,
57+ "Parse" : Parse ,
3058 "EvaluateBool" : EvaluateBool ,
3159 "EvaluateInt" : EvaluateInt ,
60+ "EvaluateFloat" : EvaluateFloat64 ,
3261 "EvaluateString" : EvaluateString ,
3362 })
3463}
3564
65+ func Parse (s string ) * js.Object {
66+ root , err := dfl .Parse (s )
67+ if err != nil {
68+ console .Log (err .Error ())
69+ return js .MakeWrapper (Node {Node : nil })
70+ }
71+ return js .MakeWrapper (Node {Node :root })
72+ }
73+
74+
75+
3676func EvaluateBool (s string , options * js.Object ) bool {
3777 root , err := dfl .Parse (s )
3878 if err != nil {
@@ -79,6 +119,29 @@ func EvaluateInt(s string, options *js.Object) int {
79119 return result
80120}
81121
122+ func EvaluateFloat64 (s string , options * js.Object ) float64 {
123+ root , err := dfl .Parse (s )
124+ if err != nil {
125+ console .Log (err .Error ())
126+ return 0.0
127+ }
128+
129+ root = root .Compile ()
130+
131+ ctx := map [string ]interface {}{}
132+ for _ , key := range js .Keys (options ) {
133+ ctx [key ] = options .Get (key ).Interface ()
134+ }
135+
136+ result , err := dfl .EvaluateFloat64 (root , ctx , dfl.FunctionMap {})
137+ if err != nil {
138+ console .Log (err .Error ())
139+ return 0.0
140+ }
141+
142+ return result
143+ }
144+
82145func EvaluateString (s string , options * js.Object ) string {
83146 root , err := dfl .Parse (s )
84147 if err != nil {
0 commit comments