From dbab20c85765d0a903b737d5a1521e8c8f729245 Mon Sep 17 00:00:00 2001 From: Dominik Przybyl Date: Mon, 6 Nov 2023 13:21:01 +0100 Subject: [PATCH] refactor empty list and empty structure handling --- .../cognifide/apm/core/grammar/ApmType.java | 31 ++++++++++++-- .../apm/core/grammar/ScriptRunner.java | 40 +++++++++---------- 2 files changed, 45 insertions(+), 26 deletions(-) diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ApmType.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ApmType.java index 61c0fb36..7e860798 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ApmType.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ApmType.java @@ -34,10 +34,6 @@ public Object getArgument(DecryptionService decryptionService) { return null; } - protected String toPrettyString(int depth, int prefixDepth) { - return null; - } - public Integer getInteger() { return null; } @@ -54,6 +50,14 @@ public Map getMap() { return null; } + public boolean isEmpty() { + return false; + } + + protected String toPrettyString(int depth, int prefixDepth) { + return null; + } + @Override public String toString() { return toPrettyString(0, 0); @@ -149,6 +153,11 @@ public List getList() { return values; } + @Override + public boolean isEmpty() { + return values.isEmpty(); + } + @Override protected String toPrettyString(int depth, int prefixDepth) { boolean simpleList = values.stream() @@ -196,6 +205,11 @@ public Map getMap() { return values; } + @Override + public boolean isEmpty() { + return values.isEmpty(); + } + @Override protected String toPrettyString(int depth, int prefixDepth) { ApmType firstEntry = values.values() @@ -254,6 +268,11 @@ public ApmType getValue() { return value; } + @Override + public boolean isEmpty() { + return value.isEmpty(); + } + @Override protected String toPrettyString(int depth, int prefixDepth) { return StringUtils.repeat('\t', depth) + key + ": " + value.toPrettyString(depth, 0); @@ -262,5 +281,9 @@ protected String toPrettyString(int depth, int prefixDepth) { public static class ApmEmpty extends ApmType { + @Override + public boolean isEmpty() { + return true; + } } } diff --git a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ScriptRunner.java b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ScriptRunner.java index 68ba120e..375230d8 100644 --- a/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ScriptRunner.java +++ b/app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ScriptRunner.java @@ -23,9 +23,8 @@ import com.cognifide.apm.api.scripts.Script; import com.cognifide.apm.api.services.ScriptFinder; import com.cognifide.apm.api.status.Status; -import com.cognifide.apm.core.grammar.ApmType.ApmEmpty; import com.cognifide.apm.core.grammar.ApmType.ApmList; -import com.cognifide.apm.core.grammar.ApmType.ApmMap; +import com.cognifide.apm.core.grammar.ApmType.ApmPair; import com.cognifide.apm.core.grammar.ApmType.ApmString; import com.cognifide.apm.core.grammar.antlr.ApmLangBaseVisitor; import com.cognifide.apm.core.grammar.antlr.ApmLangParser.AllowDenyCommandContext; @@ -49,7 +48,6 @@ import com.cognifide.apm.core.logger.Position; import com.cognifide.apm.core.logger.Progress; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -148,8 +146,8 @@ public Status visitRequireVariable(RequireVariableContext ctx) { @Override public Status visitForEach(ForEachContext ctx) { - List> values = readValues(ctx); - ListIterator> iterator = values.listIterator(); + List values = readValues(ctx); + ListIterator iterator = values.listIterator(); if (!iterator.hasNext() && shouldVisitNextChild()) { String key = ctx.IDENTIFIER().toString(); progress(ctx, Status.SKIPPED, "for-each", String.format("%s is always empty", key)); @@ -157,16 +155,16 @@ public Status visitForEach(ForEachContext ctx) { while (iterator.hasNext() && shouldVisitNextChild()) { try { int index = iterator.nextIndex(); - Map value = iterator.next(); - executionContext.createLocalContext(); - String valueStr = value.entrySet() - .stream() - .map(entry -> String.format("%s=%s", entry.getKey(), entry.getValue())) - .collect(Collectors.joining("\n")); - progress(ctx, Status.SUCCESS, "for-each", String.format("%d. Begin: %s", index, valueStr)); - value.forEach(executionContext::setVariable); - visit(ctx.body()); - progress(ctx, Status.SUCCESS, "for-each", String.format("%d. End", index)); + ApmPair value = iterator.next(); + if (value.isEmpty()) { + progress(ctx, Status.SKIPPED, "for-each", String.format("%d. %s is empty", index, value.getKey())); + } else { + executionContext.createLocalContext(); + progress(ctx, Status.SUCCESS, "for-each", String.format("%d. Begin: %s=%s", index, value.getKey(), value.getValue())); + executionContext.setVariable(value.getKey(), value.getValue()); + visit(ctx.body()); + progress(ctx, Status.SUCCESS, "for-each", String.format("%d. End", index)); + } } finally { executionContext.removeLocalContext(); } @@ -278,21 +276,19 @@ public Status visitImportScript(ImportScriptContext ctx) { return Status.SUCCESS; } - private List> readValues(ForEachContext ctx) { + private List readValues(ForEachContext ctx) { String key = ctx.IDENTIFIER().toString(); ApmType variableValue = executionContext.resolveArgument(ctx.argument()); List values; - if (variableValue instanceof ApmList) { - values = variableValue.getList(); - } else if (variableValue instanceof ApmEmpty) { - values = Collections.emptyList(); - } else if (variableValue instanceof ApmMap && variableValue.getMap().isEmpty()) { + if (variableValue.isEmpty()) { values = Collections.emptyList(); + } else if (variableValue instanceof ApmList) { + values = variableValue.getList(); } else { values = ImmutableList.of(variableValue); } return values.stream() - .map(value -> ImmutableMap.of(key, value)) + .map(value -> new ApmPair(key, value)) .collect(Collectors.toList()); }