Fix bug with nil vars

This commit is contained in:
Anton Medvedev
2019-07-10 13:59:45 +03:00
parent c9de399eec
commit 104dae12bd
3 changed files with 29 additions and 2 deletions
+27
View File
@@ -188,6 +188,33 @@ func ExampleEnv() {
// Output: true
}
func ExampleEnv_with_undefined_variables() {
env := map[string]interface{}{
"foo": 0,
"bar": 0,
}
program, err := expr.Compile(`foo + (bar != nil ? bar : 2)`, expr.Env(env))
if err != nil {
fmt.Printf("%v", err)
return
}
request := map[string]interface{}{
"foo": 3,
}
output, err := expr.Run(program, request)
if err != nil {
fmt.Printf("%v", err)
return
}
fmt.Printf("%v", output)
// Output: 5
}
func ExampleAsBool() {
env := map[string]int{
"foo": 0,
+1 -1
View File
@@ -45,7 +45,7 @@ func fetch(from interface{}, i interface{}) interface{} {
}
}
panic(fmt.Sprintf("%v doesn't contains %v", from, i))
return nil
}
func slice(array, from, to interface{}) interface{} {
+1 -1
View File
@@ -91,7 +91,7 @@ func (vm *VM) Run(program *Program, env interface{}) interface{} {
vm.push(false)
case OpNil:
vm.push(new(struct{}))
vm.push(nil)
case OpNegate:
v := negate(vm.pop())