Skip to content

Commit 5afc87e

Browse files
committed
refractor to better support javascript
1 parent b2b095c commit 5afc87e

10 files changed

Lines changed: 257 additions & 147 deletions

File tree

.travis.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,18 @@ install:
2727

2828
script:
2929
- gometalinter --misspell-locale=US --disable-all --enable=misspell --enable=vet ./dfl/
30+
- gometalinter --misspell-locale=US --disable-all --enable=misspell --enable=vet ./dfljs/
3031
- gometalinter --misspell-locale=US --disable-all --enable=misspell --enable=vet ./plugins/dfl/
3132
- gometalinter --misspell-locale=US --disable-all --enable=misspell --enable=vet ./cmd/dfl/
3233
- gometalinter --misspell-locale=US --disable-all --enable=misspell --enable=vet ./cmd/dfl.js/
33-
- cd cmd/dfl
34+
- cd dfl
3435
- go fmt
35-
- go build
3636
- go test
37+
- cd ../dfljs
38+
- go fmt
39+
- cd ../cmd/dfl
40+
- go fmt
41+
- go build
42+
- cd ../../cmd/dfljs
43+
- go fmt
44+
- go build

cmd/dfl.js/main.go

Lines changed: 7 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -17,156 +17,19 @@
1717
//
1818
package main
1919

20-
import (
21-
"github.com/spatialcurrent/go-dfl/dfl"
22-
)
23-
2420
import (
2521
"github.com/gopherjs/gopherjs/js"
26-
"honnef.co/go/js/console"
22+
"github.com/spatialcurrent/go-dfl/dfl"
23+
"github.com/spatialcurrent/go-dfl/dfljs"
2724
)
2825

29-
var GO_DFL_DEFAULT_QUOTES = []string{"\"", "'", "`"}
30-
31-
type Node struct {
32-
Node dfl.Node
33-
FunctionMap dfl.FunctionMap
34-
}
35-
36-
func (n Node) Pretty() string {
37-
return n.Node.Dfl(GO_DFL_DEFAULT_QUOTES[1:], true, 0)
38-
}
39-
40-
func (n Node) Compile() *js.Object {
41-
return js.MakeWrapper(Node{
42-
Node: n.Node.Compile(),
43-
FunctionMap: dfl.NewFuntionMapWithDefaults(),
44-
})
45-
}
46-
47-
func (n Node) Evaluate(options *js.Object) interface{} {
48-
49-
ctx := map[string]interface{}{}
50-
for _, key := range js.Keys(options) {
51-
ctx[key] = options.Get(key).Interface()
52-
}
53-
54-
result, err := n.Node.Evaluate(ctx, n.FunctionMap, GO_DFL_DEFAULT_QUOTES[1:])
55-
if err != nil {
56-
console.Error(err.Error())
57-
return false
58-
}
59-
return result
60-
}
61-
6226
func main() {
6327
js.Global.Set("dfl", map[string]interface{}{
6428
"version": dfl.VERSION,
65-
"Parse": Parse,
66-
"EvaluateBool": EvaluateBool,
67-
"EvaluateInt": EvaluateInt,
68-
"EvaluateFloat": EvaluateFloat64,
69-
"EvaluateString": EvaluateString,
29+
"Parse": dfljs.Parse,
30+
"EvaluateBool": dfljs.EvaluateBool,
31+
"EvaluateInt": dfljs.EvaluateInt,
32+
"EvaluateFloat": dfljs.EvaluateFloat64,
33+
"EvaluateString": dfljs.EvaluateString,
7034
})
7135
}
72-
73-
func Parse(s string) *js.Object {
74-
root, err := dfl.Parse(s)
75-
if err != nil {
76-
console.Log(err.Error())
77-
return js.MakeWrapper(Node{Node: nil})
78-
}
79-
return js.MakeWrapper(Node{Node: root})
80-
}
81-
82-
func EvaluateBool(s string, options *js.Object) bool {
83-
root, err := dfl.Parse(s)
84-
if err != nil {
85-
console.Log(err.Error())
86-
return false
87-
}
88-
89-
root = root.Compile()
90-
91-
ctx := map[string]interface{}{}
92-
for _, key := range js.Keys(options) {
93-
ctx[key] = options.Get(key).Interface()
94-
}
95-
96-
result, err := dfl.EvaluateBool(root, ctx, dfl.NewFuntionMapWithDefaults(), GO_DFL_DEFAULT_QUOTES[1:])
97-
if err != nil {
98-
console.Error(err.Error())
99-
return false
100-
}
101-
102-
return result
103-
}
104-
105-
func EvaluateInt(s string, options *js.Object) int {
106-
root, err := dfl.Parse(s)
107-
if err != nil {
108-
console.Error(err.Error())
109-
return 0
110-
}
111-
112-
root = root.Compile()
113-
114-
ctx := map[string]interface{}{}
115-
for _, key := range js.Keys(options) {
116-
ctx[key] = options.Get(key).Interface()
117-
}
118-
119-
result, err := dfl.EvaluateInt(root, ctx, dfl.NewFuntionMapWithDefaults(), GO_DFL_DEFAULT_QUOTES[1:])
120-
if err != nil {
121-
console.Error(err.Error())
122-
return 0
123-
}
124-
125-
return result
126-
}
127-
128-
func EvaluateFloat64(s string, options *js.Object) float64 {
129-
root, err := dfl.Parse(s)
130-
if err != nil {
131-
console.Error(err.Error())
132-
return 0.0
133-
}
134-
135-
root = root.Compile()
136-
137-
ctx := map[string]interface{}{}
138-
for _, key := range js.Keys(options) {
139-
ctx[key] = options.Get(key).Interface()
140-
}
141-
142-
result, err := dfl.EvaluateFloat64(root, ctx, dfl.NewFuntionMapWithDefaults(), GO_DFL_DEFAULT_QUOTES[1:])
143-
if err != nil {
144-
console.Error(err.Error())
145-
return 0.0
146-
}
147-
148-
return result
149-
}
150-
151-
func EvaluateString(s string, options *js.Object) string {
152-
root, err := dfl.Parse(s)
153-
if err != nil {
154-
console.Error(err.Error())
155-
return ""
156-
}
157-
158-
root = root.Compile()
159-
160-
ctx := map[string]interface{}{}
161-
for _, key := range js.Keys(options) {
162-
ctx[key] = options.Get(key).Interface()
163-
}
164-
165-
result, err := dfl.EvaluateString(root, ctx, dfl.NewFuntionMapWithDefaults(), GO_DFL_DEFAULT_QUOTES[1:])
166-
if err != nil {
167-
console.Error(err.Error())
168-
return ""
169-
}
170-
171-
return result
172-
}

cmd/dfl/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//
66
// =================================================================
77

8-
// Dfl is the command line utility for testing DFL expressions.
8+
// dfl is the command line program for DFL.
99
//
1010
// Usage
1111
//

dfljs/DefaultQuotes.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
package dfljs
9+
10+
// DefaultQuotes is the default quotes for JavaScript.
11+
var DefaultQuotes = []string{"\"", "'", "`"}

dfljs/EvaluateBool.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
package dfljs
9+
10+
import (
11+
"github.com/gopherjs/gopherjs/js"
12+
"github.com/pkg/errors"
13+
"github.com/spatialcurrent/go-dfl/dfl"
14+
"honnef.co/go/js/console"
15+
)
16+
17+
// EvaluateBool provides a simple function that parses, compiles, and executes an expression against a context object and returns true or false.
18+
func EvaluateBool(s string, options *js.Object) bool {
19+
root, err := dfl.Parse(s)
20+
if err != nil {
21+
console.Error(errors.Wrap(err, "error parsing expression in EvaluateBool").Error())
22+
return false
23+
}
24+
25+
root = root.Compile()
26+
27+
ctx := map[string]interface{}{}
28+
for _, key := range js.Keys(options) {
29+
ctx[key] = options.Get(key).Interface()
30+
}
31+
32+
result, err := dfl.EvaluateBool(root, ctx, dfl.NewFuntionMapWithDefaults(), DefaultQuotes[1:])
33+
if err != nil {
34+
console.Error(errors.Wrap(err, "error evaluating a node in EvaluateBool").Error())
35+
return false
36+
}
37+
38+
return result
39+
}

dfljs/EvaluateFloat.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
package dfljs
9+
10+
import (
11+
"github.com/gopherjs/gopherjs/js"
12+
"github.com/pkg/errors"
13+
"github.com/spatialcurrent/go-dfl/dfl"
14+
"honnef.co/go/js/console"
15+
)
16+
17+
// EvaluateFloat64 provides a simple function that parses, compiles, and executes an expression against a context object and returns a float64.
18+
func EvaluateFloat64(s string, options *js.Object) float64 {
19+
root, err := dfl.Parse(s)
20+
if err != nil {
21+
console.Error(errors.Wrap(err, "error parsing expression in EvaluateFloat64").Error())
22+
return 0.0
23+
}
24+
25+
root = root.Compile()
26+
27+
ctx := map[string]interface{}{}
28+
for _, key := range js.Keys(options) {
29+
ctx[key] = options.Get(key).Interface()
30+
}
31+
32+
result, err := dfl.EvaluateFloat64(root, ctx, dfl.NewFuntionMapWithDefaults(), DefaultQuotes[1:])
33+
if err != nil {
34+
console.Error(errors.Wrap(err, "error evaluating a node in EvaluateFloat64").Error())
35+
return 0.0
36+
}
37+
38+
return result
39+
}

dfljs/EvaluateInt.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
package dfljs
9+
10+
import (
11+
"github.com/gopherjs/gopherjs/js"
12+
"github.com/pkg/errors"
13+
"github.com/spatialcurrent/go-dfl/dfl"
14+
"honnef.co/go/js/console"
15+
)
16+
17+
// EvaluateInt provides a simple function that parses, compiles, and executes an expression against a context object and returns an integer.
18+
func EvaluateInt(s string, options *js.Object) int {
19+
root, err := dfl.Parse(s)
20+
if err != nil {
21+
console.Error(errors.Wrap(err, "error parsing expression in EvaluateInt").Error())
22+
return 0
23+
}
24+
25+
root = root.Compile()
26+
27+
ctx := map[string]interface{}{}
28+
for _, key := range js.Keys(options) {
29+
ctx[key] = options.Get(key).Interface()
30+
}
31+
32+
result, err := dfl.EvaluateInt(root, ctx, dfl.NewFuntionMapWithDefaults(), DefaultQuotes[1:])
33+
if err != nil {
34+
console.Error(errors.Wrap(err, "error evaluating a node in EvaluateInt").Error())
35+
return 0
36+
}
37+
38+
return result
39+
}

dfljs/EvaluateString.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
package dfljs
9+
10+
import (
11+
"github.com/gopherjs/gopherjs/js"
12+
"github.com/pkg/errors"
13+
"github.com/spatialcurrent/go-dfl/dfl"
14+
"honnef.co/go/js/console"
15+
)
16+
17+
// EvaluateString provides a simple function that parses, compiles, and executes an expression against a context object and returns a string.
18+
func EvaluateString(s string, options *js.Object) string {
19+
root, err := dfl.Parse(s)
20+
if err != nil {
21+
console.Error(errors.Wrap(err, "error parsing expression in EvaluateString").Error())
22+
return ""
23+
}
24+
25+
root = root.Compile()
26+
27+
ctx := map[string]interface{}{}
28+
for _, key := range js.Keys(options) {
29+
ctx[key] = options.Get(key).Interface()
30+
}
31+
32+
result, err := dfl.EvaluateString(root, ctx, dfl.NewFuntionMapWithDefaults(), DefaultQuotes[1:])
33+
if err != nil {
34+
console.Error(errors.Wrap(err, "error evaluating a node in EvaluateString").Error())
35+
return ""
36+
}
37+
38+
return result
39+
}

0 commit comments

Comments
 (0)