Skip to content

Commit f9dd158

Browse files
committed
javascript improvement; spelling check
1 parent b7e7095 commit f9dd158

16 files changed

Lines changed: 144 additions & 22 deletions

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ matrix:
1515
- go: tip
1616
fast_finish: true
1717

18+
install:
19+
- go get -u github.com/alecthomas/gometalinter
20+
- gometalinter --install
21+
1822
script:
23+
- gometalinter --misspell-locale=US --disable-all --enable=misspell --enable=vet ./dfl/
24+
- gometalinter --misspell-locale=US --disable-all --enable=misspell --enable=vet ./cmd/dfl/
25+
- gometalinter --misspell-locale=US --disable-all --enable=misspell --enable=vet ./cmd/dfljs/
1926
- cd cmd/dfl
2027
- go fmt
2128
- go test

README.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@
88

99
# Usage
1010

11-
You can import **go-dfl** as a library with:
12-
13-
```
14-
import (
15-
"github.com/spatialcurrent/go-dfl/dfl"
16-
)
17-
```
11+
**CLI**
1812

1913
You can also use the command line tool.
2014

@@ -33,6 +27,35 @@ Options:
3327
Prints version to stdout
3428
```
3529

30+
**Go**
31+
32+
You can import **go-dfl** as a library with:
33+
34+
```
35+
import (
36+
"github.com/spatialcurrent/go-dfl/dfl"
37+
)
38+
```
39+
40+
**JavaScript**
41+
42+
```html
43+
<html>
44+
<head>
45+
<script src="https://...dfljs.js"></script>
46+
</head>
47+
<body>
48+
<script>
49+
var exp = "@pop + 10";
50+
var root = dfl.Parse(exp).Compile();
51+
var result = root.Evaluate({"pop": 10})
52+
...
53+
</script>
54+
</body>
55+
</html>
56+
```
57+
58+
3659
# Examples:
3760

3861
**Environment**

cmd/dfl/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func main() {
127127
fmt.Println("******************* Parsed *******************")
128128
out, err := yaml.Marshal(root.Map())
129129
if err != nil {
130-
fmt.Println("Error marshalling expression to yaml.")
130+
fmt.Println("Error marshaling expression to yaml.")
131131
fmt.Println(err)
132132
os.Exit(1)
133133
}
@@ -140,7 +140,7 @@ func main() {
140140
fmt.Println("******************* Compiled *******************")
141141
out, err := yaml.Marshal(root.Map())
142142
if err != nil {
143-
fmt.Println("Error marshalling expression to yaml.")
143+
fmt.Println("Error marshaling expression to yaml.")
144144
fmt.Println(err)
145145
os.Exit(1)
146146
}

cmd/dfljs/main.go

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
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
//
1418
package main
1519

@@ -24,15 +28,51 @@ import (
2428

2529
var 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+
2754
func 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+
3676
func 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+
82145
func EvaluateString(s string, options *js.Object) string {
83146
root, err := dfl.Parse(s)
84147
if err != nil {

dfl/And.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,5 @@ func (a And) Evaluate(ctx Context, funcs FunctionMap) (interface{}, error) {
6969
return rv.(bool), nil
7070
}
7171
}
72-
return false, errors.New("Error evaulating expression " + a.Dfl())
72+
return false, errors.New("Error evaluating expression " + a.Dfl())
7373
}

dfl/Context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
package dfl
99

10-
// Context is a simple alias for a map[string]interface{} that is used for containg the context for evaluating a DFL Node.
10+
// Context is a simple alias for a map[string]interface{} that is used for containing the context for evaluating a DFL Node.
1111
// The values in a context are essentially the input parameters for a DFL expression and match up with the Attribute.
1212
// The Context is built from the trailing command line arguments. For example the arguments from the following command line
1313
//

dfl/EvaluateFloat64.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dfl
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
)
7+
8+
import (
9+
"github.com/pkg/errors"
10+
)
11+
12+
// EvaluateFloat64 returns the float64 value of a node given a context. If the result is not a float64, then returns an error.
13+
func EvaluateFloat64(n Node, ctx Context, funcs FunctionMap) (float64, error) {
14+
result, err := n.Evaluate(ctx, funcs)
15+
if err != nil {
16+
return 0.0, errors.Wrap(err, "Error evaluating expression")
17+
}
18+
19+
switch result.(type) {
20+
case int:
21+
return float64(result.(int)), nil
22+
case float64:
23+
return result.(float64), nil
24+
}
25+
26+
return 0.0, errors.New("Evaluation returned a " + fmt.Sprint(reflect.TypeOf(result)) + " instead of int")
27+
}

dfl/EvaluateInt.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ func EvaluateInt(n Node, ctx Context, funcs FunctionMap) (int, error) {
1919
switch result.(type) {
2020
case int:
2121
return result.(int), nil
22+
case float64:
23+
return int(result.(float64)), nil
2224
}
2325

2426
return 0, errors.New("Evaluation returned a " + fmt.Sprint(reflect.TypeOf(result)) + " instead of int")

dfl/ILike.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (i ILike) Evaluate(ctx Context, funcs FunctionMap) (interface{}, error) {
6262

6363
pattern, err := regexp.Compile("^" + strings.Replace(rvs, "%", ".*", -1) + "$")
6464
if err != nil {
65-
return false, errors.Wrap(err, "Error evaulating expression "+i.Dfl())
65+
return false, errors.Wrap(err, "Error evaluating expression "+i.Dfl())
6666
}
6767
return pattern.MatchString(lvs), nil
6868
}

dfl/In.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ func (i In) Compile() Node {
4848
func (i In) Evaluate(ctx Context, funcs FunctionMap) (interface{}, error) {
4949
lv, err := i.Left.Evaluate(ctx, funcs)
5050
if err != nil {
51-
return false, errors.Wrap(err, "Error evaulating expression "+i.Dfl())
51+
return false, errors.Wrap(err, "Error evaluating expression "+i.Dfl())
5252
}
5353

5454
rv, err := i.Right.Evaluate(ctx, funcs)
5555
if err != nil {
56-
return false, errors.Wrap(err, "Error evaulating expression "+i.Dfl())
56+
return false, errors.Wrap(err, "Error evaluating expression "+i.Dfl())
5757
}
5858

5959
switch lv.(type) {

0 commit comments

Comments
 (0)