Skip to content

Commit 3e5e6c0

Browse files
authored
Merge pull request #434 from wttech/refactor-empty-item-handling
refactor empty list and empty structure handling
2 parents f0ea8f3 + dbab20c commit 3e5e6c0

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ApmType.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ public Object getArgument(DecryptionService decryptionService) {
3434
return null;
3535
}
3636

37-
protected String toPrettyString(int depth, int prefixDepth) {
38-
return null;
39-
}
40-
4137
public Integer getInteger() {
4238
return null;
4339
}
@@ -54,6 +50,14 @@ public Map<String, ApmType> getMap() {
5450
return null;
5551
}
5652

53+
public boolean isEmpty() {
54+
return false;
55+
}
56+
57+
protected String toPrettyString(int depth, int prefixDepth) {
58+
return null;
59+
}
60+
5761
@Override
5862
public String toString() {
5963
return toPrettyString(0, 0);
@@ -149,6 +153,11 @@ public List<ApmType> getList() {
149153
return values;
150154
}
151155

156+
@Override
157+
public boolean isEmpty() {
158+
return values.isEmpty();
159+
}
160+
152161
@Override
153162
protected String toPrettyString(int depth, int prefixDepth) {
154163
boolean simpleList = values.stream()
@@ -196,6 +205,11 @@ public Map<String, ApmType> getMap() {
196205
return values;
197206
}
198207

208+
@Override
209+
public boolean isEmpty() {
210+
return values.isEmpty();
211+
}
212+
199213
@Override
200214
protected String toPrettyString(int depth, int prefixDepth) {
201215
ApmType firstEntry = values.values()
@@ -254,6 +268,11 @@ public ApmType getValue() {
254268
return value;
255269
}
256270

271+
@Override
272+
public boolean isEmpty() {
273+
return value.isEmpty();
274+
}
275+
257276
@Override
258277
protected String toPrettyString(int depth, int prefixDepth) {
259278
return StringUtils.repeat('\t', depth) + key + ": " + value.toPrettyString(depth, 0);
@@ -262,5 +281,9 @@ protected String toPrettyString(int depth, int prefixDepth) {
262281

263282
public static class ApmEmpty extends ApmType {
264283

284+
@Override
285+
public boolean isEmpty() {
286+
return true;
287+
}
265288
}
266289
}

app/aem/core/src/main/java/com/cognifide/apm/core/grammar/ScriptRunner.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
import com.cognifide.apm.api.scripts.Script;
2424
import com.cognifide.apm.api.services.ScriptFinder;
2525
import com.cognifide.apm.api.status.Status;
26-
import com.cognifide.apm.core.grammar.ApmType.ApmEmpty;
2726
import com.cognifide.apm.core.grammar.ApmType.ApmList;
28-
import com.cognifide.apm.core.grammar.ApmType.ApmMap;
27+
import com.cognifide.apm.core.grammar.ApmType.ApmPair;
2928
import com.cognifide.apm.core.grammar.ApmType.ApmString;
3029
import com.cognifide.apm.core.grammar.antlr.ApmLangBaseVisitor;
3130
import com.cognifide.apm.core.grammar.antlr.ApmLangParser.AllowDenyCommandContext;
@@ -49,7 +48,6 @@
4948
import com.cognifide.apm.core.logger.Position;
5049
import com.cognifide.apm.core.logger.Progress;
5150
import com.google.common.collect.ImmutableList;
52-
import com.google.common.collect.ImmutableMap;
5351
import java.util.ArrayList;
5452
import java.util.Collections;
5553
import java.util.List;
@@ -148,25 +146,25 @@ public Status visitRequireVariable(RequireVariableContext ctx) {
148146

149147
@Override
150148
public Status visitForEach(ForEachContext ctx) {
151-
List<Map<String, ApmType>> values = readValues(ctx);
152-
ListIterator<Map<String, ApmType>> iterator = values.listIterator();
149+
List<ApmPair> values = readValues(ctx);
150+
ListIterator<ApmPair> iterator = values.listIterator();
153151
if (!iterator.hasNext() && shouldVisitNextChild()) {
154152
String key = ctx.IDENTIFIER().toString();
155153
progress(ctx, Status.SKIPPED, "for-each", String.format("%s is always empty", key));
156154
}
157155
while (iterator.hasNext() && shouldVisitNextChild()) {
158156
try {
159157
int index = iterator.nextIndex();
160-
Map<String, ApmType> value = iterator.next();
161-
executionContext.createLocalContext();
162-
String valueStr = value.entrySet()
163-
.stream()
164-
.map(entry -> String.format("%s=%s", entry.getKey(), entry.getValue()))
165-
.collect(Collectors.joining("\n"));
166-
progress(ctx, Status.SUCCESS, "for-each", String.format("%d. Begin: %s", index, valueStr));
167-
value.forEach(executionContext::setVariable);
168-
visit(ctx.body());
169-
progress(ctx, Status.SUCCESS, "for-each", String.format("%d. End", index));
158+
ApmPair value = iterator.next();
159+
if (value.isEmpty()) {
160+
progress(ctx, Status.SKIPPED, "for-each", String.format("%d. %s is empty", index, value.getKey()));
161+
} else {
162+
executionContext.createLocalContext();
163+
progress(ctx, Status.SUCCESS, "for-each", String.format("%d. Begin: %s=%s", index, value.getKey(), value.getValue()));
164+
executionContext.setVariable(value.getKey(), value.getValue());
165+
visit(ctx.body());
166+
progress(ctx, Status.SUCCESS, "for-each", String.format("%d. End", index));
167+
}
170168
} finally {
171169
executionContext.removeLocalContext();
172170
}
@@ -278,21 +276,19 @@ public Status visitImportScript(ImportScriptContext ctx) {
278276
return Status.SUCCESS;
279277
}
280278

281-
private List<Map<String, ApmType>> readValues(ForEachContext ctx) {
279+
private List<ApmPair> readValues(ForEachContext ctx) {
282280
String key = ctx.IDENTIFIER().toString();
283281
ApmType variableValue = executionContext.resolveArgument(ctx.argument());
284282
List<ApmType> values;
285-
if (variableValue instanceof ApmList) {
286-
values = variableValue.getList();
287-
} else if (variableValue instanceof ApmEmpty) {
288-
values = Collections.emptyList();
289-
} else if (variableValue instanceof ApmMap && variableValue.getMap().isEmpty()) {
283+
if (variableValue.isEmpty()) {
290284
values = Collections.emptyList();
285+
} else if (variableValue instanceof ApmList) {
286+
values = variableValue.getList();
291287
} else {
292288
values = ImmutableList.of(variableValue);
293289
}
294290
return values.stream()
295-
.map(value -> ImmutableMap.of(key, value))
291+
.map(value -> new ApmPair(key, value))
296292
.collect(Collectors.toList());
297293
}
298294

0 commit comments

Comments
 (0)