Slice
Synopsis
A processor that extracts a portion of an array field by specifying start and end indices. The processor supports negative indexing and automatically handles various array types including strings, integers, and floats.
Schema
- slice:
field: <ident>
start: <number>
end: <number>
target_field: <ident>
description: <text>
if: <script>
ignore_failure: <boolean>
ignore_missing: <boolean>
on_failure: <processor[]>
on_success: <processor[]>
tag: <string>
Configuration
The following fields are used to define the processor:
Field | Required | Default | Description |
---|---|---|---|
field | Y | - | Array field to slice |
start | Y | - | Starting index (inclusive) |
end | Y | - | Ending index (inclusive) |
target_field | N | field | Field to store the sliced array |
description | N | - | Explanatory note |
if | N | - | Condition to run processor |
ignore_failure | N | false | Continue if processor fails |
ignore_missing | N | false | Continue if source field doesn't exist |
on_failure | N | - | See Handling Failures |
on_success | N | - | See Handling Success |
tag | N | - | Identifier |
Details
The processor extracts a contiguous portion of an array using zero-based indexing. Both start and end indices are inclusive, meaning the element at the end index is included in the result.
The processor supports negative indexing, where -1 refers to the last element, -2 to the second-to-last, and so on.
The processor automatically converts different array types ([]string
, []int
, []float64
) to a unified []any
format for processing. If the source field is not an array, the processor will fail unless ignore_failure
is set to true.
Index bounds are automatically normalized to prevent out-of-bounds errors. If start index is greater than end index after normalization, an empty array is returned.
When start index is greater than or equal to array length, an empty array is returned. Similarly, negative indices that extend beyond the array bounds are clamped to valid ranges.
Examples
Basic Array Slicing
Extracting middle elements... |
|
extracts elements from index 2 to 5: |
|
Negative Indexing
Using negative indices... |
|
extracts the last three elements: |
|
First N Elements
Getting first few elements... |
|
extracts first three elements: |
|
Single Element Extraction
Extracting single element... |
|
extracts single element as array: |
|
Mixed Type Arrays
Processing different array types... |
|
handles float arrays: |
|
In-Place Modification
Modifying original field... |
|
replaces original field content: |
|
Out-of-Bounds Handling
Handling invalid indices... |
|
returns empty array for out-of-bounds: |
|