diff options
author | Håkon Hallingstad <hakon@yahooinc.com> | 2022-01-11 14:32:51 +0100 |
---|---|---|
committer | Håkon Hallingstad <hakon@yahooinc.com> | 2022-01-11 14:32:51 +0100 |
commit | 30d845367c9018a2f423b612c05b0ecf28c01fb4 (patch) | |
tree | bb0d2969c58fb440044262f5ea8a0e1bb58e2531 /node-admin | |
parent | ffd1b4daf94cae77c02bb6dfae32bdb6555b4926 (diff) |
Implement the "else" of the if section
Diffstat (limited to 'node-admin')
4 files changed, 41 insertions, 7 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/IfSection.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/IfSection.java index 33e62750f69..6bf2811d72e 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/IfSection.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/IfSection.java @@ -14,14 +14,16 @@ public class IfSection extends Section { private final String name; private final Cursor nameOffset; private final SectionList ifSections; + private final Optional<SectionList> elseSections; public IfSection(CursorRange range, boolean negated, String name, Cursor nameOffset, - SectionList ifSections) { + SectionList ifSections, Optional<SectionList> elseSections) { super(range); this.negated = negated; this.name = name; this.nameOffset = nameOffset; this.ifSections = ifSections; + this.elseSections = elseSections; } String name() { return name; } @@ -45,6 +47,8 @@ public class IfSection extends Section { boolean condition = negated ? !value : value; if (condition) { ifSections.sections().forEach(section -> section.appendTo(buffer)); + } else if (elseSections.isPresent()) { + elseSections.get().sections().forEach(section -> section.appendTo(buffer)); } } @@ -52,6 +56,13 @@ public class IfSection extends Section { void appendCopyTo(SectionList sectionList) { SectionList ifSectionCopy = new SectionList(ifSections.range().start(), sectionList.formBuilder()); ifSections.sections().forEach(section -> section.appendCopyTo(ifSectionCopy)); - sectionList.appendIfSection(negated, name, nameOffset, range().end(), ifSectionCopy); + + Optional<SectionList> elseSectionCopy = elseSections.map(elseSections2 -> { + SectionList elseSectionCopy2 = new SectionList(elseSections2.range().start(), sectionList.formBuilder()); + elseSections2.sections().forEach(section -> section.appendCopyTo(elseSectionCopy2)); + return elseSectionCopy2; + }); + + sectionList.appendIfSection(negated, name, nameOffset, range().end(), ifSectionCopy, elseSectionCopy); } } diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/SectionList.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/SectionList.java index 60949c5ffa0..b9a8c4e8c41 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/SectionList.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/SectionList.java @@ -6,6 +6,7 @@ import com.yahoo.vespa.hosted.node.admin.task.util.text.CursorRange; import java.util.ArrayList; import java.util.List; +import java.util.Optional; /** * A mutable list of sections at the same level that can be used to build a form, e.g. the if-body. @@ -45,9 +46,9 @@ class SectionList { } void appendIfSection(boolean negated, String name, Cursor nameOffset, Cursor end, - SectionList ifSections) { + SectionList ifSections, Optional<SectionList> elseSections) { CursorRange range = verifyAndUpdateEnd(end); - var section = new IfSection(range, negated, name, nameOffset, ifSections); + var section = new IfSection(range, negated, name, nameOffset, ifSections, elseSections); formBuilder.addIfSection(section); sections.add(section); } diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java index c81a07512b0..93a83a3d29f 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java @@ -27,7 +27,7 @@ class TemplateParser { return parser; } - private enum Sentinel { END, EOT } + private enum Sentinel { ELSE, END, EOT } private TemplateParser(TemplateDescriptor descriptor, Cursor start) { this.descriptor = descriptor; @@ -69,6 +69,11 @@ class TemplateParser { String type = skipId().orElseThrow(() -> new BadTemplateException(current, "Missing section name")); switch (type) { + case "else": + if (!sentinels.contains(Sentinel.ELSE)) + throw new BadTemplateException(startOfType, "Extraneous 'else'"); + parseEndDirective(); + return Optional.of(Sentinel.ELSE); case "end": if (!sentinels.contains(Sentinel.END)) throw new BadTemplateException(startOfType, "Extraneous 'end'"); @@ -120,9 +125,15 @@ class TemplateParser { parseEndDelimiter(true); SectionList ifSectionList = new SectionList(current, formBuilder); - parse(ifSectionList, EnumSet.of(Sentinel.END)); + Sentinel ifSentinel = parse(ifSectionList, EnumSet.of(Sentinel.ELSE, Sentinel.END)); + + Optional<SectionList> elseSectionList = Optional.empty(); + if (ifSentinel == Sentinel.ELSE) { + elseSectionList = Optional.of(new SectionList(current, formBuilder)); + parse(elseSectionList.get(), EnumSet.of(Sentinel.END)); + } - sectionList.appendIfSection(negated, name, startOfName, current, ifSectionList); + sectionList.appendIfSection(negated, name, startOfName, current, ifSectionList, elseSectionList); } private void skipRequiredWhitespaces() { diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateFileTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateFileTest.java index cc2228e7f77..807849bbdb6 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateFileTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateFileTest.java @@ -120,6 +120,17 @@ class TemplateFileTest { "list: element\n", form.render()); } + @Test + void verifyElse() { + var template = Template.from("%{if cond-}\n" + + "if body\n" + + "%{else-}\n" + + "else body\n" + + "%{end-}\n"); + assertEquals("if body\n", template.instantiate().set("cond", true).render()); + assertEquals("else body\n", template.instantiate().set("cond", false).render()); + } + private Form getForm(String filename) { return TemplateFile.read(Path.of("src/test/resources/" + filename)).instantiate(); } |