Skip to content

Commit 34283a1

Browse files
authored
Merge pull request #19 from spatialcurrent/javascript
GopherJS Build of DFL (Closes #18)
2 parents 54ec72f + af124e1 commit 34283a1

5 files changed

Lines changed: 130 additions & 1 deletion

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ You can also use DFL to filter OpenStreetMap features.
7676
# returns true as exit code 0
7777
```
7878

79+
# Building
80+
81+
**CLI**
82+
83+
The command line DFL program can be built with the `scripts/build_cli.sh` script.
84+
85+
**JavaScript**
86+
87+
You can compile DFL to pure JavaScript with the `scripts/build_javascript.sh` script.
88+
7989
# Contributing
8090

8191
[Spatial Current, Inc.](https://spatialcurrent.io) is currently accepting pull requests for this repository. We'd love to have your contributions! Please see [Contributing.md](https://github.com/spatialcurrent/go-dfl/blob/master/CONTRIBUTING.md) for how to get started.

cmd/dfljs/main.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

dfl/TryConvertString_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ func TestTryConvertStringTimes(t *testing.T) {
6565

6666
}
6767

68-
6968
func TestTryConvertStringIPv4(t *testing.T) {
7069

7170
testCases := []struct {
File renamed without changes.

scripts/build_javascript.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
4+
5+
mkdir -p $DIR/../bin
6+
7+
echo "******************"
8+
echo "Formatting $(realpath $DIR/../dfl)"
9+
cd $DIR/../dfl
10+
go fmt
11+
echo "Done formatting."
12+
echo "******************"
13+
echo "Building Javascript for DFL"
14+
cd $DIR/../bin
15+
gopherjs build github.com/spatialcurrent/go-dfl/cmd/dfljs
16+
if [[ "$?" != 0 ]] ; then
17+
echo "Error building Javascript for DFL"
18+
exit 1
19+
fi
20+
echo "Executable built at $(realpath $DIR/../bin)"

0 commit comments

Comments
 (0)