Skip to content

Commit

Permalink
Enhance insert-field-action.kamelet
Browse files Browse the repository at this point in the history
- Support simple language parsed value
- Minor typo fix
  • Loading branch information
christophd committed Sep 15, 2023
1 parent 3dac528 commit 22e057a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion kamelets/insert-field-action.kamelet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ spec:
description: |-
Adds a custom field with a simple language parsed value to the message in transit.
The extract field action expected an application/json content type.
The insert field action expected an application/json content type.
If for example you have an array like '{ "foo":"John", "bar":30 }' and your action has been configured with field as 'element' and value as 'hello', you'll get '{ "foo":"John", "bar":30, "element":"hello" }'
required:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.camel.Exchange;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.Processor;
import org.apache.camel.support.LanguageSupport;

public class InsertField implements Processor {

Expand Down Expand Up @@ -51,15 +52,22 @@ public void process(Exchange ex) throws InvalidPayloadException {
throw new InvalidPayloadException(ex, JsonNode.class);
}

String resolvedValue;
if (LanguageSupport.hasSimpleFunction(value)) {
resolvedValue = ex.getContext().resolveLanguage("simple").createExpression(value).evaluate(ex, String.class);
} else {
resolvedValue = value;
}

switch (body.getNodeType()) {
case ARRAY:
((ArrayNode) body).add(value);
((ArrayNode) body).add(resolvedValue);
break;
case OBJECT:
((ObjectNode) body).put(field, value);
((ObjectNode) body).put(field, resolvedValue);
break;
default:
((ObjectNode) body).put(field, value);
((ObjectNode) body).put(field, resolvedValue);
break;
}

Expand Down

0 comments on commit 22e057a

Please sign in to comment.