Skip to content

Commit d22339d

Browse files
committed
Add support for maps in len builtin
1 parent 022271e commit d22339d

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

docs/Language-Definition.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ user.Age > 30 ? "mature" : "immature"
148148

149149
## Builtin functions
150150

151-
* `len` (length of array or string)
151+
* `len` (length of array, map or string)
152152
* `all` (will return `true` if all element satisfies the predicate)
153153
* `none` (will return `true` if all element does NOT satisfies the predicate)
154154
* `any` (will return `true` if any element satisfies the predicate)

expr_test.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,18 @@ func TestExpr(t *testing.T) {
684684
`len(["hello", "world"])`,
685685
2,
686686
},
687+
{
688+
`len("hello, world")`,
689+
12,
690+
},
691+
{
692+
`len(Array)`,
693+
5,
694+
},
695+
{
696+
`len({a: 1, b: 2, c: 2})`,
697+
3,
698+
},
687699
{
688700
`{foo: 0, bar: 1}`,
689701
map[string]interface{}{"foo": 0, "bar": 1},
@@ -696,10 +708,6 @@ func TestExpr(t *testing.T) {
696708
`(true ? 0+1 : 2+3) + (false ? -1 : -2)`,
697709
-1,
698710
},
699-
{
700-
`len(Array)`,
701-
5,
702-
},
703711
{
704712
`filter(1..9, {# > 7})`,
705713
[]interface{}{8, 9},

vm/runtime.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func in(needle interface{}, array interface{}) bool {
173173
func length(a interface{}) int {
174174
v := reflect.ValueOf(a)
175175
switch v.Kind() {
176-
case reflect.Array, reflect.Slice, reflect.String:
176+
case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
177177
return v.Len()
178178
default:
179179
panic(fmt.Sprintf("invalid argument for len (type %T)", a))

0 commit comments

Comments
 (0)