Skip to content

Commit

Permalink
fix(core): JQ filters now allow object / array destructuring after do…
Browse files Browse the repository at this point in the history
…ing its thing

closes #1748
  • Loading branch information
brian-mulier-p committed Jul 13, 2023
1 parent 44ecc27 commit 0e65692
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package io.kestra.core.runners.pebble.filters;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.NumericNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.fasterxml.jackson.databind.node.*;
import io.kestra.core.serializers.JacksonMapper;
import io.pebbletemplates.pebble.error.PebbleException;
import io.pebbletemplates.pebble.extension.Filter;
import io.pebbletemplates.pebble.template.EvaluationContext;
import io.pebbletemplates.pebble.template.PebbleTemplate;
import io.kestra.core.serializers.JacksonMapper;
import net.thisptr.jackson.jq.BuiltinFunctionLoader;
import net.thisptr.jackson.jq.JsonQuery;
import net.thisptr.jackson.jq.Scope;
Expand Down Expand Up @@ -71,17 +68,21 @@ public Object apply(Object input, Map<String, Object> args, PebbleTemplate self,
out.add(v.numberValue());
} else if (v instanceof BooleanNode) {
out.add(v.booleanValue());
} else if (v instanceof ObjectNode) {
out.add(JacksonMapper.ofJson().convertValue(v, Map.class));
} else if (v instanceof ArrayNode) {
out.add(JacksonMapper.ofJson().convertValue(v, List.class));
} else {
out.add(v);
}
});
} catch (Exception e) {
throw new Exception("Failed to resolve JQ expression '" + pattern + "' and value '" + input + "'", e);
throw new Exception("Failed to resolve JQ expression '" + pattern + "' and value '" + input + "'", e);
}

return out;
} catch (Exception e) {
throw new PebbleException(e, "Unable to parse jq value '" + input + "' with type '" + input.getClass().getName() + "'", lineNumber, self.getName());
throw new PebbleException(e, "Unable to parse jq value '" + input + "' with type '" + input.getClass().getName() + "'", lineNumber, self.getName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.kestra.core.exceptions.IllegalVariableEvaluationException;
import io.kestra.core.runners.VariableRenderer;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

import java.time.ZoneId;
Expand All @@ -12,7 +13,6 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import jakarta.inject.Inject;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
Expand Down Expand Up @@ -128,4 +128,20 @@ void typed() throws IllegalVariableEvaluationException {
assertThat(variableRenderer.render("{{ vars | jq(\".bool\") | first | className }}", vars), is("java.lang.Boolean"));
assertThat(variableRenderer.render("{{ vars | jq(\".null\") | first | className }}", vars), is(""));
}

@Test
void object() throws IllegalVariableEvaluationException {
ImmutableMap<String, Object> vars = ImmutableMap.of(
"vars", ImmutableMap.of(
"object", Map.of("key", "value"),
"array", new String[]{"arrayValue"}
)
);

String render = variableRenderer.render("{% set object = vars | jq(\".object\") %}{{object[0].key}}", vars);
assertThat(render, is("value"));

render = variableRenderer.render("{% set array = vars | jq(\".array\") %}{{array[0][0]}}", vars);
assertThat(render, is("arrayValue"));
}
}

0 comments on commit 0e65692

Please sign in to comment.