Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support optional array and string access #571

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ var Builtins = []*Function{
return
}
}()
return runtime.Fetch(args[0], 0), nil
return runtime.Fetch(args[0], 0, true), nil
},
Validate: func(args []reflect.Type) (reflect.Type, error) {
if len(args) != 1 {
Expand All @@ -704,7 +704,7 @@ var Builtins = []*Function{
return
}
}()
return runtime.Fetch(args[0], -1), nil
return runtime.Fetch(args[0], -1, true), nil
},
Validate: func(args []reflect.Type) (reflect.Type, error) {
if len(args) != 1 {
Expand All @@ -727,7 +727,7 @@ var Builtins = []*Function{
return
}
}()
return runtime.Fetch(args[0], args[1]), nil
return runtime.Fetch(args[0], args[1], true), nil
},
},
{
Expand Down
6 changes: 6 additions & 0 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,12 @@ func (v *checker) MemberNode(node *ast.MemberNode) (reflect.Type, info) {
}
return base.Elem(), info{}

case reflect.String:
if !isInteger(prop) && !isAny(prop) {
return v.error(node.Property, "string elements can only be selected using an integer (got %v)", prop)
}
return stringType, info{}

case reflect.Struct:
if name, ok := node.Property.(*ast.StringNode); ok {
propertyName := name.Value
Expand Down
6 changes: 5 additions & 1 deletion compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,11 @@ func (c *compiler) MemberNode(node *ast.MemberNode) {

if op == OpFetch {
c.compile(node.Property)
c.emit(OpFetch)
if node.Optional {
c.emit(OpOptionalFetch)
} else {
c.emit(op)
}
} else {
c.emitLocation(node.Location(), op, c.addConstant(
&runtime.Field{Index: index, Path: path},
Expand Down
82 changes: 79 additions & 3 deletions compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,71 @@ func TestCompile_optimizes_jumps(t *testing.T) {
{vm.OpNil, 0},
},
},
{
`let m = {"a": {"b": {"c": 1}}}; m.a.b.c ?? 'nil coalescing'`,
[]op{
{vm.OpPush, 0},
{vm.OpPush, 1},
{vm.OpPush, 2},
{vm.OpPush, 3},
{vm.OpPush, 3},
{vm.OpMap, 0},
{vm.OpPush, 3},
{vm.OpMap, 0},
{vm.OpPush, 3},
{vm.OpMap, 0},
{vm.OpStore, 0},
{vm.OpLoadVar, 0},
{vm.OpJumpIfNil, 8},
{vm.OpPush, 0},
{vm.OpOptionalFetch, 0},
{vm.OpJumpIfNil, 5},
{vm.OpPush, 1},
{vm.OpOptionalFetch, 0},
{vm.OpJumpIfNil, 2},
{vm.OpPush, 2},
{vm.OpOptionalFetch, 0},
{vm.OpDeref, 0},
{vm.OpJumpIfNotNil, 2},
{vm.OpPop, 0},
{vm.OpPush, 4},
},
},
{
`let m = [{"a": {"b": {"c": 1}}}]; m[5].a.b.c ?? 'nil coalescing'`,
[]op{
{vm.OpPush, 0},
{vm.OpPush, 1},
{vm.OpPush, 2},
{vm.OpPush, 3},
{vm.OpPush, 3},
{vm.OpMap, 0},
{vm.OpPush, 3},
{vm.OpMap, 0},
{vm.OpPush, 3},
{vm.OpMap, 0},
{vm.OpPush, 3},
{vm.OpArray, 0},
{vm.OpStore, 0},
{vm.OpLoadVar, 0},
{vm.OpJumpIfNil, 11},
{vm.OpPush, 4},
{vm.OpOptionalFetch, 0},
{vm.OpJumpIfNil, 8},
{vm.OpPush, 0},
{vm.OpOptionalFetch, 0},
{vm.OpJumpIfNil, 5},
{vm.OpPush, 1},
{vm.OpOptionalFetch, 0},
{vm.OpJumpIfNil, 2},
{vm.OpPush, 2},
{vm.OpOptionalFetch, 0},
{vm.OpDeref, 0},
{vm.OpJumpIfNotNil, 2},
{vm.OpPop, 0},
{vm.OpPush, 5},
},
},
{
`let m = {"a": {"b": {"c": 1}}}; m?.a?.b?.c`,
[]op{
Expand All @@ -532,13 +597,24 @@ func TestCompile_optimizes_jumps(t *testing.T) {
{vm.OpLoadVar, 0},
{vm.OpJumpIfNil, 8},
{vm.OpPush, 0},
{vm.OpFetch, 0},
{vm.OpOptionalFetch, 0},
{vm.OpJumpIfNil, 5},
{vm.OpPush, 1},
{vm.OpFetch, 0},
{vm.OpOptionalFetch, 0},
{vm.OpJumpIfNil, 2},
{vm.OpPush, 2},
{vm.OpFetch, 0},
{vm.OpOptionalFetch, 0},
},
},
{
`let m = [1, 2, 3]; m?.[5]`,
[]op{
{vm.OpPush, 0},
{vm.OpStore, 0},
{vm.OpLoadVar, 0},
{vm.OpJumpIfNil, 2},
{vm.OpPush, 1},
{vm.OpOptionalFetch, 0},
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *Config) ConstExpr(name string) {
if c.Env == nil {
panic("no environment is specified for ConstExpr()")
}
fn := reflect.ValueOf(runtime.Fetch(c.Env, name))
fn := reflect.ValueOf(runtime.Fetch(c.Env, name, true))
if fn.Kind() != reflect.Func {
panic(fmt.Errorf("const expression %q must be a function", name))
}
Expand Down
30 changes: 30 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,18 @@ func TestExpr(t *testing.T) {
nil,
},
{
`[1, 2, 3]?.[5]`,
nil,
},
{
`'string'?.[5]`,
"g",
},
{
`'string'?.[7]`,
nil,
},
{
`1 > 2 < 3`,
false,
},
Expand Down Expand Up @@ -1967,6 +1979,24 @@ func TestRun_NilCoalescingOperator(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "default", out)
})

t.Run("default without chain", func(t *testing.T) {
p, err := expr.Compile(`foo.foo.bar ?? "default"`, expr.Env(env))
assert.NoError(t, err)

out, err := expr.Run(p, map[string]any{})
assert.NoError(t, err)
assert.Equal(t, "default", out)
})

t.Run("array default without chain", func(t *testing.T) {
p, err := expr.Compile(`foo.foo[10].bar ?? "default"`, expr.Env(env))
assert.NoError(t, err)

out, err := expr.Run(p, map[string]any{})
assert.NoError(t, err)
assert.Equal(t, "default", out)
})
}

func TestEval_nil_in_maps(t *testing.T) {
Expand Down
Loading
Loading