Skip to content

Commit

Permalink
⭐ add simple accessors for maps
Browse files Browse the repository at this point in the history
The long-overdue counterpart to #1695

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
  • Loading branch information
arlimus committed Jan 7, 2024
1 parent 1b1aecd commit d5e9c01
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
7 changes: 5 additions & 2 deletions mqlc/mqlc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,10 @@ func (c *compiler) compileOperand(operand *parser.Operand) (*llx.Primitive, erro
return nil, err
}
if !found {
if typ != types.Dict || !reAccessor.MatchString(id) {
// we add simple accessors for maps and dicts, but this also requires
// the `id` to look like a regular accessor (to avoid matching against more
// native internal operators)
if !((typ == types.Dict || typ.IsMap()) && reAccessor.MatchString(id)) {
addFieldSuggestions(availableFields(c, typ), id, c.Result)
return nil, errors.New("cannot find field '" + id + "' in " + typ.Label())
}
Expand All @@ -1569,7 +1572,7 @@ func (c *compiler) compileOperand(operand *parser.Operand) (*llx.Primitive, erro
Call: llx.Chunk_FUNCTION,
Id: "[]",
Function: &llx.Function{
Type: string(typ),
Type: string(typ.Child()),
Binding: ref,
Args: []*llx.Primitive{llx.StringPrimitive(id)},
},
Expand Down
4 changes: 2 additions & 2 deletions mqlc/mqlc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1946,9 +1946,9 @@ func TestSuggestions(t *testing.T) {
},
{
// native type function call
"sshd.config.params.leng",
"sshd.config.ciphers.leng",
[]string{"length"},
errors.New("cannot find field 'leng' in map[string]string"),
errors.New("cannot find field 'leng' in []string"),
nil,
},
{
Expand Down
8 changes: 8 additions & 0 deletions providers/core/resources/mql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,14 @@ func TestMap(t *testing.T) {
m := "{'a': 1, 'b': 1, 'c': 2}"
x := testutils.InitTester(testutils.LinuxMock())
x.TestSimple(t, []testutils.SimpleTest{
{
Code: m + "['c']",
ResultIndex: 0, Expectation: int64(2),
},
{
Code: m + ".c",
ResultIndex: 0, Expectation: int64(2),
},
// contains
{
Code: m + ".contains(key == 'a')",
Expand Down

0 comments on commit d5e9c01

Please sign in to comment.