MCPcopy
hub / github.com/google/go-jsonnet / builtinFlatMap

Function builtinFlatMap

builtins.go:305–346  ·  view source on GitHub ↗
(i *interpreter, funcv, arrv value)

Source from the content-addressed store, hash-verified

303}
304
305func builtinFlatMap(i *interpreter, funcv, arrv value) (value, error) {
306 fun, err := i.getFunction(funcv)
307 if err != nil {
308 return nil, err
309 }
310 switch arrv := arrv.(type) {
311 case *valueArray:
312 num := arrv.length()
313 // Start with capacity of the original array.
314 // This may spare us a few reallocations.
315 // TODO(sbarzowski) verify that it actually helps
316 elems := make([]*cachedThunk, 0, num)
317 for counter := 0; counter < num; counter++ {
318 returnedValue, err := fun.call(i, args(arrv.elements[counter]))
319 if err != nil {
320 return nil, err
321 }
322 returned, err := i.getArray(returnedValue)
323 if err != nil {
324 return nil, err
325 }
326 elems = append(elems, returned.elements...)
327 }
328 return makeValueArray(elems), nil
329 case valueString:
330 var str strings.Builder
331 for _, elem := range arrv.getRunes() {
332 returnedValue, err := fun.call(i, args(readyThunk(makeValueString(string(elem)))))
333 if err != nil {
334 return nil, err
335 }
336 returned, err := i.getString(returnedValue)
337 if err != nil {
338 return nil, err
339 }
340 str.WriteString(returned.getGoString())
341 }
342 return makeValueString(str.String()), nil
343 default:
344 return nil, i.Error("std.flatMap second param must be array / string, got " + arrv.getType().name)
345 }
346}
347
348// builtinFlatMapArray is like builtinFlatMap, but only accepts array as the
349// arrv value. Desugared comprehensions contain a call to this function, rather

Callers 1

builtinFlatMapArrayFunction · 0.85

Calls 14

argsFunction · 0.85
makeValueArrayFunction · 0.85
readyThunkFunction · 0.85
makeValueStringFunction · 0.85
getFunctionMethod · 0.80
callMethod · 0.80
getArrayMethod · 0.80
getStringMethod · 0.80
lengthMethod · 0.65
getRunesMethod · 0.65
getGoStringMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…