jsonnet icon indicating copy to clipboard operation
jsonnet copied to clipboard

std.flatMap behaviour is strange if f returns a string

Open johnbartholomew opened this issue 10 months ago • 0 comments

The docs say:

Apply the given function to every element of arr to form a new array then flatten the result. The argument arr must be an array or a string. If arr is an array, function func must return an array. If arr is a string, function func must return an string.

However, this is not enforced in all combinations. Example:

# Array input, function returns array, behaves as expected:
$ ./jsonnet -e 'std.flatMap(function(x) [x], ["a", "b", "c"])'
[
   "a",
   "b",
   "c"
]

# Array input, function returns a string, behaves strangely:
$ ./jsonnet -e 'std.flatMap(function(x) x, ["a", "b", "c"])'
"[ ]abc"

Noticed while testing https://github.com/google/go-jsonnet/pull/797

johnbartholomew avatar Mar 21 '25 16:03 johnbartholomew