mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
205 lines
8.6 KiB
Java
205 lines
8.6 KiB
Java
package io.floci.aws.services.stepfunctions;
|
|
|
|
import com.dashjoin.jsonata.Parser;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.LinkedHashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
import static io.floci.aws.services.stepfunctions.JsonataSymbolReader.read;
|
|
|
|
/**
|
|
* Parses a Step Functions JSONata expression once and reports either the message the parser refused
|
|
* it with, or every reference it makes against the top-level context.
|
|
*
|
|
* <p>A Step Functions expression is evaluated with no context item: the input arrives as
|
|
* {@code $states.input} and workflow variables as {@code $name}. A path that starts from neither
|
|
* reads a context item that does not exist, and real AWS refuses the whole definition at
|
|
* {@code CreateStateMachine} time with
|
|
* {@code UNSUPPORTED_JSONATA_EXPRESSION: Reference to 'phone' at the top level is not supported.}
|
|
*
|
|
* <p>Only the first step of a path is evaluated against the top-level context. Every later step,
|
|
* every predicate, every sort term and every grouping expression on that path runs against the
|
|
* value the step before it produced, so a name there is legal and this walk never descends into
|
|
* one. A grouping expression on a bare variable is different: AWS reads its keys and values from
|
|
* the top-level context, so {@code $p{$string(user_id): $max(id)}} names {@code user_id} and
|
|
* {@code id}. A lambda body, on the other hand, keeps the context of the expression that defines
|
|
* it, so {@code $map($states.input.a, function($x){ b })} does name {@code b} at the top level.
|
|
* All of these are measured against AWS in {@code JsonataTopLevelReferencesTest}.
|
|
*
|
|
* <p>Nonpublic parse-tree fields are read through {@link JsonataSymbolReader}.
|
|
*/
|
|
final class JsonataTopLevelReferences {
|
|
|
|
/** The name AWS prints for {@code $}, the context item itself. */
|
|
private static final String CONTEXT_ITEM = "$";
|
|
|
|
/** The pseudo-reference reported for {@code $$}, AWS's separate name rule. */
|
|
static final String ROOT_REFERENCE = "$$";
|
|
|
|
/**
|
|
* The pseudo-reference reported for a {@code $states.errorOutput} path, wherever it starts.
|
|
* Not a top-level context read like the others: {@code $states} is always in scope, but the
|
|
* {@code errorOutput} field only exists on it inside a {@code Catch} entry's own {@code Output}
|
|
* and {@code Assign}, and only the caller knows whether the walk is there.
|
|
*/
|
|
static final String STATES_ERROR_OUTPUT = "$states.errorOutput";
|
|
|
|
private JsonataTopLevelReferences() {
|
|
}
|
|
|
|
/**
|
|
* What parsing {@code expression} once finds: either the parser's own message, unchanged
|
|
* (AWS's {@code INVALID_JSONATA_EXPRESSION} refuses it with the same sentence, its
|
|
* {@code S0xxx: } code prefix stripped, and {@link com.dashjoin.jsonata.JException#getMessage()}
|
|
* never carries that prefix), or the distinct top-level references the expression makes, in
|
|
* the order they are written.
|
|
*/
|
|
record Analysis(List<String> topLevelReferences, String parseError) {
|
|
static Analysis parseError(String message) {
|
|
return new Analysis(List.of(), message);
|
|
}
|
|
|
|
static Analysis references(List<String> names) {
|
|
return new Analysis(names, null);
|
|
}
|
|
}
|
|
|
|
static Analysis analyze(String expression) {
|
|
Parser.Symbol root;
|
|
try {
|
|
root = new Parser().parse(expression);
|
|
} catch (Exception e) {
|
|
return Analysis.parseError(e.getMessage());
|
|
}
|
|
Set<String> names = new LinkedHashSet<>();
|
|
collect(root, names);
|
|
return Analysis.references(new ArrayList<>(names));
|
|
}
|
|
|
|
private static void collect(Parser.Symbol node, Set<String> names) {
|
|
String type = node == null ? null : (String) read(node, "type");
|
|
if (type == null) {
|
|
return;
|
|
}
|
|
switch (type) {
|
|
case "path" -> {
|
|
collect(first(node, "steps"), names);
|
|
if (isStatesErrorOutputPath(node)) {
|
|
names.add(STATES_ERROR_OUTPUT);
|
|
}
|
|
}
|
|
case "name" -> names.add(String.valueOf(read(node, "value")));
|
|
case "variable" -> {
|
|
collectContextItem(node, names);
|
|
if (node.group != null) {
|
|
collectEachPair(node.group.lhsObject, names);
|
|
}
|
|
}
|
|
case "unary" -> collectUnary(node, names);
|
|
case "binary", "apply" -> {
|
|
collect(child(node, "lhs"), names);
|
|
collect(child(node, "rhs"), names);
|
|
}
|
|
case "function", "partial" -> {
|
|
collect(child(node, "procedure"), names);
|
|
collectEach(children(node, "arguments"), names);
|
|
}
|
|
case "lambda" -> collect(child(node, "body"), names);
|
|
case "condition" -> {
|
|
collect(child(node, "condition"), names);
|
|
collect(child(node, "then"), names);
|
|
collect(child(node, "_else"), names);
|
|
}
|
|
case "block" -> collectEach(children(node, "expressions"), names);
|
|
case "bind" -> collect(child(node, "rhs"), names);
|
|
default -> {
|
|
// literal, number, string, regex, descendant, transform, sort, filter and group
|
|
// never read the top-level context.
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@code $} carries the empty name and is the context item AWS refuses. {@code $$} carries the
|
|
* name {@code "$"} and AWS refuses it too, under a different message. Every other variable is a
|
|
* workflow variable or a function, and AWS accepts an undefined one.
|
|
*/
|
|
private static void collectContextItem(Parser.Symbol variable, Set<String> names) {
|
|
String value = String.valueOf(read(variable, "value"));
|
|
if (value.isEmpty()) {
|
|
names.add(CONTEXT_ITEM);
|
|
} else if (CONTEXT_ITEM.equals(value)) {
|
|
// "$$" parses as a variable named "$": the sigil is consumed twice, once as the
|
|
// variable marker and once as its name.
|
|
names.add(ROOT_REFERENCE);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Whether {@code path}'s first step is {@code $states} and its second is {@code errorOutput}.
|
|
* Only those two steps matter: whatever follows ({@code $states.errorOutput.Cause}) still reads
|
|
* the same field on {@code $states} first.
|
|
*/
|
|
private static boolean isStatesErrorOutputPath(Parser.Symbol path) {
|
|
List<Parser.Symbol> steps = children(path, "steps");
|
|
if (steps == null || steps.size() < 2) {
|
|
return false;
|
|
}
|
|
Parser.Symbol first = steps.get(0);
|
|
Parser.Symbol second = steps.get(1);
|
|
return "variable".equals(read(first, "type")) && "states".equals(read(first, "value"))
|
|
&& "name".equals(read(second, "type")) && "errorOutput".equals(read(second, "value"));
|
|
}
|
|
|
|
/**
|
|
* The array constructor carries its bracket as a {@link Character} and every other unary
|
|
* operator carries it as a {@link String}, so the operator is read through
|
|
* {@link String#valueOf(Object)} rather than compared against the field directly. The object
|
|
* constructor keeps its key/value pairs in the public {@code lhsObject}, and AWS reads a key
|
|
* at the top level too: {@code {aaa: 1}} names {@code aaa}.
|
|
*/
|
|
private static void collectUnary(Parser.Symbol unary, Set<String> names) {
|
|
switch (String.valueOf(read(unary, "value"))) {
|
|
case "[" -> collectEach(children(unary, "expressions"), names);
|
|
case "{" -> collectEachPair(unary.lhsObject, names);
|
|
default -> collect(child(unary, "expression"), names);
|
|
}
|
|
}
|
|
|
|
private static void collectEach(List<Parser.Symbol> nodes, Set<String> names) {
|
|
if (nodes == null) {
|
|
return;
|
|
}
|
|
for (Parser.Symbol node : nodes) {
|
|
collect(node, names);
|
|
}
|
|
}
|
|
|
|
private static void collectEachPair(List<Parser.Symbol[]> pairs, Set<String> names) {
|
|
if (pairs == null) {
|
|
return;
|
|
}
|
|
for (Parser.Symbol[] pair : pairs) {
|
|
collect(pair[0], names);
|
|
collect(pair[1], names);
|
|
}
|
|
}
|
|
|
|
private static Parser.Symbol child(Parser.Symbol node, String field) {
|
|
return (Parser.Symbol) read(node, field);
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
private static List<Parser.Symbol> children(Parser.Symbol node, String field) {
|
|
return (List<Parser.Symbol>) read(node, field);
|
|
}
|
|
|
|
private static Parser.Symbol first(Parser.Symbol node, String field) {
|
|
List<Parser.Symbol> nodes = children(node, field);
|
|
return nodes == null || nodes.isEmpty() ? null : nodes.get(0);
|
|
}
|
|
|
|
}
|