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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ libjsonnet++.so.$(VERSION): $(LIB_CPP_OBJ)

# std.jsonnet.h is a generated file so the first build will fail if it isn't
# specified as an explicit dependency (after that, deps are known from compiler output)
core/desugarer.cpp: core/std.jsonnet.h
.makebuild/core/desugarer.cpp.o: core/std.jsonnet.h

# Encode standard library for embedding in C
.makebuild/stdlib/to_c_array: stdlib/to_c_array.cpp
Expand Down
14 changes: 12 additions & 2 deletions stdlib/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,19 @@ limitations under the License.
if !std.isFunction(func) then
error ('std.flatMap first param must be function, got ' + std.type(func))
else if std.isArray(arr) then
std.flattenArrays(std.makeArray(std.length(arr), function(i) func(arr[i])))
std.flattenArrays(std.makeArray(
std.length(arr), function(i)
local o = func(arr[i]);
if std.isArray(o) then o else
error ('std.flatMap on arrays, provided function must return an array, got ' + std.type(o))
))
else if std.isString(arr) then
std.join('', std.makeArray(std.length(arr), function(i) func(arr[i])))
std.join('', std.makeArray(
std.length(arr), function(i)
local o = func(arr[i]);
if std.isString(o) then o else
error ('std.flatMap on strings, provided function must return a string, got ' + std.type(o))
))
else error ('std.flatMap second param must be array / string, got ' + std.type(arr)),

join(sep, arr)::
Expand Down
1 change: 1 addition & 0 deletions test_suite/error.flatMap_array_typecheck.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.flatMap(function(x) x, ['a', 'b', 'c'])
11 changes: 11 additions & 0 deletions test_suite/error.flatMap_array_typecheck.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
RUNTIME ERROR: std.flatMap on arrays, provided function must return an array, got string
std.jsonnet:<stdlib_position_redacted> thunk <array_element>
std.jsonnet:<stdlib_position_redacted> thunk <b>
std.jsonnet:<stdlib_position_redacted> function <func>
std.jsonnet:<stdlib_position_redacted> thunk <running>
std.jsonnet:<stdlib_position_redacted> function <aux>
std.jsonnet:<stdlib_position_redacted> function <aux>
std.jsonnet:<stdlib_position_redacted> function <anonymous>
std.jsonnet:<stdlib_position_redacted> function <anonymous>
std.jsonnet:(302:7)-(307:9) function <anonymous>
error.flatMap_array_typecheck.jsonnet:1:1-44
1 change: 1 addition & 0 deletions test_suite/error.flatMap_seq_typecheck.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.flatMap(function(x) [x], { a: 1, b: 2, c: 3 })
3 changes: 3 additions & 0 deletions test_suite/error.flatMap_seq_typecheck.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RUNTIME ERROR: std.flatMap second param must be array / string, got object
std.jsonnet:<stdlib_position_redacted> function <anonymous>
error.flatMap_seq_typecheck.jsonnet:1:1-51
1 change: 1 addition & 0 deletions test_suite/error.flatMap_string_typecheck.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.flatMap(function(x) std.codepoint(x), 'abc')
4 changes: 4 additions & 0 deletions test_suite/error.flatMap_string_typecheck.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RUNTIME ERROR: std.flatMap on strings, provided function must return a string, got number
std.jsonnet:<stdlib_position_redacted> thunk <array_element>
std.jsonnet:(309:7)-(314:9) function <anonymous>
error.flatMap_string_typecheck.jsonnet:1:1-49