mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(stepfunctions): ResultSelector, Pass Parameters, ArrayContains, wildcard projection; fix IsPresent for absent paths (#1558)
* feat(stepfunctions): apply ResultSelector, add States.ArrayContains and wildcard path projection Three JSONPath/intrinsic features that AslExecutor was missing: - ResultSelector is now applied to a Task's result (before ResultPath), the same template resolution used for Parameters. Previously it was accepted but silently ignored, so the raw result flowed through unchanged. - $.X.*.Y wildcard projection in path resolution: a * segment projects the remainder of the path over each array element and collects the results (e.g. $.Regions.*.RegionName). - States.ArrayContains(array, value) intrinsic. These are used together by orchestration that lists results and validates an input against them. Unit-tested for projection, ArrayContains, and ResultSelector evaluation; existing Step Functions suites unchanged. * fix(stepfunctions): apply Parameters in Pass states A Pass state's Parameters (with intrinsics) were accepted but never applied — the effective input flowed through unchanged. Apply Parameters to produce the Pass result (a static Result still overrides), so transforms like States.ArrayContains in a Pass state take effect. Integration-tested. * fix(stepfunctions): IsPresent reports an absent path as not present resolvePath collapses an absent path to a null node, so the Choice IsPresent comparator (which only checked isMissingNode) reported absent fields as present. Treat null and missing alike. Integration-tested for absent vs present fields. * feat(stepfunctions): bracket [*] wildcard, ResultSelector on Map/Parallel, present-null Address review feedback on #1558: - Support the AWS bracket wildcard form ($.Regions[*].RegionName) and root-level brackets ($[*][*]) by normalizing bracket notation into path segments; multi-level wildcards now flatten, so $[*][*] flattens an array of arrays. The dot form ($.Regions.*.RegionName) keeps working. - Apply ResultSelector to Map and Parallel states (not just Task), before ResultPath merges the result in. - IsPresent now treats an explicit null value as present: resolvePathNode preserves the missing-vs-null distinction that resolvePath collapses. * fix(stepfunctions): keep nulls in projections, validate ArrayContains arg Address greptile review on #1558: - Wildcard projection now keeps explicit null values and omits only missing matches, so $[*].field over [{"field":null},{"field":"x"}] yields [null,"x"] (consistent with treating null as a present value). - States.ArrayContains throws States.Runtime when its first argument is not an array, matching AWS and States.ArrayLength, instead of silently returning false. - Documented the dotted-field-key limitation of path segment splitting. * test(stepfunctions): update AslExecutor ctor for new main params Current main added CloudFormationQueryHandler, Ec2Service and S3Service to the AslExecutor constructor; pass mocks for them in AslExecutorPathIntrinsicsTest so it compiles against main. * fix(stepfunctions): resolve the $.[0] dot-bracket root array-index form splitPathSegments turned "$.[0]" into "..0" (the [0]->.0 rewrite stacked on the existing leading dot), leaving a spurious empty segment after the split, so the path resolved to null. "$[0]" already worked. Collapse consecutive dots so both the dot-bracket ($.[0]) and no-dot ($[0]) root array-index forms resolve. EventBridge Pipes batch unwrapping (InputPath "$.[0]") relies on this. Covered by AslExecutorPathIntrinsicsTest root-array-index cases.
This commit is contained in:
1 parent
b87955b630
commit
e0a1d027b9
5 files changed
+593
-23
No files matched your search
@@ -22,6 +22,7 @@ import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.MissingNode;
|
||||
import com.fasterxml.jackson.databind.node.NullNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.quarkus.arc.Arc;
|
||||
@@ -288,11 +289,14 @@ public class AslExecutor {
|
||||
|
||||
JsonNode effectiveInput = applyInputPath(stateDef, input);
|
||||
|
||||
JsonNode result;
|
||||
// Pass states transform their input through Parameters (with intrinsics), then a static
|
||||
// Result overrides if present.
|
||||
JsonNode result = effectiveInput;
|
||||
if (stateDef.has("Parameters")) {
|
||||
result = resolveParameters(stateDef.get("Parameters"), effectiveInput, context);
|
||||
}
|
||||
if (stateDef.has("Result")) {
|
||||
result = stateDef.get("Result");
|
||||
} else {
|
||||
result = effectiveInput;
|
||||
}
|
||||
|
||||
JsonNode output = mergeResult(stateDef, input, result);
|
||||
@@ -343,6 +347,10 @@ public class AslExecutor {
|
||||
JsonNode output = applyJsonataOutput(stateDef, input, taskResult, context);
|
||||
return new StateResult(output, stateDef.path("Next").asText(null));
|
||||
} else {
|
||||
// ResultSelector transforms the raw result before ResultPath merges it into the state input.
|
||||
if (stateDef.has("ResultSelector")) {
|
||||
taskResult = resolveParameters(stateDef.get("ResultSelector"), taskResult, context);
|
||||
}
|
||||
JsonNode output = mergeResult(stateDef, input, taskResult);
|
||||
output = applyOutputPath(stateDef, input, output);
|
||||
return new StateResult(output, stateDef.path("Next").asText(null));
|
||||
@@ -955,7 +963,10 @@ public class AslExecutor {
|
||||
}
|
||||
if (rule.has("IsPresent")) {
|
||||
boolean expectPresent = rule.get("IsPresent").asBoolean();
|
||||
return !value.isMissingNode() == expectPresent;
|
||||
// A field that exists with an explicit null value still counts as present in AWS, so
|
||||
// resolve without collapsing missing into null: only a truly absent path is "not present".
|
||||
boolean present = !resolvePathNode(variable, input).isMissingNode();
|
||||
return present == expectPresent;
|
||||
}
|
||||
if (rule.has("IsString")) {
|
||||
return value.isTextual() == rule.get("IsString").asBoolean();
|
||||
@@ -1065,7 +1076,11 @@ public class AslExecutor {
|
||||
return new StateResult(output, stateDef.path("Next").asText(null));
|
||||
}
|
||||
|
||||
JsonNode output = mergeResult(stateDef, input, results);
|
||||
// ResultSelector transforms the raw branch results before ResultPath merges them in.
|
||||
JsonNode selected = stateDef.has("ResultSelector")
|
||||
? resolveParameters(stateDef.get("ResultSelector"), results, context)
|
||||
: results;
|
||||
JsonNode output = mergeResult(stateDef, input, selected);
|
||||
output = applyOutputPath(stateDef, input, output);
|
||||
return new StateResult(output, stateDef.path("Next").asText(null));
|
||||
}
|
||||
@@ -1127,7 +1142,11 @@ public class AslExecutor {
|
||||
return new StateResult(output, stateDef.path("Next").asText(null));
|
||||
}
|
||||
|
||||
JsonNode output = mergeResult(stateDef, input, results);
|
||||
// ResultSelector transforms the raw iteration results before ResultPath merges them in.
|
||||
JsonNode selected = stateDef.has("ResultSelector")
|
||||
? resolveParameters(stateDef.get("ResultSelector"), results, context)
|
||||
: results;
|
||||
JsonNode output = mergeResult(stateDef, input, selected);
|
||||
output = applyOutputPath(stateDef, input, output);
|
||||
return new StateResult(output, stateDef.path("Next").asText(null));
|
||||
}
|
||||
@@ -1280,7 +1299,7 @@ public class AslExecutor {
|
||||
return resolvePath(path, output);
|
||||
}
|
||||
|
||||
private JsonNode resolveParameters(JsonNode parameters, JsonNode input, JsonNode context) throws Exception {
|
||||
JsonNode resolveParameters(JsonNode parameters, JsonNode input, JsonNode context) throws Exception {
|
||||
if (parameters.isObject()) {
|
||||
ObjectNode resolved = objectMapper.createObjectNode();
|
||||
Iterator<Map.Entry<String, JsonNode>> fields = parameters.fields();
|
||||
@@ -1311,39 +1330,116 @@ public class AslExecutor {
|
||||
}
|
||||
|
||||
JsonNode resolvePath(String path, JsonNode root) {
|
||||
JsonNode node = resolvePathNode(path, root);
|
||||
// Most callers do not distinguish an absent path from an explicit null; collapse both to null.
|
||||
return node.isMissingNode() ? NullNode.getInstance() : node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a reference path while preserving the distinction between an explicit null value
|
||||
* (returns a {@link NullNode}) and a missing/absent path (returns a {@link MissingNode}).
|
||||
* {@link #resolvePath} collapses both to null; only callers that care about presence
|
||||
* (e.g. {@code IsPresent}) should use this variant.
|
||||
*/
|
||||
JsonNode resolvePathNode(String path, JsonNode root) {
|
||||
if (path == null || "$".equals(path)) {
|
||||
return root;
|
||||
}
|
||||
if (path.startsWith("States.")) {
|
||||
return evaluateIntrinsic(path, root);
|
||||
}
|
||||
if (!path.startsWith("$.")) {
|
||||
return NullNode.getInstance();
|
||||
// Support dotted ($.a.b) and root-bracket ($[*], $[0]) forms; anything else is unsupported.
|
||||
if (!path.startsWith("$.") && !path.startsWith("$[")) {
|
||||
return MissingNode.getInstance();
|
||||
}
|
||||
String[] parts = path.substring(2).split("\\.");
|
||||
JsonNode current = root;
|
||||
for (String part : parts) {
|
||||
if (current == null || current.isMissingNode()) {
|
||||
return NullNode.getInstance();
|
||||
return walkPath(splitPathSegments(path), 0, root);
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits a reference path into segments, normalizing bracket notation into dot segments so the
|
||||
* AWS bracket forms reduce to the same walk as the dot forms:
|
||||
* {@code $.Regions[*].RegionName} and {@code $.Regions.*.RegionName} both yield
|
||||
* {@code [Regions, *, RegionName]}, and {@code $[*][*]} yields {@code [*, *]}.
|
||||
*
|
||||
* <p>Limitation: every literal dot is treated as a segment separator, so a field name that
|
||||
* itself contains a dot is mis-split. AWS's bracket-quoted escape hatch ({@code $.a['b.c']})
|
||||
* is not supported; this matches the prior behavior and is rare in ASL reference paths.
|
||||
*/
|
||||
private String[] splitPathSegments(String path) {
|
||||
String rest = path.substring(1); // drop leading '$'
|
||||
rest = rest.replaceAll("\\[(\\*|\\d+)]", ".$1"); // [*] -> .*, [0] -> .0
|
||||
rest = rest.replaceAll("\\.{2,}", "."); // collapse ".[0]" -> "..0" -> ".0"
|
||||
if (rest.startsWith(".")) {
|
||||
rest = rest.substring(1);
|
||||
}
|
||||
return rest.isEmpty() ? new String[0] : rest.split("\\.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Walks the remaining path segments from {@code idx}. A {@code *} segment projects the rest of
|
||||
* the path over each element of the current array and collects the results into an array
|
||||
* (e.g. {@code $.Regions[*].RegionName}). When the projected suffix contains a further wildcard,
|
||||
* the nested projections are flattened one level so {@code $[*][*]} flattens an array of arrays.
|
||||
* A purely numeric segment indexes into an array (e.g. {@code $.items[0]}).
|
||||
*/
|
||||
private JsonNode walkPath(String[] parts, int idx, JsonNode current) {
|
||||
for (int i = idx; i < parts.length; i++) {
|
||||
if (current == null || current.isMissingNode() || current.isNull()) {
|
||||
return MissingNode.getInstance();
|
||||
}
|
||||
// Handle array index notation like field[0]
|
||||
if (part.contains("[")) {
|
||||
int bracketOpen = part.indexOf('[');
|
||||
int bracketClose = part.indexOf(']');
|
||||
String fieldName = part.substring(0, bracketOpen);
|
||||
int index = Integer.parseInt(part.substring(bracketOpen + 1, bracketClose));
|
||||
current = current.path(fieldName).path(index);
|
||||
String part = parts[i];
|
||||
if ("*".equals(part)) {
|
||||
if (!current.isArray()) {
|
||||
return MissingNode.getInstance();
|
||||
}
|
||||
boolean flattenSub = false;
|
||||
for (int j = i + 1; j < parts.length; j++) {
|
||||
if ("*".equals(parts[j])) {
|
||||
flattenSub = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
ArrayNode projected = objectMapper.createArrayNode();
|
||||
for (JsonNode element : current) {
|
||||
JsonNode value = walkPath(parts, i + 1, element);
|
||||
// Only absent matches are skipped; an explicit null is a real value and is kept,
|
||||
// so $[*].field over [{"field":null},{"field":"x"}] yields [null,"x"].
|
||||
if (value == null || value.isMissingNode()) {
|
||||
continue;
|
||||
}
|
||||
if (flattenSub && value.isArray()) {
|
||||
value.forEach(projected::add);
|
||||
} else {
|
||||
projected.add(value);
|
||||
}
|
||||
}
|
||||
return projected;
|
||||
}
|
||||
if (current.isArray() && isArrayIndex(part)) {
|
||||
current = current.path(Integer.parseInt(part));
|
||||
} else {
|
||||
current = current.path(part);
|
||||
}
|
||||
}
|
||||
return current.isMissingNode() ? NullNode.getInstance() : current;
|
||||
return current;
|
||||
}
|
||||
|
||||
private static boolean isArrayIndex(String segment) {
|
||||
if (segment.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < segment.length(); i++) {
|
||||
if (!Character.isDigit(segment.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluate a JSONPath-mode intrinsic function (States.*).
|
||||
* Supports: States.StringToJson, States.JsonToString, States.Format,
|
||||
* States.Array, States.ArrayLength, States.MathAdd, States.UUID.
|
||||
* States.Array, States.ArrayLength, States.ArrayContains, States.MathAdd, States.UUID.
|
||||
* Throws FailStateException("States.Runtime") for unrecognized functions.
|
||||
*/
|
||||
private JsonNode evaluateIntrinsic(String expr, JsonNode root) {
|
||||
@@ -1419,6 +1515,28 @@ public class AslExecutor {
|
||||
JsonNode b = resolveIntrinsicArg(parts.get(1).trim(), root);
|
||||
yield objectMapper.getNodeFactory().numberNode(a.asLong() + b.asLong());
|
||||
}
|
||||
case "States.ArrayContains" -> {
|
||||
List<String> parts = splitIntrinsicArgs(argsStr);
|
||||
if (parts.size() != 2) {
|
||||
throw new FailStateException("States.Runtime",
|
||||
"States.ArrayContains requires exactly 2 arguments");
|
||||
}
|
||||
JsonNode array = resolveIntrinsicArg(parts.get(0).trim(), root);
|
||||
JsonNode value = resolveIntrinsicArg(parts.get(1).trim(), root);
|
||||
if (!array.isArray()) {
|
||||
// AWS throws rather than silently returning false, matching States.ArrayLength.
|
||||
throw new FailStateException("States.Runtime",
|
||||
"States.ArrayContains: first argument must be an array");
|
||||
}
|
||||
boolean contains = false;
|
||||
for (JsonNode element : array) {
|
||||
if (element.equals(value)) {
|
||||
contains = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
yield objectMapper.getNodeFactory().booleanNode(contains);
|
||||
}
|
||||
case "States.UUID" -> {
|
||||
yield objectMapper.getNodeFactory().textNode(java.util.UUID.randomUUID().toString());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user