Document updated on Jul 24, 2020
Since | v0.9 |
---|---|
Namespace | github.com/devopsfaith/krakend/proxy |
Scope | backend , endpoint |
Source | krakend/flatmap |
The flatmap middleware allows you to manipulate collections (or arrays, or lists, you name it). While the basic manipulation operations allow you to work directly with objects, the collections require a different approach: the flatmap component.
When working with lists, KrakenD needs to flatten and expand array structures to objects to operate with them, and vice versa. This process is automatically done by the flatmap component, letting you concentrate only on the type of operation you want to execute.
You can manipulate collections at two different stages:
backend
section)endpoint
section, starting at KrakenD 1.2)You can do simultaneous combinations to output the desired result. For instance, declare an endpoint with three backends that apply transformations independently and a final change within the endpoint after merging the three.
There are different types of operations you can do:
mapping
and allow
)deny
)Use a basic data manipulation operation such as target
, deny
or allow
whenever it fits as their computational cost is lower.
The flatmap component is not as a general solution for all objects, and makes sense only when you need to manipulate collections.
Depending on the stage you want to do the manipulation, you will need an extra_config
configuration inside your endpoint
or backend
section. For both cases, the namespace is github.com/devopsfaith/krakend/proxy
.
The component structure with three operations would be as follows:
"extra_config": {
"github.com/devopsfaith/krakend/proxy": {
"flatmap_filter": [
{
"type": "move",
"args": ["target_in_collection", "destination_in_collection"]
},
{
"type": "del",
"args": ["target_in_collection"]
},
{
"type": "append",
"args": ["collection_to_append", "returned_collection"]
}
]
}
}
flatmap_filter
(list) The list of operations to execute sequentially (top down). Every operation is defined with an object containing two properties:type
(string) One of the recognized operation typesargs
(list) The arguments passed to the operation.The types of operations are defined as follows:
"type": "move"
"args": ["target_in_collection", "destination_in_collection"]
"type": "del"
"args": ["target_in_collection_to_delete", "another_collection_to_delete", "..."]
"type": "append"
"args": ["collection_to_append", "returned_collection"]
The format of the arguments (args
) to proceed with the operation is very simple. In short, object nesting is represented with dots, while the index of an array is represented with a number. Or all matching items with wildcards. So:
.
indicates a new array nesting level*
matches any key (property name, collection key name, or index)number
identifies the Nth-1 member of a collection, being 0
its first item.Operations always apply to ** the last item** in the arguments. For instance, a deletion of a.b.c
deletes c
but leaves a.b
in the response.
We are going to use an elementary JSON structure as an example of data representation. See below:
{
"a": [
{
"b1": [
{
"c": 1,
"d": "foo"
},
{
"c": 2,
"d": "bar"
}
],
"b2": true
},
{
"b1": [
{
"c": 3,
"d": "vaz"
}
]
}
]
}
Notice from this example that…
a
and b1
contain arrays ([...]
) with objects inside.b2
, c
and d
are not arraysa
is an array ("a": []
) we need to use the flatmap component. If it were an object ("a": {}
) we would use deny or allowNow that we are familiar with the structure let’s represent same values:
Notation | Value |
---|---|
a | The content of a: [{"b1": [{"c": 1,"d": "foo"},{"c": 2,"d": "bar"}],"b2": true}, {"b1": [{"c": 3,"d": "vaz"}]}] |
a.1 | Second object of a key: {"b1": [ { "c": 3, "d": "vaz" } ]} (first objects starts at 0) |
a.0.b1.0.d | foo |
a.1.b1.0.d | vaz |
a.*.b1.*.d | 3 matches of d in this path: foo , bar , vaz |
a.*.*.*.d | 3 matches of d in this path: foo , bar , vaz |
Some individual operations on the example structure above:
Target | Destination | Correct? | Comments |
---|---|---|---|
"a.*.b1.*.c" | "a.*.b1.*.d" | Rename c to d | |
"a.*.b1.*.c" | "a.*.c" | Missing level | |
"a.b1.c" | "c" | Missing array after a | |
"a.0.b1.0.c" | "c" | Extract only c from the first and first items | |
"a.*.b1.c" | "c" | Incorrect target, b1 has an array surrounding c | |
"a.*.b1.c" | "a.*.b1.*.d.*.e" | Incorrect target, b1 has an array surrounding c | |
"a.*.b1.*.c" | "a.*.b1.*.c.d.e.f.g" | Add additional levels | |
"a.*.b1.*.c" | "a.*.x.*.c" | Incorrect, renaming to an element x that is not in the last position | |
"a.*.b1.*.c" | "a.*.x.*.c.d.e.f.g" | Incorrect, renaming to an element x that is not in the last position | |
"a.*.b1.*.c" | "a.*.b1.*.d.*.e" | Incorrect, destination path has more wildcards than source path |
The following example demonstrates how to modify a collection doing these operations:
"extra_config": {
"github.com/devopsfaith/krakend/proxy": {
"flatmap_filter": [
{
"type": "append",
"args": ["kindergarten", "schools"]
},
{
"type": "move",
"args": ["schools.42.students", "alumni"]
},
{
"type": "del",
"args": ["schools"]
},
{
"type": "del",
"args": ["alumni.*.password"]
},
{
"type": "move",
"args": ["alumni.*.PK_ID", "alumni.*.id"]
}
]
}
}
What did we do here?
There is a sequence of 4 operations to:
kindergarten
and append them to the students
collection.students
of the 43rd
school (array starts at 0) and put them under a new property alumni
password
inside the arrayPK_ID
to id
For more examples, see the test file.
When the flatmap filter is enabled, the operations group
and target
keep their functionality, but allow
, deny
, and mapping
are ignored.
The documentation is only a piece of the help you can get! Whether you are looking for Open Source or Enterprise support, see more support channels that can help you.