Skip to content

Commit

Permalink
Merge pull request #187 from GetFeedback/improve-insert-field-step
Browse files Browse the repository at this point in the history
Improve insert field step
  • Loading branch information
Stefano Guerrini authored Aug 24, 2022
2 parents a77db8b + fde91e1 commit 1a69d77
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
44 changes: 40 additions & 4 deletions docs/features/transformers.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ Another for the drink:
{
"customer": "Paolo",
"order": {
"type": "food",
"product": "pizza"
"type": "drink",
"product": "cocacola"
}
}
```
Expand Down Expand Up @@ -150,12 +150,35 @@ Wraps the current content in a single field.
## Insert Static Value
Insert a static value in a chosen field of the value object; if the field exists, the action will be skipped, and eventually is possible to configure the step with `overrideIfExists: true` to permit the override.

Let's take as example this record:

```yaml
{
"name": "Paolo",
"email": "[email protected]"
}
```

We want to add a new field `type`, so our record will look like this:

```yaml
{
"name": "Paolo",
"email": "[email protected]",
"type": "customer"
}
```

So, our step configuration will look like this:

```yaml
- name: addNewField
type: dev.vox.platform.kahpp.configuration.transform.InsertStaticFieldTransform
config:
field: newField
value: foo
field: type
value: customer
```

### Insert JSON Value
Expand All @@ -171,6 +194,19 @@ Eventually, we can also add a static JSON value.
```

### Override an existing field

It's also possible to override an existent field.

```yaml
- name: addNewJsonField
type: dev.vox.platform.kahpp.configuration.transform.InsertStaticFieldTransform
config:
field: email
value: '*****'
overrideIfExists: true
```

## Conditional

All transformer steps can be triggered conditionally using the parameter `condition`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ public final class InsertStaticFieldTransform extends AbstractRecordTransform {
@NotBlank private final transient String field;
@NotBlank private transient JsonNode value;
@NotBlank private transient Format format = Format.STRING;
@NotBlank private final boolean overrideIfExist;

public InsertStaticFieldTransform(String name, Map<String, ?> config) {
super(name, config);
this.field = String.format("value.%s", config.get("field").toString());
this.value = getValue(config);
this.overrideIfExist =
config.containsKey("overrideIfExists")
? Boolean.valueOf(config.get("overrideIfExists").toString())
: false;
}

private JsonNode getValue(Map<String, ?> config) {
Expand Down Expand Up @@ -58,7 +63,8 @@ public TransformRecord transform(

// If the field exists, this step won't change it's value
if (jmesPathExpression.search(record.build()) != null
&& !jmesPathExpression.search(record.build()).isNull()) {
&& !jmesPathExpression.search(record.build()).isNull()
&& !overrideIfExist) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(getTypedName() + ": field `" + field + "` is not empty");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.TextNode;
import dev.vox.platform.kahpp.configuration.TransformRecord;
import dev.vox.platform.kahpp.configuration.TransformRecordApplier;
import dev.vox.platform.kahpp.configuration.transform.InsertStaticFieldTransform;
import dev.vox.platform.kahpp.streams.KaHPPRecord;
import io.burt.jmespath.jackson.JacksonRuntime;
Expand All @@ -25,6 +26,7 @@ class InsertStaticFieldTransformTest {
private static final String NEW_KEY = "newKey";
private static final String STEP_NAME = "name";
private static final String FORMAT_FIELD = "format";
private static final String CONFIG_OVERRIDE = "overrideIfExists";

private transient KaHPPRecord record;

Expand Down Expand Up @@ -124,4 +126,35 @@ void shouldThrowException() {
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Failed to parse [[\"foo\", \"bar\"] value to JSON");
}

@Test
void shouldOverrideFieldIfExists() {
KaHPPRecord record =
KaHPPRecord.build(
NullNode.getInstance(),
MAPPER.createObjectNode().put("foo", "bar").put("overrideThis", "oldValue"),
1584352842123L);

final InsertStaticFieldTransform insertStaticFieldTransform =
new InsertStaticFieldTransform(
STEP_NAME,
Map.of(CONFIG_FIELD, "overrideThis", CONFIG_VALUE, NEW_VALUE, CONFIG_OVERRIDE, true));

JacksonRuntime runtime = new JacksonRuntime();
final TransformRecord transformation =
insertStaticFieldTransform.transform(runtime, new MockProcessorContext(), record);

assertThat(transformation.getMutations().size()).isEqualTo(1);
assertThat(transformation.getMutations())
.contains(TransformRecord.JmesPathMutation.pair("@", "value.overrideThis"));
assertThat(transformation.getDataSource()).isEqualTo(TextNode.valueOf(NEW_VALUE));

TransformRecordApplier.apply(runtime, record, transformation);
assertThat(record.build().toString())
.isEqualTo(
"""
{"key":null,"value":{"foo":"bar","overrideThis":"newValue"},"timestamp":1584352842123,"headers":null}
"""
.trim());
}
}

0 comments on commit 1a69d77

Please sign in to comment.