Skip to content

Commit

Permalink
✨ support dict.inRange for numbers (#2953)
Browse files Browse the repository at this point in the history
* ⭐ number.inRange support

Support a simpler methods to see if a number is in range.

So far users have to:

```coffee
num >= 3 && num <= 5
```

Now you can:

```coffee
num.inRange(3, 5)
```

This supports both integers and floats. Dict support is coming next.

Signed-off-by: Dominik Richter <[email protected]>

* ✨ support dict.inRange for numbers

After #2952

Signed-off-by: Dominik Richter <[email protected]>

---------

Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus authored Jan 6, 2024
1 parent 6c9a3f0 commit 83cce30
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions llx/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ func init() {
"last": {f: dictGetLastIndexV2},
"{}": {f: dictBlockCallV2},
"length": {f: dictLengthV2},
"inRange": {f: dictInRange},
"camelcase": {f: dictCamelcaseV2, Label: "camelcase"},
"downcase": {f: dictDowncaseV2, Label: "downcase"},
"upcase": {f: dictUpcaseV2, Label: "upcase"},
Expand Down
17 changes: 16 additions & 1 deletion llx/builtin_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,22 @@ func dictLengthV2(e *blockExecutor, bind *RawData, chunk *Chunk, ref uint64) (*R
case map[string]interface{}:
return IntData(int64(len(x))), 0, nil
default:
return nil, 0, errors.New("dict value does not support field `length`")
return nil, 0, errors.New("dict value does not support `length`")
}
}

func dictInRange(e *blockExecutor, bind *RawData, chunk *Chunk, ref uint64) (*RawData, uint64, error) {
if bind.Value == nil {
return BoolFalse, 0, nil
}

switch x := bind.Value.(type) {
case int64:
return int64InRange(e, x, chunk, ref)
case float64:
return float64InRange(e, x, chunk, ref)
default:
return nil, 0, errors.New("dict value does not support `inRange`")
}
}

Expand Down
2 changes: 2 additions & 0 deletions mqlc/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func init() {
types.Dict: {
"[]": {typ: dictType, signature: FunctionSignature{Required: 1, Args: []types.Type{types.Any}}},
"{}": {typ: blockType, signature: FunctionSignature{Required: 1, Args: []types.Type{types.FunctionLike}}},
// number-ish
"inRange": {typ: boolType, compile: compileInRange},
// string-ish
"find": {typ: stringArrayType, signature: FunctionSignature{Required: 1, Args: []types.Type{types.Regex}}},
"length": {typ: intType, signature: FunctionSignature{}},
Expand Down
18 changes: 18 additions & 0 deletions providers/os/resources/mql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,24 @@ func TestResource_duplicateFields(t *testing.T) {
})
}

func TestDict_Methods_InRange(t *testing.T) {
p := "parse.json('/dummy.json')."

x := testutils.InitTester(testutils.LinuxMock())
x.TestSimple(t, []testutils.SimpleTest{
{
Code: p + "params['1'].inRange(1,3)",
ResultIndex: 1,
Expectation: true,
},
{
Code: p + "params['1'].inRange(3,4)",
ResultIndex: 1,
Expectation: false,
},
})
}

func TestDict_Methods_Contains(t *testing.T) {
p := "parse.json('/dummy.json')."

Expand Down

0 comments on commit 83cce30

Please sign in to comment.