|
| 1 | +// ================================================================= |
| 2 | +// |
| 3 | +// Copyright (C) 2018 Spatial Current, Inc. - All Rights Reserved |
| 4 | +// Released as open source under the MIT License. See LICENSE file. |
| 5 | +// |
| 6 | +// ================================================================= |
| 7 | + |
| 8 | +// DFLJS is the Javascript version of DFL. |
| 9 | +// |
| 10 | +// Usage |
| 11 | +// |
| 12 | +// In you html document simply add dfl as a script and call dfl.EvaluateBool(expression, {"a": 1}); |
| 13 | +// |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "github.com/spatialcurrent/go-dfl/dfl" |
| 18 | +) |
| 19 | + |
| 20 | +import ( |
| 21 | + "github.com/gopherjs/gopherjs/js" |
| 22 | + "honnef.co/go/js/console" |
| 23 | +) |
| 24 | + |
| 25 | +func main() { |
| 26 | + js.Global.Set("dfl", map[string]interface{}{ |
| 27 | + "EvaluateBool": EvaluateBool, |
| 28 | + "EvaluateInt": EvaluateInt, |
| 29 | + "EvaluateString": EvaluateString, |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +func EvaluateBool(s string, options *js.Object) bool { |
| 34 | + root, err := dfl.Parse(s) |
| 35 | + if err != nil { |
| 36 | + console.Log(err.Error()) |
| 37 | + return false |
| 38 | + } |
| 39 | + |
| 40 | + root = root.Compile() |
| 41 | + |
| 42 | + ctx := map[string]interface{}{} |
| 43 | + for _, key := range js.Keys(options) { |
| 44 | + ctx[key] = options.Get(key).Interface() |
| 45 | + } |
| 46 | + |
| 47 | + result, err := dfl.EvaluateBool(root, ctx, dfl.FunctionMap{}) |
| 48 | + if err != nil { |
| 49 | + console.Log(err.Error()) |
| 50 | + return false |
| 51 | + } |
| 52 | + |
| 53 | + return result |
| 54 | +} |
| 55 | + |
| 56 | +func EvaluateInt(s string, options *js.Object) int { |
| 57 | + root, err := dfl.Parse(s) |
| 58 | + if err != nil { |
| 59 | + console.Log(err.Error()) |
| 60 | + return 0 |
| 61 | + } |
| 62 | + |
| 63 | + root = root.Compile() |
| 64 | + |
| 65 | + ctx := map[string]interface{}{} |
| 66 | + for _, key := range js.Keys(options) { |
| 67 | + ctx[key] = options.Get(key).Interface() |
| 68 | + } |
| 69 | + |
| 70 | + result, err := dfl.EvaluateInt(root, ctx, dfl.FunctionMap{}) |
| 71 | + if err != nil { |
| 72 | + console.Log(err.Error()) |
| 73 | + return 0 |
| 74 | + } |
| 75 | + |
| 76 | + return result |
| 77 | +} |
| 78 | + |
| 79 | +func EvaluateString(s string, options *js.Object) string { |
| 80 | + root, err := dfl.Parse(s) |
| 81 | + if err != nil { |
| 82 | + console.Log(err.Error()) |
| 83 | + return "" |
| 84 | + } |
| 85 | + |
| 86 | + root = root.Compile() |
| 87 | + |
| 88 | + ctx := map[string]interface{}{} |
| 89 | + for _, key := range js.Keys(options) { |
| 90 | + ctx[key] = options.Get(key).Interface() |
| 91 | + } |
| 92 | + |
| 93 | + result, err := dfl.EvaluateString(root, ctx, dfl.FunctionMap{}) |
| 94 | + if err != nil { |
| 95 | + console.Log(err.Error()) |
| 96 | + return "" |
| 97 | + } |
| 98 | + |
| 99 | + return result |
| 100 | +} |
0 commit comments