Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(agama): remove check of existing flows #3123

Merged
merged 1 commit into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions agama/engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,13 @@
<artifactId>kryo</artifactId>
<version>5.3.0</version>
</dependency>

<!-- ZIP4J -->
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>2.11.2</version>
</dependency>

<!-- TESTS -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void process() throws IOException {

String error = null, shortError = null;
try {
TranspilationResult result = Transpiler.transpile(qname, map.keySet(), fl.getSource());
TranspilationResult result = Transpiler.transpile(qname, fl.getSource());
logger.debug("Successful transpilation");

FlowMetadata meta = fl.getMetadata();
Expand Down
61 changes: 18 additions & 43 deletions agama/transpiler/src/main/java/io/jans/agama/dsl/Transpiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public class Transpiler {
private final Logger logger = LoggerFactory.getLogger(Transpiler.class);

private String flowId;
private Set<String> flowNames;
private String fanny;

private Processor processor;
Expand Down Expand Up @@ -89,18 +88,13 @@ public class Transpiler {

}

public Transpiler(String flowQName, Set<String> flowQNames) throws TranspilerException {
public Transpiler(String flowQName) throws TranspilerException {

if (flowQName == null)
throw new TranspilerException("Qualified name cannot be null", new NullPointerException());

this.flowId = flowQName;
fanny = "_" + flowQName.replaceAll("\\.", "_"); //Generates a valid JS function name

if (flowQNames != null) {
flowNames = new HashSet(flowQNames);
flowNames.remove(flowQName);
}

processor = new Processor(false);
xpathCompiler = processor.newXPathCompiler();
Expand Down Expand Up @@ -212,8 +206,7 @@ private void applyValidations(SaplingDocument doc) throws TranspilerException {
try {
XdmNode node = doc.toXdmNode(processor);

//Ensure only existing flows are referenced
checkUnknownInvocation(flowNames, node);
checkAutoInvocations(node);
checkInputsUniqueness(node);
} catch (SaxonApiException se) {
throw new TranspilerException("Validation failed", se);
Expand All @@ -229,21 +222,13 @@ private void validateName(AuthnFlowParser.FlowContext flowContext) throws Transp

}

private void checkUnknownInvocation(Set<String> known, XdmNode node)
throws TranspilerException, SaxonApiException {

if (known != null) {
List<String> invocations = xpathCompiler.evaluate(Visitor.FLOWCALL_XPATH_EXPR, node)
.stream().map(XdmItem::getStringValue).collect(Collectors.toList());
private void checkAutoInvocations(XdmNode node) throws TranspilerException, SaxonApiException {

Set<String> invocations = xpathCompiler.evaluate(Visitor.FLOWCALL_XPATH_EXPR, node)
.stream().map(XdmItem::getStringValue).collect(Collectors.toSet());

for (String t : invocations) {
if (t.equals(flowId))
throw new TranspilerException("A flow cannot trigger an instance of itself");

if (!known.contains(t))
throw new TranspilerException("Invocation of unknown element '" + t + "'");
}
}
if (invocations.contains(flowId))
throw new TranspilerException("A flow must not trigger an instance of itself");

}

Expand Down Expand Up @@ -278,19 +263,16 @@ private void generateFromXml(String fileName, OutputStream out) throws Exception
* Transpiles the input source code to code runnable by Agama flow engine in the
* form of a Javascript function
* @param flowQname Qualified name of the input flow
* @param flowQNames A list of known flow names. This is used to validate which flows can be
* triggered from the input flow (Trigger directive). Passing null disables the validation.
* Passing an empty list will make validation fail if any Trigger directive is found
* @param source Source code of input flow (written using Agama DSL)
* @return A TranspilerResult object holding the details of the generated function
* @throws SyntaxException When the input source has syntactic errors, details are contained
* in the exception thrown
* @throws TranspilerException When other kind of processing error occurred.
*/
public static TranspilationResult transpile(String flowQname, Set<String> flowQNames, String source)
public static TranspilationResult transpile(String flowQname, String source)
throws TranspilerException, SyntaxException {

Transpiler tr = new Transpiler(flowQname, flowQNames);
Transpiler tr = new Transpiler(flowQname);
try {
XdmNode doc = tr.asXML(source);

Expand All @@ -306,36 +288,29 @@ public static TranspilationResult transpile(String flowQname, Set<String> flowQN
}

}

public static void runSyntaxCheck(String flowQname, String source)
throws SyntaxException, TranspilerException {

Transpiler tr = new Transpiler(flowQname, null);
Transpiler tr = new Transpiler(flowQname);
tr.validateName(tr.getFlowContext(source));
}

public static void runSyntaxCheck(String source) throws SyntaxException, TranspilerException {
Transpiler tr = new Transpiler("", null);
Transpiler tr = new Transpiler("");
tr.getFlowContext(source);
}

public static void main(String... args) throws Exception {

Set<String> knownFlows = null;

int len = args.length;

if (len < 2) {
System.err.println("Expecting at least 2 params: input file path and flow ID");
if (len != 2) {
System.err.println("Expecting 2 params: input file path and flow ID");
return;
} else if (len > 2) {

knownFlows = new HashSet<>();
for (int i = 3; i < len; i++) {
knownFlows.add(args[i]);
}
}

Transpiler tr = new Transpiler(args[1], knownFlows);
Transpiler tr = new Transpiler(args[1]);
String dslCode = new String(Files.readAllBytes(Paths.get(args[0])), UTF_8);

XdmNode doc = tr.asXML(dslCode);
Expand Down