diff options
author | Håkon Hallingstad <hakon@yahooinc.com> | 2022-01-11 14:04:27 +0100 |
---|---|---|
committer | Håkon Hallingstad <hakon@yahooinc.com> | 2022-01-11 14:04:27 +0100 |
commit | ffd1b4daf94cae77c02bb6dfae32bdb6555b4926 (patch) | |
tree | 0c4dd6a78e50b4ceffb0a3f81edeebf5a87bafcc /node-admin | |
parent | 41b20087891c366ebd1aa42f9532006baa309e97 (diff) |
Name section that can be added any number of times "list"
Diffstat (limited to 'node-admin')
10 files changed, 46 insertions, 46 deletions
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<Section> sections; private final Map<String, String> values = new HashMap<>(); - private final Map<String, SubformSection> subforms; + private final Map<String, ListSection> lists; - Form(CursorRange range, List<Section> sections, Map<String, SubformSection> subforms) { + Form(CursorRange range, List<Section> sections, Map<String, ListSection> 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<Section> allSections = new ArrayList<>(); private final Map<String, VariableSection> sampleVariables = new HashMap<>(); private final Map<String, IfSection> sampleIfSections = new HashMap<>(); - private final Map<String, SubformSection> subforms = new HashMap<>(); + private final Map<String, ListSection> 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/SubformSection.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/ListSection.java index fa65589d7d5..aede0647683 100644 --- 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/ListSection.java @@ -8,18 +8,18 @@ import java.util.ArrayList; import java.util.List; /** - * Represents a template subform section + * Represents a template list section, to be replaced by any number of body form elements. * * @see Template * @author hakonhall */ -class SubformSection extends Section { +class ListSection extends Section { private final String name; private final Cursor nameOffset; private final Form body; private final List<Form> elements = new ArrayList<>(); - SubformSection(CursorRange range, String name, Cursor nameOffset, Form body) { + ListSection(CursorRange range, String name, Cursor nameOffset, Form body) { super(range); this.name = name; this.nameOffset = new Cursor(nameOffset); @@ -52,6 +52,6 @@ class SubformSection extends Section { // avoid copying elements for now // Optimization: Reuse body in copy, since it is only used for copying. - sectionList.appendSubformSection(name, nameOffset, range().end(), body); + 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/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; * * <pre> * 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 * </pre> * @@ -22,7 +22,7 @@ package com.yahoo.vespa.hosted.node.admin.task.util.template; * <p>To use the template, <b>Instantiate</b> 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()}).</p> * - * <p>A form (like a template) has direct sections, and indirect sections in the body of direct subforms + * <p>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.</p> * 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-} |