Skip to content

Commit 30e1621

Browse files
committed
add custom quotes
1 parent d87968c commit 30e1621

57 files changed

Lines changed: 224 additions & 215 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/dfl.js/main.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"honnef.co/go/js/console"
2727
)
2828

29+
var GO_DFL_DEFAULT_QUOTES = []string{"\"", "'", "`"}
30+
2931
type Node struct {
3032
Node dfl.Node
3133
FunctionMap dfl.FunctionMap
@@ -45,7 +47,7 @@ func (n Node) Evaluate(options *js.Object) interface{} {
4547
ctx[key] = options.Get(key).Interface()
4648
}
4749

48-
result, err := n.Node.Evaluate(ctx, n.FunctionMap)
50+
result, err := n.Node.Evaluate(ctx, n.FunctionMap, GO_DFL_DEFAULT_QUOTES[1:])
4951
if err != nil {
5052
console.Log(err.Error())
5153
return false
@@ -87,7 +89,7 @@ func EvaluateBool(s string, options *js.Object) bool {
8789
ctx[key] = options.Get(key).Interface()
8890
}
8991

90-
result, err := dfl.EvaluateBool(root, ctx, dfl.NewFuntionMapWithDefaults())
92+
result, err := dfl.EvaluateBool(root, ctx, dfl.NewFuntionMapWithDefaults(), GO_DFL_DEFAULT_QUOTES[1:])
9193
if err != nil {
9294
console.Log(err.Error())
9395
return false
@@ -110,7 +112,7 @@ func EvaluateInt(s string, options *js.Object) int {
110112
ctx[key] = options.Get(key).Interface()
111113
}
112114

113-
result, err := dfl.EvaluateInt(root, ctx, dfl.NewFuntionMapWithDefaults())
115+
result, err := dfl.EvaluateInt(root, ctx, dfl.NewFuntionMapWithDefaults(), GO_DFL_DEFAULT_QUOTES[1:])
114116
if err != nil {
115117
console.Log(err.Error())
116118
return 0
@@ -133,7 +135,7 @@ func EvaluateFloat64(s string, options *js.Object) float64 {
133135
ctx[key] = options.Get(key).Interface()
134136
}
135137

136-
result, err := dfl.EvaluateFloat64(root, ctx, dfl.NewFuntionMapWithDefaults())
138+
result, err := dfl.EvaluateFloat64(root, ctx, dfl.NewFuntionMapWithDefaults(), GO_DFL_DEFAULT_QUOTES[1:])
137139
if err != nil {
138140
console.Log(err.Error())
139141
return 0.0
@@ -156,7 +158,7 @@ func EvaluateString(s string, options *js.Object) string {
156158
ctx[key] = options.Get(key).Interface()
157159
}
158160

159-
result, err := dfl.EvaluateString(root, ctx, dfl.NewFuntionMapWithDefaults())
161+
result, err := dfl.EvaluateString(root, ctx, dfl.NewFuntionMapWithDefaults() GO_DFL_DEFAULT_QUOTES[1:])
160162
if err != nil {
161163
console.Log(err.Error())
162164
return ""

cmd/dfl/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import (
4444
"github.com/spatialcurrent/go-dfl/dfl"
4545
)
4646

47+
var GO_DFL_DEFAULT_QUOTES = []string{"'", "\"", "`"}
48+
4749
func main() {
4850

4951
start := time.Now()
@@ -154,9 +156,11 @@ func main() {
154156
os.Exit(1)
155157
}
156158
fmt.Println(string(out))
159+
160+
fmt.Println(GO_DFL_DEFAULT_QUOTES[0] + root.Dfl(GO_DFL_DEFAULT_QUOTES[1:]) + GO_DFL_DEFAULT_QUOTES[0])
157161
}
158162

159-
result, err := root.Evaluate(ctx, dfl.NewFuntionMapWithDefaults())
163+
result, err := root.Evaluate(ctx, dfl.NewFuntionMapWithDefaults(), GO_DFL_DEFAULT_QUOTES[1:])
160164
if err != nil {
161165
fmt.Println("Error evaluating expression.")
162166
fmt.Println(err)

dfl/Add.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ type Add struct {
1313
}
1414

1515
// Dfl returns the DFL representation of this node as a string
16-
func (a Add) Dfl() string {
17-
return "(" + a.Left.Dfl() + " + " + a.Right.Dfl() + ")"
16+
func (a Add) Dfl(quotes []string, pretty bool) string {
17+
return "(" + a.Left.Dfl(quotes, pretty) + " + " + a.Right.Dfl(quotes, pretty) + ")"
1818
}
1919

2020
// Map returns a map representation of this node
@@ -47,9 +47,9 @@ func (a Add) Compile() Node {
4747
}
4848

4949
// Evaluate returns the value of this node given Context ctx, and an error if any.
50-
func (a Add) Evaluate(ctx interface{}, funcs FunctionMap) (interface{}, error) {
50+
func (a Add) Evaluate(ctx interface{}, funcs FunctionMap, quotes []string) (interface{}, error) {
5151

52-
lv, rv, err := a.EvaluateLeftAndRight(ctx, funcs)
52+
lv, rv, err := a.EvaluateLeftAndRight(ctx, funcs, quotes)
5353
if err != nil {
5454
return 0, err
5555
}

dfl/Add_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestAdd(t *testing.T) {
3434
continue
3535
}
3636
node = node.Compile()
37-
got, err := node.Evaluate(testCase.Context, FunctionMap{})
37+
got, err := node.Evaluate(testCase.Context, NewFuntionMapWithDefaults(), DefaultQuotes)
3838
if err != nil {
3939
t.Errorf(errors.Wrap(err, "Error evaluating expression \""+testCase.Expression+"\"").Error())
4040
} else if got != testCase.Result {

dfl/After.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ type After struct {
1313
*TemporalBinaryOperator // Extends the TemporalBinaryOperator struct
1414
}
1515

16-
func (a After) Dfl() string {
17-
return "(" + a.Left.Dfl() + " after " + a.Right.Dfl() + ")"
16+
func (a After) Dfl(quotes []string, pretty bool) string {
17+
return "(" + a.Left.Dfl(quotes, pretty) + " after " + a.Right.Dfl(quotes, pretty) + ")"
1818
}
1919

2020
func (a After) Map() map[string]interface{} {
@@ -42,9 +42,9 @@ func (a After) Compile() Node {
4242
return After{&TemporalBinaryOperator{&BinaryOperator{Left: left, Right: right}}}
4343
}
4444

45-
func (a After) Evaluate(ctx interface{}, funcs FunctionMap) (interface{}, error) {
45+
func (a After) Evaluate(ctx interface{}, funcs FunctionMap, quotes []string) (interface{}, error) {
4646

47-
v, err := a.EvaluateAndCompare(ctx, funcs)
47+
v, err := a.EvaluateAndCompare(ctx, funcs, quotes)
4848
if err != nil {
4949
return false, err
5050
}

dfl/After_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestAfter(t *testing.T) {
3838
continue
3939
}
4040
node = node.Compile()
41-
got, err := node.Evaluate(testCase.Context, FunctionMap{})
41+
got, err := node.Evaluate(testCase.Context, NewFuntionMapWithDefaults(), DefaultQuotes)
4242
if err != nil {
4343
t.Errorf(errors.Wrap(err, "Error evaluating expression \""+testCase.Expression+"\"").Error())
4444
} else if got != testCase.Result {

dfl/And.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ type And struct {
1616
*BinaryOperator
1717
}
1818

19-
func (a And) Dfl() string {
20-
return "(" + a.Left.Dfl() + " and " + a.Right.Dfl() + ")"
19+
func (a And) Dfl(quotes []string, pretty bool) string {
20+
return "(" + a.Left.Dfl(quotes, pretty) + " and " + a.Right.Dfl(quotes, pretty) + ")"
2121
}
2222

2323
func (a And) Map() map[string]interface{} {
@@ -50,8 +50,8 @@ func (a And) Compile() Node {
5050
return And{&BinaryOperator{Left: left, Right: right}}
5151
}
5252

53-
func (a And) Evaluate(ctx interface{}, funcs FunctionMap) (interface{}, error) {
54-
lv, err := a.Left.Evaluate(ctx, funcs)
53+
func (a And) Evaluate(ctx interface{}, funcs FunctionMap, quotes []string) (interface{}, error) {
54+
lv, err := a.Left.Evaluate(ctx, funcs, quotes)
5555
if err != nil {
5656
return false, err
5757
}
@@ -60,7 +60,7 @@ func (a And) Evaluate(ctx interface{}, funcs FunctionMap) (interface{}, error) {
6060
if !lv.(bool) {
6161
return false, nil
6262
}
63-
rv, err := a.Right.Evaluate(ctx, funcs)
63+
rv, err := a.Right.Evaluate(ctx, funcs, quotes)
6464
if err != nil {
6565
return false, err
6666
}
@@ -69,5 +69,5 @@ func (a And) Evaluate(ctx interface{}, funcs FunctionMap) (interface{}, error) {
6969
return rv.(bool), nil
7070
}
7171
}
72-
return false, errors.New("Error evaluating expression " + a.Dfl())
72+
return false, errors.New("Error evaluating expression " + a.Dfl(quotes, false))
7373
}

dfl/And_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestAnd(t *testing.T) {
3434
continue
3535
}
3636
node = node.Compile()
37-
got, err := node.Evaluate(testCase.Context, FunctionMap{})
37+
got, err := node.Evaluate(testCase.Context, NewFuntionMapWithDefaults(), DefaultQuotes)
3838
if err != nil {
3939
t.Errorf(errors.Wrap(err, "Error evaluating expression \""+testCase.Expression+"\"").Error())
4040
} else if got != testCase.Result {

dfl/Array.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ type Array struct {
1616
Nodes []Node
1717
}
1818

19-
func (a Array) Dfl() string {
19+
func (a Array) Dfl(quotes []string, pretty bool) string {
2020
str := "["
2121
for i, x := range a.Nodes {
2222
if i > 0 {
2323
str += ", "
2424
}
25-
str += x.Dfl()
25+
str += x.Dfl(quotes, pretty)
2626
}
2727
str = str + "]"
2828
return str
@@ -52,10 +52,10 @@ func (a Array) Compile() Node {
5252
return Literal{Value: TryConvertArray(values)}
5353
}
5454

55-
func (a Array) Evaluate(ctx interface{}, funcs FunctionMap) (interface{}, error) {
55+
func (a Array) Evaluate(ctx interface{}, funcs FunctionMap, quotes []string) (interface{}, error) {
5656
values := make([]interface{}, len(a.Nodes))
5757
for i, n := range a.Nodes {
58-
v, err := n.Evaluate(ctx, funcs)
58+
v, err := n.Evaluate(ctx, funcs, quotes)
5959
if err != nil {
6060
return values, err
6161
}

dfl/Attribute.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Attribute struct {
1414
Name string
1515
}
1616

17-
func (a Attribute) Dfl() string {
17+
func (a Attribute) Dfl(quotes []string, pretty bool) string {
1818
return "@" + a.Name
1919
}
2020

@@ -28,7 +28,7 @@ func (a Attribute) Compile() Node {
2828
return Attribute{Name: a.Name}
2929
}
3030

31-
func (a Attribute) Evaluate(ctx interface{}, funcs FunctionMap) (interface{}, error) {
31+
func (a Attribute) Evaluate(ctx interface{}, funcs FunctionMap, quotes []string) (interface{}, error) {
3232
if len(a.Name) == 0 {
3333
return ctx, nil
3434
}

0 commit comments

Comments
 (0)