From ffd1b4daf94cae77c02bb6dfae32bdb6555b4926 Mon Sep 17 00:00:00 2001 From: HÃ¥kon Hallingstad Date: Tue, 11 Jan 2022 14:04:27 +0100 Subject: Name section that can be added any number of times "list" --- .../hosted/node/admin/task/util/template/Form.java | 10 ++-- .../node/admin/task/util/template/FormBuilder.java | 16 +++--- .../node/admin/task/util/template/ListSection.java | 57 ++++++++++++++++++++++ .../node/admin/task/util/template/SectionList.java | 6 +-- .../admin/task/util/template/SubformSection.java | 57 ---------------------- .../node/admin/task/util/template/Template.java | 6 +-- .../admin/task/util/template/TemplateParser.java | 10 ++-- .../admin/task/util/template/TemplateFileTest.java | 26 +++++----- node-admin/src/test/resources/template1.tmp | 4 +- node-admin/src/test/resources/template2.tmp | 4 +- node-admin/src/test/resources/template3.tmp | 2 +- 11 files changed, 99 insertions(+), 99 deletions(-) create mode 100644 node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/ListSection.java delete mode 100644 node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/SubformSection.java (limited to 'node-admin') diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Form.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Form.java index c95885d7753..0148a976251 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Form.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Form.java @@ -20,12 +20,12 @@ public class Form { private final List
sections; private final Map values = new HashMap<>(); - private final Map subforms; + private final Map lists; - Form(CursorRange range, List
sections, Map subforms) { + Form(CursorRange range, List
sections, Map lists) { this.range = new CursorRange(range); this.sections = List.copyOf(sections); - this.subforms = Map.copyOf(subforms); + this.lists = Map.copyOf(lists); } void setParent(Form parent) { this.parent = parent; } @@ -49,9 +49,9 @@ public class Form { return set(name, value); } - /** Add an instance of a subform section after any previously added (for the given name) */ + /** Add an instance of a list section after any previously added (for the given name) */ public Form add(String name) { - var section = subforms.get(name); + var section = lists.get(name); if (section == null) { throw new NoSuchNameTemplateException(range, name); } diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/FormBuilder.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/FormBuilder.java index 833e5916fce..cae5279f68a 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/FormBuilder.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/FormBuilder.java @@ -17,7 +17,7 @@ class FormBuilder { private final List
allSections = new ArrayList<>(); private final Map sampleVariables = new HashMap<>(); private final Map sampleIfSections = new HashMap<>(); - private final Map subforms = new HashMap<>(); + private final Map lists = new HashMap<>(); FormBuilder(Cursor start) { this.sectionList = new SectionList(start, this); @@ -33,7 +33,7 @@ class FormBuilder { // It's OK if the same name is used in an if-directive (as long as the value is boolean, // determined when set on a form). - SubformSection existing = subforms.get(section.name()); + ListSection existing = lists.get(section.name()); if (existing != null) throw new NameAlreadyExistsTemplateException(section.name(), existing.nameOffset(), section.nameOffset()); @@ -46,16 +46,16 @@ class FormBuilder { // It's OK if the same name is used in a variable section (as long as the value is boolean, // determined when set on a form). - SubformSection subform = subforms.get(section.name()); - if (subform != null) - throw new NameAlreadyExistsTemplateException(section.name(), subform.nameOffset(), + ListSection list = lists.get(section.name()); + if (list != null) + throw new NameAlreadyExistsTemplateException(section.name(), list.nameOffset(), section.nameOffset()); sampleIfSections.put(section.name(), section); allSections.add(section); } - void addSubformSection(SubformSection section) { + void addListSection(ListSection section) { VariableSection variableSection = sampleVariables.get(section.name()); if (variableSection != null) throw new NameAlreadyExistsTemplateException(section.name(), variableSection.nameOffset(), @@ -66,7 +66,7 @@ class FormBuilder { throw new NameAlreadyExistsTemplateException(section.name(), ifSection.nameOffset(), section.nameOffset()); - SubformSection previous = subforms.put(section.name(), section); + ListSection previous = lists.put(section.name(), section); if (previous != null) throw new NameAlreadyExistsTemplateException(section.name(), previous.nameOffset(), section.nameOffset()); @@ -74,7 +74,7 @@ class FormBuilder { } Form build() { - var form = new Form(sectionList.range(), sectionList.sections(), subforms); + var form = new Form(sectionList.range(), sectionList.sections(), lists); allSections.forEach(section -> section.setForm(form)); return form; } diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/ListSection.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/ListSection.java new file mode 100644 index 00000000000..aede0647683 --- /dev/null +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/ListSection.java @@ -0,0 +1,57 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.hosted.node.admin.task.util.template; + +import com.yahoo.vespa.hosted.node.admin.task.util.text.Cursor; +import com.yahoo.vespa.hosted.node.admin.task.util.text.CursorRange; + +import java.util.ArrayList; +import java.util.List; + +/** + * Represents a template list section, to be replaced by any number of body form elements. + * + * @see Template + * @author hakonhall + */ +class ListSection extends Section { + private final String name; + private final Cursor nameOffset; + private final Form body; + private final List
elements = new ArrayList<>(); + + ListSection(CursorRange range, String name, Cursor nameOffset, Form body) { + super(range); + this.name = name; + this.nameOffset = new Cursor(nameOffset); + this.body = body; + } + + String name() { return name; } + Cursor nameOffset() { return new Cursor(nameOffset); } + + @Override + void setForm(Form form) { + super.setForm(form); + body.setParent(form); + } + + Form add() { + Form element = body.copy(); + element.setParent(form()); + elements.add(element); + return element; + } + + @Override + void appendTo(StringBuilder buffer) { + elements.forEach(form -> form.appendTo(buffer)); + } + + @Override + void appendCopyTo(SectionList sectionList) { + // avoid copying elements for now + // Optimization: Reuse body in copy, since it is only used for copying. + + sectionList.appendListSection(name, nameOffset, range().end(), body); + } +} 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 679888678bf..60949c5ffa0 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 @@ -52,10 +52,10 @@ class SectionList { sections.add(section); } - void appendSubformSection(String name, Cursor nameOffset, Cursor end, Form body) { + void appendListSection(String name, Cursor nameOffset, Cursor end, Form body) { CursorRange range = verifyAndUpdateEnd(end); - var section = new SubformSection(range, name, nameOffset, body); - formBuilder.addSubformSection(section); + var section = new ListSection(range, name, nameOffset, body); + formBuilder.addListSection(section); sections.add(section); } diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/SubformSection.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/SubformSection.java deleted file mode 100644 index fa65589d7d5..00000000000 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/SubformSection.java +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.hosted.node.admin.task.util.template; - -import com.yahoo.vespa.hosted.node.admin.task.util.text.Cursor; -import com.yahoo.vespa.hosted.node.admin.task.util.text.CursorRange; - -import java.util.ArrayList; -import java.util.List; - -/** - * Represents a template subform section - * - * @see Template - * @author hakonhall - */ -class SubformSection extends Section { - private final String name; - private final Cursor nameOffset; - private final Form body; - private final List elements = new ArrayList<>(); - - SubformSection(CursorRange range, String name, Cursor nameOffset, Form body) { - super(range); - this.name = name; - this.nameOffset = new Cursor(nameOffset); - this.body = body; - } - - String name() { return name; } - Cursor nameOffset() { return new Cursor(nameOffset); } - - @Override - void setForm(Form form) { - super.setForm(form); - body.setParent(form); - } - - Form add() { - Form element = body.copy(); - element.setParent(form()); - elements.add(element); - return element; - } - - @Override - void appendTo(StringBuilder buffer) { - elements.forEach(form -> form.appendTo(buffer)); - } - - @Override - void appendCopyTo(SectionList sectionList) { - // avoid copying elements for now - // Optimization: Reuse body in copy, since it is only used for copying. - - sectionList.appendSubformSection(name, nameOffset, range().end(), body); - } -} diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java index b7cd2f80ad6..c5b752f2d6e 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java @@ -8,11 +8,11 @@ package com.yahoo.vespa.hosted.node.admin.task.util.template; * *
  *     template: section*
- *     section: literal | variable | subform
+ *     section: literal | variable | list
  *     literal: plain text not containing %{
  *     variable: %{=id}
  *     if: %{if [!]id}template[%{else}template]%{end}
- *     subform: %{form id}template%{end}
+ *     list: %{list id}template%{end}
  *     id: a valid Java identifier
  * 
* @@ -22,7 +22,7 @@ package com.yahoo.vespa.hosted.node.admin.task.util.template; *

To use the template, Instantiate it to get a form ({@link #instantiate()}), fill it (e.g. * {@link Form#set(String, String) Form.set()}), and render the String ({@link Form#render()}).

* - *

A form (like a template) has direct sections, and indirect sections in the body of direct subforms + *

A form (like a template) has direct sections, and indirect sections in the body of direct list * sections (recursively). The variables that can be set for a form, are the variables defined in * either direct or indirect variable sections.

* 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 ac38a23d4ad..c81a07512b0 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 @@ -74,12 +74,12 @@ class TemplateParser { throw new BadTemplateException(startOfType, "Extraneous 'end'"); parseEndDirective(); return Optional.of(Sentinel.END); - case "form": - parseSubformSection(sectionList); - break; case "if": parseIfSection(sectionList); break; + case "list": + parseListSection(sectionList); + break; default: throw new BadTemplateException(startOfType, "Unknown section '" + type + "'"); } @@ -99,7 +99,7 @@ class TemplateParser { parseEndDelimiter(true); } - private void parseSubformSection(SectionList sectionList) { + private void parseListSection(SectionList sectionList) { skipRequiredWhitespaces(); var startOfName = new Cursor(current); String name = parseId(); @@ -108,7 +108,7 @@ class TemplateParser { TemplateParser bodyParser = parse(descriptor, current, EnumSet.of(Sentinel.END)); current.set(bodyParser.current); - sectionList.appendSubformSection(name, startOfName, current, bodyParser.formBuilder.build()); + sectionList.appendListSection(name, startOfName, current, bodyParser.formBuilder.build()); } private void parseIfSection(SectionList sectionList) { 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 2f4b108dee7..cc2228e7f77 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 @@ -20,10 +20,10 @@ class TemplateFileTest { } @Test - void verifySimpleSubformSection() { + void verifySimpleListSection() { Form form = getForm("template1.tmp"); form.set("varname", "varvalue") - .add("formname") + .add("listname") .set("varname", "different varvalue") .set("varname2", "varvalue2"); assertEquals("variable section 'varvalue'\n" + @@ -34,14 +34,14 @@ class TemplateFileTest { } @Test - void verifyNestedSubformSection() { + void verifyNestedListSection() { Form form = getForm("template2.tmp"); - Form A0 = form.add("formA"); - Form A0B0 = A0.add("formB"); - Form A0B1 = A0.add("formB"); + Form A0 = form.add("listA"); + Form A0B0 = A0.add("listB"); + Form A0B1 = A0.add("listB"); - Form A1 = form.add("formA"); - Form A1B0 = A1.add("formB"); + Form A1 = form.add("listA"); + Form A1B0 = A1.add("listB"); assertEquals("body A\n" + "body B\n" + "body B\n" + @@ -69,9 +69,9 @@ class TemplateFileTest { @Test void verifyNewlineRemoval() { - Form form = makeForm("a%{form a}\n" + + Form form = makeForm("a%{list a}\n" + "b%{end}\n" + - "c%{form c-}\n" + + "c%{list c-}\n" + "d%{end-}\n" + "e\n"); form.add("a"); @@ -95,14 +95,14 @@ class TemplateFileTest { Template template = Template.from("%{if cond-}\n" + "var: %{=varname}\n" + "if: %{if !inner}inner is false%{end}\n" + - "subform: %{form formname}subform%{end}\n" + + "list: %{list formname}element%{end}\n" + "%{end-}\n"); assertEquals("", template.instantiate().set("cond", false).render()); assertEquals("var: varvalue\n" + "if: \n" + - "subform: \n", + "list: \n", template.instantiate() .set("cond", true) .set("varname", "varvalue") @@ -117,7 +117,7 @@ class TemplateFileTest { assertEquals("var: varvalue\n" + "if: inner is false\n" + - "subform: subform\n", form.render()); + "list: element\n", form.render()); } private Form getForm(String filename) { diff --git a/node-admin/src/test/resources/template1.tmp b/node-admin/src/test/resources/template1.tmp index 289dc00b0bb..3468709cc2e 100644 --- a/node-admin/src/test/resources/template1.tmp +++ b/node-admin/src/test/resources/template1.tmp @@ -1,8 +1,8 @@ variable section '%{=varname}' -%{form formname-} +%{list listname-} same variable section '%{=varname}' different variable section '%{=varname2}' -%{form innerformname-} +%{list innerlistname-} inner form text %{end-} between ends diff --git a/node-admin/src/test/resources/template2.tmp b/node-admin/src/test/resources/template2.tmp index cdf392171ce..3bfa30ec7d3 100644 --- a/node-admin/src/test/resources/template2.tmp +++ b/node-admin/src/test/resources/template2.tmp @@ -1,4 +1,4 @@ -%{form formA-}body A -%{form formB-}body B +%{list listA-}body A +%{list listB-}body B %{end-} %{end-} diff --git a/node-admin/src/test/resources/template3.tmp b/node-admin/src/test/resources/template3.tmp index fc971bc4ba3..454b01761b5 100644 --- a/node-admin/src/test/resources/template3.tmp +++ b/node-admin/src/test/resources/template3.tmp @@ -1,6 +1,6 @@ %{=varname} %{=varname} -%{form l-} +%{list l-} inner %{=varname} %{=innerVarSetAtTop} %{end-} -- cgit v1.2.3