Skip to content

Commit

Permalink
Add support for maps in len builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed May 15, 2020
1 parent 022271e commit d22339d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/Language-Definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ user.Age > 30 ? "mature" : "immature"

## Builtin functions

* `len` (length of array or string)
* `len` (length of array, map or string)
* `all` (will return `true` if all element satisfies the predicate)
* `none` (will return `true` if all element does NOT satisfies the predicate)
* `any` (will return `true` if any element satisfies the predicate)
Expand Down
16 changes: 12 additions & 4 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,18 @@ func TestExpr(t *testing.T) {
`len(["hello", "world"])`,
2,
},
{
`len("hello, world")`,
12,
},
{
`len(Array)`,
5,
},
{
`len({a: 1, b: 2, c: 2})`,
3,
},
{
`{foo: 0, bar: 1}`,
map[string]interface{}{"foo": 0, "bar": 1},
Expand All @@ -696,10 +708,6 @@ func TestExpr(t *testing.T) {
`(true ? 0+1 : 2+3) + (false ? -1 : -2)`,
-1,
},
{
`len(Array)`,
5,
},
{
`filter(1..9, {# > 7})`,
[]interface{}{8, 9},
Expand Down
2 changes: 1 addition & 1 deletion vm/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func in(needle interface{}, array interface{}) bool {
func length(a interface{}) int {
v := reflect.ValueOf(a)
switch v.Kind() {
case reflect.Array, reflect.Slice, reflect.String:
case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
return v.Len()
default:
panic(fmt.Sprintf("invalid argument for len (type %T)", a))
Expand Down

0 comments on commit d22339d

Please sign in to comment.