Skip to content

Commit

Permalink
docs(examples): array group
Browse files Browse the repository at this point in the history
  • Loading branch information
jshlbrd committed Jan 4, 2024
1 parent 7219a98 commit ab5c5f7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 42 deletions.
28 changes: 28 additions & 0 deletions examples/build/config/transform/array/group/config.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This example groups an array of arrays into an array of objects
// based on index and configured keys.
local sub = import '../../../../../../build/config/substation.libsonnet';

local files_key = 'meta files';

{
concurrency: 1,
transforms: [
// This example sends data to stdout at each step to iteratively show
// how the data is transformed.
sub.tf.send.stdout(),
// Copy the object to metadata, where it is grouped.
sub.tf.obj.cp({ object: { target_key: files_key } }),
// Elements from the file_name array are transformed and derived file extensions
// are added to a new array.
sub.tf.meta.for_each({
object: { source_key: sub.helpers.object.get_element(files_key, 'file_name'), target_key: sub.helpers.object.append(files_key, 'file_extension') },
transform: sub.tf.string.capture(settings={ pattern: '\\.([^\\.]+)$' }),
}),
// The arrays grouped into an array of objects, then copied to the message's data field.
// For example:
//
// [{name: name1, type: type1, size: size1, extension: extension1}, {name: name2, type: type2, size: size2, extension: extension2}]
sub.tf.object.cp({ object: { source_key: files_key + '|@group' } }),
sub.tf.send.stdout(),
],
}
1 change: 1 addition & 0 deletions examples/build/config/transform/array/group/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"file_name":["foo.txt","bar.html"],"file_type":["text/plain","text/html"],"file_size":[100,500]}
41 changes: 0 additions & 41 deletions examples/build/config/transform/array/to_object/config.jsonnet

This file was deleted.

1 change: 0 additions & 1 deletion examples/build/config/transform/array/to_object/data.json

This file was deleted.

0 comments on commit ab5c5f7

Please sign in to comment.