Skip to content
Merged
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
14 changes: 14 additions & 0 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,19 @@ func builtinFlatMap(i *interpreter, funcv, arrv value) (value, error) {
}
}

// builtinFlatMapArray is like builtinFlatMap, but only accepts array as the
// arrv value. Desugared comprehensions contain a call to this function, rather
// than builtinFlatMap, so that a better error message is printed when the
// comprehension would iterate over a non-array.
func builtinFlatMapArray(i *interpreter, funcv, arrv value) (value, error) {
switch arrv := arrv.(type) {
case *valueArray:
return builtinFlatMap(i, funcv, arrv)
default:
return nil, i.typeErrorSpecific(arrv, &valueArray{})
}
}

func joinArrays(i *interpreter, sep *valueArray, arr *valueArray) (value, error) {
result := make([]*cachedThunk, 0, arr.length())
first := true
Expand Down Expand Up @@ -2880,4 +2893,5 @@ var funcBuiltins = buildBuiltinMap([]builtin{

// internal
&unaryBuiltin{name: "$objectFlatMerge", function: builtinUglyObjectFlatMerge, params: ast.Identifiers{"x"}},
&binaryBuiltin{name: "$flatMapArray", function: builtinFlatMapArray, params: ast.Identifiers{"func", "arr"}},
})
2 changes: 1 addition & 1 deletion internal/program/desugarer.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func desugarForSpec(inside ast.Node, loc ast.LocationRange, forSpec *ast.ForSpec
if err != nil {
return nil, err
}
current := buildStdCall("flatMap", loc, function, forSpec.Expr)
current := buildStdCall("$flatMapArray", loc, function, forSpec.Expr)
if forSpec.Outer == nil {
return current, nil
}
Expand Down
1 change: 1 addition & 0 deletions linter/internal/types/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func prepareStdlib(g *typeGraph) {
"mod": g.newSimpleFuncType(stringOrNumber, "a", "b"),
"native": g.newSimpleFuncType(anyFunctionType, "x"),
"$objectFlatMerge": g.newSimpleFuncType(anyObjectType, "x"),
"$flatMapArray": g.newSimpleFuncType(anyArrayType, "func", "arr"),

// Boolean

Expand Down
10 changes: 10 additions & 0 deletions testdata/array_comp_try_iterate_over_empty_string.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RUNTIME ERROR: Unexpected type string, expected array
-------------------------------------------------
testdata/array_comp_try_iterate_over_empty_string:1:1-16

[a for a in '']

-------------------------------------------------
During evaluation


1 change: 1 addition & 0 deletions testdata/array_comp_try_iterate_over_empty_string.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[a for a in '']
Empty file.
2 changes: 1 addition & 1 deletion testdata/array_comp_try_iterate_over_obj.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RUNTIME ERROR: std.flatMap second param must be array / string, got object
RUNTIME ERROR: Unexpected type object, expected array
-------------------------------------------------
testdata/array_comp_try_iterate_over_obj:1:1-16

Expand Down
10 changes: 10 additions & 0 deletions testdata/array_comp_try_iterate_over_string.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RUNTIME ERROR: Unexpected type string, expected array
-------------------------------------------------
testdata/array_comp_try_iterate_over_string:1:1-17

[a for a in 'b']

-------------------------------------------------
During evaluation


1 change: 1 addition & 0 deletions testdata/array_comp_try_iterate_over_string.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[a for a in 'b']
Empty file.
15 changes: 15 additions & 0 deletions testdata/object_comp_try_iterate_over_obj.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
RUNTIME ERROR: Unexpected type object, expected array
-------------------------------------------------
testdata/object_comp_try_iterate_over_obj:1:1-23

{ [a]: 0 for a in {} }

-------------------------------------------------
testdata/object_comp_try_iterate_over_obj:1:1-23

{ [a]: 0 for a in {} }

-------------------------------------------------
During evaluation


1 change: 1 addition & 0 deletions testdata/object_comp_try_iterate_over_obj.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ [a]: 0 for a in {} }
Empty file.
15 changes: 15 additions & 0 deletions testdata/object_comp_try_iterate_over_string.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
RUNTIME ERROR: Unexpected type string, expected array
-------------------------------------------------
testdata/object_comp_try_iterate_over_string:1:1-24

{ [a]: 0 for a in 'b' }

-------------------------------------------------
testdata/object_comp_try_iterate_over_string:1:1-24

{ [a]: 0 for a in 'b' }

-------------------------------------------------
During evaluation


1 change: 1 addition & 0 deletions testdata/object_comp_try_iterate_over_string.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ [a]: 0 for a in 'b' }
Empty file.