Skip to content

Commit 85ffe57

Browse files
committed
Fix bug with nil vars
1 parent be55ef0 commit 85ffe57

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

expr_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,33 @@ func ExampleEnv() {
188188
// Output: true
189189
}
190190

191+
func ExampleEnv_with_undefined_variables() {
192+
env := map[string]interface{}{
193+
"foo": 0,
194+
"bar": 0,
195+
}
196+
197+
program, err := expr.Compile(`foo + (bar != nil ? bar : 2)`, expr.Env(env))
198+
if err != nil {
199+
fmt.Printf("%v", err)
200+
return
201+
}
202+
203+
request := map[string]interface{}{
204+
"foo": 3,
205+
}
206+
207+
output, err := expr.Run(program, request)
208+
if err != nil {
209+
fmt.Printf("%v", err)
210+
return
211+
}
212+
213+
fmt.Printf("%v", output)
214+
215+
// Output: 5
216+
}
217+
191218
func ExampleAsBool() {
192219
env := map[string]int{
193220
"foo": 0,

vm/runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func fetch(from interface{}, i interface{}) interface{} {
4545
}
4646

4747
}
48-
panic(fmt.Sprintf("%v doesn't contains %v", from, i))
48+
return nil
4949
}
5050

5151
func slice(array, from, to interface{}) interface{} {

vm/vm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (vm *VM) Run(program *Program, env interface{}) interface{} {
9191
vm.push(false)
9292

9393
case OpNil:
94-
vm.push(new(struct{}))
94+
vm.push(nil)
9595

9696
case OpNegate:
9797
v := negate(vm.pop())

0 commit comments

Comments
 (0)