mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
Delete toBool and toText
This commit is contained in:
@@ -4,8 +4,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/antonmedv/expr"
|
||||
"github.com/dop251/goja"
|
||||
"github.com/robertkrimen/otto"
|
||||
)
|
||||
|
||||
type segment struct {
|
||||
@@ -40,50 +38,3 @@ func Benchmark_expr(b *testing.B) {
|
||||
expr.Run(script, r)
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_otto(b *testing.B) {
|
||||
r := &request{
|
||||
Segments: []*segment{
|
||||
{Origin: "MOW"},
|
||||
},
|
||||
Passengers: &passengers{
|
||||
Adults: 2,
|
||||
},
|
||||
Marker: "test",
|
||||
}
|
||||
|
||||
vm := otto.New()
|
||||
|
||||
script, err := vm.Compile("", `r.Segments[0].Origin == "MOW" && r.Passengers.Adults == 2 && r.Marker == "test"`)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
for n := 0; n < b.N; n++ {
|
||||
vm.Set("r", r)
|
||||
vm.Run(script)
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_goja(b *testing.B) {
|
||||
r := &request{
|
||||
Segments: []*segment{
|
||||
{Origin: "MOW"},
|
||||
},
|
||||
Passengers: &passengers{
|
||||
Adults: 2,
|
||||
},
|
||||
Marker: "test",
|
||||
}
|
||||
|
||||
vm := goja.New()
|
||||
program, err := goja.Compile("", `r.Segments[0].Origin == "MOW" && r.Passengers.Adults == 2 && r.Marker == "test"`, false)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
for n := 0; n < b.N; n++ {
|
||||
vm.Set("r", r)
|
||||
vm.RunProgram(program)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ func (n unaryNode) Eval(env interface{}) (interface{}, error) {
|
||||
|
||||
switch n.operator {
|
||||
case "not", "!":
|
||||
return !toBool(n, val), nil
|
||||
return !val.(bool), nil
|
||||
}
|
||||
|
||||
v := toNumber(n, val)
|
||||
@@ -87,22 +87,22 @@ func (n binaryNode) Eval(env interface{}) (interface{}, error) {
|
||||
|
||||
switch n.operator {
|
||||
case "or", "||":
|
||||
if toBool(n.left, left) {
|
||||
if left.(bool) {
|
||||
return true, nil
|
||||
}
|
||||
right, err := n.right.Eval(env)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toBool(n.right, right), nil
|
||||
return right.(bool), nil
|
||||
|
||||
case "and", "&&":
|
||||
if toBool(n.left, left) {
|
||||
if left.(bool) {
|
||||
right, err := n.right.Eval(env)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return toBool(n.right, right), nil
|
||||
return right.(bool), nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
@@ -134,7 +134,7 @@ func (n binaryNode) Eval(env interface{}) (interface{}, error) {
|
||||
return !ok, nil
|
||||
|
||||
case "~":
|
||||
return toText(n.left, left) + toText(n.right, right), nil
|
||||
return left.(string) + right.(string), nil
|
||||
}
|
||||
|
||||
// Next goes operators on numbers
|
||||
@@ -216,7 +216,7 @@ func (n matchesNode) Eval(env interface{}) (interface{}, error) {
|
||||
}
|
||||
|
||||
if n.r != nil {
|
||||
return n.r.MatchString(toText(n.left, left)), nil
|
||||
return n.r.MatchString(left.(string)), nil
|
||||
}
|
||||
|
||||
right, err := n.right.Eval(env)
|
||||
@@ -224,7 +224,7 @@ func (n matchesNode) Eval(env interface{}) (interface{}, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
matched, err := regexp.MatchString(toText(n.right, right), toText(n.left, left))
|
||||
matched, err := regexp.MatchString(right.(string), left.(string))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -361,7 +361,7 @@ func (n conditionalNode) Eval(env interface{}) (interface{}, error) {
|
||||
}
|
||||
|
||||
// If
|
||||
if toBool(n.cond, cond) {
|
||||
if cond.(bool) {
|
||||
// Then
|
||||
a, err := n.exp1.Eval(env)
|
||||
if err != nil {
|
||||
|
||||
+8
-8
@@ -350,22 +350,22 @@ var evalErrorTests = []evalErrorTest{
|
||||
{
|
||||
`"foo" ~ foo`,
|
||||
map[string]*int{"foo": nil},
|
||||
`cannot convert foo (type *int) to type string`,
|
||||
`interface conversion: interface {} is *int, not string`,
|
||||
},
|
||||
{
|
||||
"1 or 0",
|
||||
nil,
|
||||
"cannot convert 1 (type float64) to type bool",
|
||||
"interface conversion: interface {} is float64, not bool",
|
||||
},
|
||||
{
|
||||
"not nil",
|
||||
nil,
|
||||
"cannot convert not nil (type <nil>) to type bool",
|
||||
"interface conversion: interface {} is nil, not bool",
|
||||
},
|
||||
{
|
||||
"nil matches 'nil'",
|
||||
nil,
|
||||
"cannot convert nil (type <nil>) to type string",
|
||||
"interface conversion: interface {} is nil, not string",
|
||||
},
|
||||
{
|
||||
"foo['bar'].baz",
|
||||
@@ -390,22 +390,22 @@ var evalErrorTests = []evalErrorTest{
|
||||
{
|
||||
`1 matches "1" ~ "2"`,
|
||||
nil,
|
||||
"cannot convert 1 (type float64) to type string",
|
||||
"interface conversion: interface {} is float64, not string",
|
||||
},
|
||||
{
|
||||
`1 matches "1"`,
|
||||
nil,
|
||||
"cannot convert 1 (type float64) to type string",
|
||||
"interface conversion: interface {} is float64, not string",
|
||||
},
|
||||
{
|
||||
`"1" matches 1`,
|
||||
nil,
|
||||
"cannot convert 1 (type float64) to type string",
|
||||
"interface conversion: interface {} is float64, not string",
|
||||
},
|
||||
{
|
||||
`foo ? 1 : 2`,
|
||||
map[string]interface{}{"foo": 0},
|
||||
`cannot convert foo (type int) to type bool`,
|
||||
`interface conversion: interface {} is int, not bool`,
|
||||
},
|
||||
{
|
||||
`foo()`,
|
||||
|
||||
-18
@@ -5,24 +5,6 @@ import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func toBool(n Node, val interface{}) bool {
|
||||
v := reflect.ValueOf(val)
|
||||
switch v.Kind() {
|
||||
case reflect.Bool:
|
||||
return v.Bool()
|
||||
}
|
||||
panic(fmt.Sprintf("cannot convert %v (type %T) to type bool", n, val))
|
||||
}
|
||||
|
||||
func toText(n Node, val interface{}) string {
|
||||
v := reflect.ValueOf(val)
|
||||
switch v.Kind() {
|
||||
case reflect.String:
|
||||
return v.String()
|
||||
}
|
||||
panic(fmt.Sprintf("cannot convert %v (type %T) to type string", n, val))
|
||||
}
|
||||
|
||||
func toNumber(n Node, val interface{}) float64 {
|
||||
v, ok := cast(val)
|
||||
if ok {
|
||||
|
||||
Reference in New Issue
Block a user