Skip to content

Commit 7df0cda

Browse files
committed
Delete toBool and toText
1 parent 5846dec commit 7df0cda

File tree

4 files changed

+17
-84
lines changed

4 files changed

+17
-84
lines changed

bench_test.go

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"testing"
55

66
"github.com/antonmedv/expr"
7-
"github.com/dop251/goja"
8-
"github.com/robertkrimen/otto"
97
)
108

119
type segment struct {
@@ -40,50 +38,3 @@ func Benchmark_expr(b *testing.B) {
4038
expr.Run(script, r)
4139
}
4240
}
43-
44-
func Benchmark_otto(b *testing.B) {
45-
r := &request{
46-
Segments: []*segment{
47-
{Origin: "MOW"},
48-
},
49-
Passengers: &passengers{
50-
Adults: 2,
51-
},
52-
Marker: "test",
53-
}
54-
55-
vm := otto.New()
56-
57-
script, err := vm.Compile("", `r.Segments[0].Origin == "MOW" && r.Passengers.Adults == 2 && r.Marker == "test"`)
58-
if err != nil {
59-
b.Fatal(err)
60-
}
61-
62-
for n := 0; n < b.N; n++ {
63-
vm.Set("r", r)
64-
vm.Run(script)
65-
}
66-
}
67-
68-
func Benchmark_goja(b *testing.B) {
69-
r := &request{
70-
Segments: []*segment{
71-
{Origin: "MOW"},
72-
},
73-
Passengers: &passengers{
74-
Adults: 2,
75-
},
76-
Marker: "test",
77-
}
78-
79-
vm := goja.New()
80-
program, err := goja.Compile("", `r.Segments[0].Origin == "MOW" && r.Passengers.Adults == 2 && r.Marker == "test"`, false)
81-
if err != nil {
82-
b.Fatal(err)
83-
}
84-
85-
for n := 0; n < b.N; n++ {
86-
vm.Set("r", r)
87-
vm.RunProgram(program)
88-
}
89-
}

eval.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (n unaryNode) Eval(env interface{}) (interface{}, error) {
6565

6666
switch n.operator {
6767
case "not", "!":
68-
return !toBool(n, val), nil
68+
return !val.(bool), nil
6969
}
7070

7171
v := toNumber(n, val)
@@ -87,22 +87,22 @@ func (n binaryNode) Eval(env interface{}) (interface{}, error) {
8787

8888
switch n.operator {
8989
case "or", "||":
90-
if toBool(n.left, left) {
90+
if left.(bool) {
9191
return true, nil
9292
}
9393
right, err := n.right.Eval(env)
9494
if err != nil {
9595
return nil, err
9696
}
97-
return toBool(n.right, right), nil
97+
return right.(bool), nil
9898

9999
case "and", "&&":
100-
if toBool(n.left, left) {
100+
if left.(bool) {
101101
right, err := n.right.Eval(env)
102102
if err != nil {
103103
return nil, err
104104
}
105-
return toBool(n.right, right), nil
105+
return right.(bool), nil
106106
}
107107
return false, nil
108108
}
@@ -134,7 +134,7 @@ func (n binaryNode) Eval(env interface{}) (interface{}, error) {
134134
return !ok, nil
135135

136136
case "~":
137-
return toText(n.left, left) + toText(n.right, right), nil
137+
return left.(string) + right.(string), nil
138138
}
139139

140140
// Next goes operators on numbers
@@ -216,15 +216,15 @@ func (n matchesNode) Eval(env interface{}) (interface{}, error) {
216216
}
217217

218218
if n.r != nil {
219-
return n.r.MatchString(toText(n.left, left)), nil
219+
return n.r.MatchString(left.(string)), nil
220220
}
221221

222222
right, err := n.right.Eval(env)
223223
if err != nil {
224224
return nil, err
225225
}
226226

227-
matched, err := regexp.MatchString(toText(n.right, right), toText(n.left, left))
227+
matched, err := regexp.MatchString(right.(string), left.(string))
228228
if err != nil {
229229
return nil, err
230230
}
@@ -361,7 +361,7 @@ func (n conditionalNode) Eval(env interface{}) (interface{}, error) {
361361
}
362362

363363
// If
364-
if toBool(n.cond, cond) {
364+
if cond.(bool) {
365365
// Then
366366
a, err := n.exp1.Eval(env)
367367
if err != nil {

eval_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,22 +350,22 @@ var evalErrorTests = []evalErrorTest{
350350
{
351351
`"foo" ~ foo`,
352352
map[string]*int{"foo": nil},
353-
`cannot convert foo (type *int) to type string`,
353+
`interface conversion: interface {} is *int, not string`,
354354
},
355355
{
356356
"1 or 0",
357357
nil,
358-
"cannot convert 1 (type float64) to type bool",
358+
"interface conversion: interface {} is float64, not bool",
359359
},
360360
{
361361
"not nil",
362362
nil,
363-
"cannot convert not nil (type <nil>) to type bool",
363+
"interface conversion: interface {} is nil, not bool",
364364
},
365365
{
366366
"nil matches 'nil'",
367367
nil,
368-
"cannot convert nil (type <nil>) to type string",
368+
"interface conversion: interface {} is nil, not string",
369369
},
370370
{
371371
"foo['bar'].baz",
@@ -390,22 +390,22 @@ var evalErrorTests = []evalErrorTest{
390390
{
391391
`1 matches "1" ~ "2"`,
392392
nil,
393-
"cannot convert 1 (type float64) to type string",
393+
"interface conversion: interface {} is float64, not string",
394394
},
395395
{
396396
`1 matches "1"`,
397397
nil,
398-
"cannot convert 1 (type float64) to type string",
398+
"interface conversion: interface {} is float64, not string",
399399
},
400400
{
401401
`"1" matches 1`,
402402
nil,
403-
"cannot convert 1 (type float64) to type string",
403+
"interface conversion: interface {} is float64, not string",
404404
},
405405
{
406406
`foo ? 1 : 2`,
407407
map[string]interface{}{"foo": 0},
408-
`cannot convert foo (type int) to type bool`,
408+
`interface conversion: interface {} is int, not bool`,
409409
},
410410
{
411411
`foo()`,

runtime.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,6 @@ import (
55
"reflect"
66
)
77

8-
func toBool(n Node, val interface{}) bool {
9-
v := reflect.ValueOf(val)
10-
switch v.Kind() {
11-
case reflect.Bool:
12-
return v.Bool()
13-
}
14-
panic(fmt.Sprintf("cannot convert %v (type %T) to type bool", n, val))
15-
}
16-
17-
func toText(n Node, val interface{}) string {
18-
v := reflect.ValueOf(val)
19-
switch v.Kind() {
20-
case reflect.String:
21-
return v.String()
22-
}
23-
panic(fmt.Sprintf("cannot convert %v (type %T) to type string", n, val))
24-
}
25-
268
func toNumber(n Node, val interface{}) float64 {
279
v, ok := cast(val)
2810
if ok {

0 commit comments

Comments
 (0)