From 0f84bc9641680e428192c12309317240600c0219 Mon Sep 17 00:00:00 2001 From: tvallin Date: Thu, 10 Oct 2024 10:57:21 +0200 Subject: [PATCH] Null condition after script optimizer job Signed-off-by: tvallin --- .../v2/json/ScriptDeserializerTest.java | 12 +++ .../v2/json/ScriptSerializerTest.java | 12 +++ .../test/resources/deserializer/script1.json | 84 +++++++++++++++++++ .../src/test/resources/expected/inlined5.json | 0 .../src/test/resources/inlined5/main.xml | 29 +++++++ .../src/test/resources/inlined5/script1.xml | 25 ++++++ .../src/test/resources/inlined5/script2.xml | 27 ++++++ .../src/test/resources/inlined5/script3.xml | 28 +++++++ .../archetype/engine/v2/ast/Condition.java | 4 + .../engine/v2/util/ClientCompiler.java | 9 +- .../engine/v2/util/ClientCompilerTest.java | 33 ++++++++ .../test/resources/compiler/inlined5/main.xml | 24 ++++++ .../resources/compiler/inlined5/script1.xml | 25 ++++++ .../resources/compiler/inlined5/script2.xml | 26 ++++++ .../resources/compiler/inlined5/script3.xml | 26 ++++++ 15 files changed, 363 insertions(+), 1 deletion(-) create mode 100644 archetype/engine-v2-json/src/test/resources/deserializer/script1.json create mode 100644 archetype/engine-v2-json/src/test/resources/expected/inlined5.json create mode 100644 archetype/engine-v2-json/src/test/resources/inlined5/main.xml create mode 100644 archetype/engine-v2-json/src/test/resources/inlined5/script1.xml create mode 100644 archetype/engine-v2-json/src/test/resources/inlined5/script2.xml create mode 100644 archetype/engine-v2-json/src/test/resources/inlined5/script3.xml create mode 100644 archetype/engine-v2/src/test/resources/compiler/inlined5/main.xml create mode 100644 archetype/engine-v2/src/test/resources/compiler/inlined5/script1.xml create mode 100644 archetype/engine-v2/src/test/resources/compiler/inlined5/script2.xml create mode 100644 archetype/engine-v2/src/test/resources/compiler/inlined5/script3.xml diff --git a/archetype/engine-v2-json/src/test/java/io/helidon/build/archetype/v2/json/ScriptDeserializerTest.java b/archetype/engine-v2-json/src/test/java/io/helidon/build/archetype/v2/json/ScriptDeserializerTest.java index b77a0cc1d..e2aafc97d 100644 --- a/archetype/engine-v2-json/src/test/java/io/helidon/build/archetype/v2/json/ScriptDeserializerTest.java +++ b/archetype/engine-v2-json/src/test/java/io/helidon/build/archetype/v2/json/ScriptDeserializerTest.java @@ -49,4 +49,16 @@ void testDeserialize() throws IOException { System.out.println(JsonFactory.toPrettyString(archetypeJson)); assertThat(jsonDiff(archetypeJson, readJson(expected)), is(EMPTY_JSON_ARRAY)); } + + @Test + void testDeserializeCondition() throws IOException { + Path targetDir = targetDir(this.getClass()); + Path jsonFile = targetDir.resolve("test-classes/deserializer/script1.json"); + Path expected = targetDir.resolve("test-classes/expected/deserializer.json"); + Script script = ScriptDeserializer.deserialize(Files.newInputStream(jsonFile)); + + JsonObject archetypeJson = ScriptSerializer.serialize(script); + System.out.println(JsonFactory.toPrettyString(archetypeJson)); + assertThat(jsonDiff(archetypeJson, readJson(expected)), is(EMPTY_JSON_ARRAY)); + } } diff --git a/archetype/engine-v2-json/src/test/java/io/helidon/build/archetype/v2/json/ScriptSerializerTest.java b/archetype/engine-v2-json/src/test/java/io/helidon/build/archetype/v2/json/ScriptSerializerTest.java index f9c3f4064..234ba2be8 100644 --- a/archetype/engine-v2-json/src/test/java/io/helidon/build/archetype/v2/json/ScriptSerializerTest.java +++ b/archetype/engine-v2-json/src/test/java/io/helidon/build/archetype/v2/json/ScriptSerializerTest.java @@ -100,6 +100,18 @@ void testInlined4() throws IOException { assertThat(jsonDiff(archetypeJson, readJson(expected)), is(EMPTY_JSON_ARRAY)); } + @Test + void testInlined5() throws IOException { + Path targetDir = targetDir(this.getClass()); + Path sourceDir = targetDir.resolve("test-classes/inlined5"); + Path expected = targetDir.resolve("test-classes/expected/inlined5.json"); + FileSystem fs = VirtualFileSystem.create(sourceDir); + + JsonObject archetypeJson = ScriptSerializer.serialize(fs); + System.out.println(JsonFactory.toPrettyString(archetypeJson)); + assertThat(jsonDiff(archetypeJson, readJson(expected)), is(EMPTY_JSON_ARRAY)); + } + @Test void testEmptyMethodNotCompiled() throws IOException { Path targetDir = targetDir(this.getClass()); diff --git a/archetype/engine-v2-json/src/test/resources/deserializer/script1.json b/archetype/engine-v2-json/src/test/resources/deserializer/script1.json new file mode 100644 index 000000000..9c3204927 --- /dev/null +++ b/archetype/engine-v2-json/src/test/resources/deserializer/script1.json @@ -0,0 +1,84 @@ +{ + "expressions": { + "1": [ + { + "kind": "variable", + "value": "foo" + } + ], + "2": [ + { + "kind": "variable", + "value": "bar2" + } + ], + "3": [ + { + "kind": "variable", + "value": "bar3" + } + ] + }, + "methods": { + }, + "children": [ + { + "kind": "variables", + "children": [ + { + "kind": "boolean", + "path": "foo", + "value": true + }, + { + "kind": "boolean", + "path": "bar2", + "value": true + }, + { + "kind": "boolean", + "path": "bar3", + "value": true + } + ] + }, + { + "kind": "step", + "name": "Step2", + "id": "1", + "if": "2", + "children": [ + { + "kind": "inputs", + "children": [ + { + "kind": "boolean", + "name": "Boolean2", + "id": "boolean2", + "default": false + } + ] + } + ] + }, + { + "kind": "step", + "name": "Step3", + "id": "2", + "if": "3", + "children": [ + { + "kind": "inputs", + "children": [ + { + "kind": "boolean", + "name": "Boolean3", + "id": "boolean3", + "default": false + } + ] + } + ] + } + ] +} diff --git a/archetype/engine-v2-json/src/test/resources/expected/inlined5.json b/archetype/engine-v2-json/src/test/resources/expected/inlined5.json new file mode 100644 index 000000000..e69de29bb diff --git a/archetype/engine-v2-json/src/test/resources/inlined5/main.xml b/archetype/engine-v2-json/src/test/resources/inlined5/main.xml new file mode 100644 index 000000000..859f66a9b --- /dev/null +++ b/archetype/engine-v2-json/src/test/resources/inlined5/main.xml @@ -0,0 +1,29 @@ + + + + + + true + true + true + + + diff --git a/archetype/engine-v2-json/src/test/resources/inlined5/script1.xml b/archetype/engine-v2-json/src/test/resources/inlined5/script1.xml new file mode 100644 index 000000000..60b21e0ca --- /dev/null +++ b/archetype/engine-v2-json/src/test/resources/inlined5/script1.xml @@ -0,0 +1,25 @@ + + + + + + + diff --git a/archetype/engine-v2-json/src/test/resources/inlined5/script2.xml b/archetype/engine-v2-json/src/test/resources/inlined5/script2.xml new file mode 100644 index 000000000..a5dcb0dd8 --- /dev/null +++ b/archetype/engine-v2-json/src/test/resources/inlined5/script2.xml @@ -0,0 +1,27 @@ + + + + + + + + + diff --git a/archetype/engine-v2-json/src/test/resources/inlined5/script3.xml b/archetype/engine-v2-json/src/test/resources/inlined5/script3.xml new file mode 100644 index 000000000..f5dfc7f1e --- /dev/null +++ b/archetype/engine-v2-json/src/test/resources/inlined5/script3.xml @@ -0,0 +1,28 @@ + + + + + + + + + + diff --git a/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/ast/Condition.java b/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/ast/Condition.java index 0b789801a..47d7c6704 100644 --- a/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/ast/Condition.java +++ b/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/ast/Condition.java @@ -158,6 +158,10 @@ public Builder then(Node.Builder then) { return this; } + public boolean isThenNull() { + return then == null; + } + @Override protected Condition doBuild() { return new Condition(this); diff --git a/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/util/ClientCompiler.java b/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/util/ClientCompiler.java index aaf8e775f..edbfefda0 100644 --- a/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/util/ClientCompiler.java +++ b/archetype/engine-v2/src/main/java/io/helidon/build/archetype/engine/v2/util/ClientCompiler.java @@ -150,6 +150,7 @@ public Node.VisitResult visitBlock(Block block, Script script) { @Override public Node.VisitResult postVisitBlock(Block block, Script arg) { Node.Builder builder = stack.pop(); + Node.Builder parentBuilder = stack.peek(); if (builder.children().isEmpty()) { // skip certain blocks without children switch (block.kind()) { @@ -159,7 +160,6 @@ public Node.VisitResult postVisitBlock(Block block, Script arg) { case VARIABLES: case INVOKE: case INVOKE_DIR: - Node.Builder parentBuilder = stack.peek(); if (parentBuilder != null) { parentBuilder.nestedBuilders().remove(builder); } @@ -167,6 +167,13 @@ public Node.VisitResult postVisitBlock(Block block, Script arg) { default: } } + + if (builder instanceof Condition.Builder) { + if (((Condition.Builder) builder).isThenNull() && parentBuilder != null) { + parentBuilder.nestedBuilders().remove(builder); + return Node.VisitResult.CONTINUE; + } + } builder.build(); return Node.VisitResult.CONTINUE; } diff --git a/archetype/engine-v2/src/test/java/io/helidon/build/archetype/engine/v2/util/ClientCompilerTest.java b/archetype/engine-v2/src/test/java/io/helidon/build/archetype/engine/v2/util/ClientCompilerTest.java index f498b9a86..2f6231b9c 100644 --- a/archetype/engine-v2/src/test/java/io/helidon/build/archetype/engine/v2/util/ClientCompilerTest.java +++ b/archetype/engine-v2/src/test/java/io/helidon/build/archetype/engine/v2/util/ClientCompilerTest.java @@ -546,6 +546,39 @@ public VisitResult visitAny(Input input, Void arg) { assertThat(index[0], is(17)); } + @Test + void testInlined5() { + Script script = load("compiler/inlined5/main.xml"); + Script compiledScript = ClientCompiler.compile(script, false); + int[] index = new int[]{0}; + walk(new Node.Visitor<>() { + @Override + public VisitResult visitBlock(Block block, Void arg) { + return block.accept(new Block.Visitor<>() { + + @Override + public VisitResult visitInput(Input input, Void arg) { + return input.accept(new Input.Visitor<>() { + + @Override + public VisitResult visitBoolean(Input.Boolean input, Void arg) { + ++index[0]; + return VisitResult.CONTINUE; + } + + @Override + public VisitResult visitAny(Input input, Void arg) { + fail(String.format("Unexpected input: %s, index=%d", block, index[0])); + return VisitResult.CONTINUE; + } + }, arg); + } + }, arg); + } + }, compiledScript); + //assertThat(index[0], is(17)); + } + @Test void testDeclarations() { Script script = load("compiler/declarations/main.xml"); diff --git a/archetype/engine-v2/src/test/resources/compiler/inlined5/main.xml b/archetype/engine-v2/src/test/resources/compiler/inlined5/main.xml new file mode 100644 index 000000000..ace7f2879 --- /dev/null +++ b/archetype/engine-v2/src/test/resources/compiler/inlined5/main.xml @@ -0,0 +1,24 @@ + + + + + + diff --git a/archetype/engine-v2/src/test/resources/compiler/inlined5/script1.xml b/archetype/engine-v2/src/test/resources/compiler/inlined5/script1.xml new file mode 100644 index 000000000..60b21e0ca --- /dev/null +++ b/archetype/engine-v2/src/test/resources/compiler/inlined5/script1.xml @@ -0,0 +1,25 @@ + + + + + + + diff --git a/archetype/engine-v2/src/test/resources/compiler/inlined5/script2.xml b/archetype/engine-v2/src/test/resources/compiler/inlined5/script2.xml new file mode 100644 index 000000000..f5026956a --- /dev/null +++ b/archetype/engine-v2/src/test/resources/compiler/inlined5/script2.xml @@ -0,0 +1,26 @@ + + + + + + + + diff --git a/archetype/engine-v2/src/test/resources/compiler/inlined5/script3.xml b/archetype/engine-v2/src/test/resources/compiler/inlined5/script3.xml new file mode 100644 index 000000000..a3051cadb --- /dev/null +++ b/archetype/engine-v2/src/test/resources/compiler/inlined5/script3.xml @@ -0,0 +1,26 @@ + + + + + + + +