aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src
diff options
context:
space:
mode:
authorHåkon Hallingstad <hakon@yahooinc.com>2022-01-12 01:26:03 +0100
committerHåkon Hallingstad <hakon@yahooinc.com>2022-01-12 01:26:55 +0100
commitf139ccd3d74fb54688f912b3a34a267b0a5d8a85 (patch)
treeaa31eff647ffa7e27b00380e711860d74e3578d1 /node-admin/src
parent39466201d45b2326ea11349e647649c94f18b491 (diff)
Merge Form into Template
Diffstat (limited to 'node-admin/src')
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Form.java86
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/IfSection.java7
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/ListSection.java31
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Section.java10
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/SectionList.java21
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java82
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateBuilder.java (renamed from node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/FormBuilder.java)18
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java14
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/VariableSection.java4
-rw-r--r--node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateFileTest.java50
-rw-r--r--node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateTest.java44
11 files changed, 178 insertions, 189 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
deleted file mode 100644
index 6b38835e24f..00000000000
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Form.java
+++ /dev/null
@@ -1,86 +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.CursorRange;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-
-/**
- * A form is an instance of a template to be filled, e.g. values set for variable sections, etc.
- *
- * @see Template
- * @author hakonhall
- */
-public class Form {
- private Form parent = null;
- private final CursorRange range;
- private final List<Section> sections;
-
- private final Map<String, String> values = new HashMap<>();
- private final Map<String, ListSection> lists;
-
- Form(CursorRange range, List<Section> sections, Map<String, ListSection> lists) {
- this.range = new CursorRange(range);
- this.sections = List.copyOf(sections);
- this.lists = Map.copyOf(lists);
- }
-
- void setParent(Form parent) { this.parent = parent; }
-
- /** Set the value of a variable, e.g. %{=color}. */
- public Form set(String name, String value) {
- values.put(name, value);
- return this;
- }
-
- /** Set the value of a variable and/or if-condition. */
- public Form set(String name, boolean value) { return set(name, Boolean.toString(value)); }
-
- public Form set(String name, int value) { return set(name, Integer.toString(value)); }
- public Form set(String name, long value) { return set(name, Long.toString(value)); }
-
- public Form set(String name, String format, String first, String... rest) {
- var args = new Object[1 + rest.length];
- args[0] = first;
- System.arraycopy(rest, 0, args, 1, rest.length);
- var value = String.format(format, args);
-
- return set(name, value);
- }
-
- /** Add an instance of a list section after any previously added (for the given name) */
- public Form add(String name) {
- var section = lists.get(name);
- if (section == null) {
- throw new NoSuchNameTemplateException(range, name);
- }
- return section.add();
- }
-
- public String render() {
- var buffer = new StringBuilder((int) (range.length() * 1.2 + 128));
- appendTo(buffer);
- return buffer.toString();
- }
-
- public void appendTo(StringBuilder buffer) {
- sections.forEach(section -> section.appendTo(buffer));
- }
-
- /** Returns a deep copy of this. No changes to this affects the returned form, and vice versa. */
- Form copy() {
- var builder = new FormBuilder(range.start());
- sections.forEach(section -> section.appendCopyTo(builder.topLevelSectionList()));
- return builder.build();
- }
-
- Optional<String> getVariableValue(String name) {
- String value = values.get(name);
- if (value != null) return Optional.of(value);
- if (parent != null) return parent.getVariableValue(name);
- return Optional.empty();
- }
-}
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 8775e764b4f..4a00115cee4 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
@@ -31,7 +31,7 @@ class IfSection extends Section {
@Override
void appendTo(StringBuilder buffer) {
- Optional<String> stringValue = form().getVariableValue(name);
+ Optional<String> stringValue = template().getVariableValue(name);
if (stringValue.isEmpty())
throw new TemplateNameNotSetException(name, nameOffset);
@@ -54,11 +54,12 @@ class IfSection extends Section {
@Override
void appendCopyTo(SectionList sectionList) {
- SectionList ifSectionCopy = new SectionList(ifSections.range().start(), sectionList.formBuilder());
+ SectionList ifSectionCopy = new SectionList(ifSections.range().start(), sectionList.templateBuilder());
ifSections.sections().forEach(section -> section.appendCopyTo(ifSectionCopy));
Optional<SectionList> elseSectionCopy = elseSections.map(elseSections2 -> {
- SectionList elseSectionCopy2 = new SectionList(elseSections2.range().start(), sectionList.formBuilder());
+ SectionList elseSectionCopy2 = new SectionList(elseSections2.range().start(),
+ sectionList.templateBuilder());
elseSections2.sections().forEach(section -> section.appendCopyTo(elseSectionCopy2));
return elseSectionCopy2;
});
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
index bc68cf96153..831dc3fe5e8 100644
--- 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
@@ -13,10 +13,10 @@ import java.util.List;
class ListSection extends Section {
private final String name;
private final Cursor nameOffset;
- private final Form body;
- private final List<Form> elements = new ArrayList<>();
+ private final Template body;
+ private final List<Template> elements = new ArrayList<>();
- ListSection(CursorRange range, String name, Cursor nameOffset, Form body) {
+ ListSection(CursorRange range, String name, Cursor nameOffset, Template body) {
super(range);
this.name = name;
this.nameOffset = new Cursor(nameOffset);
@@ -27,28 +27,35 @@ class ListSection extends Section {
Cursor nameOffset() { return new Cursor(nameOffset); }
@Override
- void setForm(Form form) {
- super.setForm(form);
- body.setParent(form);
+ void setTemplate(Template template) {
+ super.setTemplate(template);
+ body.setParent(template);
}
- Form add() {
- Form element = body.copy();
- element.setParent(form());
+ Template add() {
+ Template element = body.snapshot();
+ element.setParent(template());
elements.add(element);
return element;
}
@Override
void appendTo(StringBuilder buffer) {
- elements.forEach(form -> form.appendTo(buffer));
+ elements.forEach(template -> template.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);
+ ListSection newSection = sectionList.appendListSection(name, nameOffset, range().end(), body);
+
+ elements.stream()
+ .map(template -> {
+ Template templateCopy = template.snapshot();
+ templateCopy.setParent(template());
+ return templateCopy;
+ })
+ .forEach(newSection.elements::add);
}
}
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Section.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Section.java
index 234915770f8..2c52fd5c34e 100644
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Section.java
+++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Section.java
@@ -3,6 +3,8 @@ package com.yahoo.vespa.hosted.node.admin.task.util.template;
import com.yahoo.vespa.hosted.node.admin.task.util.text.CursorRange;
+import java.util.Objects;
+
/**
* A section of a template text.
*
@@ -11,16 +13,16 @@ import com.yahoo.vespa.hosted.node.admin.task.util.text.CursorRange;
*/
abstract class Section {
private final CursorRange range;
- private Form form;
+ private Template template;
protected Section(CursorRange range) {
this.range = range;
}
- void setForm(Form form) { this.form = form; }
+ void setTemplate(Template template) { this.template = template; }
- /** Guaranteed to return non-null after FormBuilder::build() returns. */
- protected Form form() { return form; }
+ /** Guaranteed to return non-null after TemplateBuilder::build() returns. */
+ protected Template template() { return Objects.requireNonNull(template); }
protected CursorRange range() { return range; }
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 b9a8c4e8c41..066f6476bcb 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
@@ -9,38 +9,38 @@ 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.
+ * A mutable list of sections at the same level that can be used to build a template, e.g. the if-body.
*
* @author hakonhall
*/
class SectionList {
private final Cursor start;
private final Cursor end;
- private final FormBuilder formBuilder;
+ private final TemplateBuilder templateBuilder;
private final List<Section> sections = new ArrayList<>();
- SectionList(Cursor start, FormBuilder formBuilder) {
+ SectionList(Cursor start, TemplateBuilder templateBuilder) {
this.start = new Cursor(start);
this.end = new Cursor(start);
- this.formBuilder = formBuilder;
+ this.templateBuilder = templateBuilder;
}
CursorRange range() { return new CursorRange(start, end); }
- FormBuilder formBuilder() { return formBuilder; }
+ TemplateBuilder templateBuilder() { return templateBuilder; }
List<Section> sections() { return List.copyOf(sections); }
void appendLiteralSection(Cursor end) {
CursorRange range = verifyAndUpdateEnd(end);
var section = new LiteralSection(range);
- formBuilder.addLiteralSection(section);
+ templateBuilder.addLiteralSection(section);
sections.add(section);
}
VariableSection appendVariableSection(String name, Cursor nameOffset, Cursor end) {
CursorRange range = verifyAndUpdateEnd(end);
var section = new VariableSection(range, name, nameOffset);
- formBuilder.addVariableSection(section);
+ templateBuilder.addVariableSection(section);
sections.add(section);
return section;
}
@@ -49,15 +49,16 @@ class SectionList {
SectionList ifSections, Optional<SectionList> elseSections) {
CursorRange range = verifyAndUpdateEnd(end);
var section = new IfSection(range, negated, name, nameOffset, ifSections, elseSections);
- formBuilder.addIfSection(section);
+ templateBuilder.addIfSection(section);
sections.add(section);
}
- void appendListSection(String name, Cursor nameOffset, Cursor end, Form body) {
+ ListSection appendListSection(String name, Cursor nameOffset, Cursor end, Template body) {
CursorRange range = verifyAndUpdateEnd(end);
var section = new ListSection(range, name, nameOffset, body);
- formBuilder.addListSection(section);
+ templateBuilder.addListSection(section);
sections.add(section);
+ return section;
}
private CursorRange verifyAndUpdateEnd(Cursor newEnd) {
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 bb8466a0393..c87383ae620 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
@@ -1,6 +1,13 @@
// 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.CursorRange;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
/**
* The Java representation of a template text.
*
@@ -16,15 +23,22 @@ package com.yahoo.vespa.hosted.node.admin.task.util.template;
* id: a valid Java identifier
* </pre>
*
- * <p>To use the template create a form ({@link #newForm()}), fill the form (e.g.
- * {@link Form#set(String, String) Form.set()}), and render the String ({@link Form#render()}).</p>
+ * <p>Fill the template with variable values ({@link #set(String, String) set()}, set if conditions
+ * ({@link #set(String, boolean)}), add list elements ({@link #add(String) add()}, etc, and finally
+ * render it as a String ({@link #render()}).</p>
+ *
+ * <p>To reuse a template, create the template and work on snapshots of that ({@link #snapshot()}).</p>
*
- * @see Form
* @see TemplateFile
* @author hakonhall
*/
public class Template {
- private final Form form;
+ private Template parent = null;
+ private final CursorRange range;
+ private final List<Section> sections;
+
+ private final Map<String, String> values = new HashMap<>();
+ private final Map<String, ListSection> lists;
public static Template from(String text) { return from(text, new TemplateDescriptor()); }
@@ -32,10 +46,64 @@ public class Template {
return TemplateParser.parse(text, descriptor).template();
}
- Template(Form form) {
- this.form = form;
+ Template(CursorRange range, List<Section> sections, Map<String, ListSection> lists) {
+ this.range = new CursorRange(range);
+ this.sections = List.copyOf(sections);
+ this.lists = Map.copyOf(lists);
+ }
+
+ /** Must be set (if there is a parent) before any other method. */
+ void setParent(Template parent) { this.parent = parent; }
+
+ /** Set the value of a variable, e.g. %{=color}. */
+ public Template set(String name, String value) {
+ values.put(name, value);
+ return this;
}
- public Form newForm() { return form.copy(); }
+ /** Set the value of a variable and/or if-condition. */
+ public Template set(String name, boolean value) { return set(name, Boolean.toString(value)); }
+
+ public Template set(String name, int value) { return set(name, Integer.toString(value)); }
+ public Template set(String name, long value) { return set(name, Long.toString(value)); }
+ public Template set(String name, String format, String first, String... rest) {
+ var args = new Object[1 + rest.length];
+ args[0] = first;
+ System.arraycopy(rest, 0, args, 1, rest.length);
+ var value = String.format(format, args);
+
+ return set(name, value);
+ }
+
+ /** Add an instance of a list section after any previously added (for the given name) */
+ public Template add(String name) {
+ var section = lists.get(name);
+ if (section == null) {
+ throw new NoSuchNameTemplateException(range, name);
+ }
+ return section.add();
+ }
+
+ public String render() {
+ var buffer = new StringBuilder((int) (range.length() * 1.2 + 128));
+ appendTo(buffer);
+ return buffer.toString();
+ }
+
+ public void appendTo(StringBuilder buffer) { sections.forEach(section -> section.appendTo(buffer)); }
+
+ /** Returns a deep copy of this. No changes to this affects the returned template, and vice versa. */
+ public Template snapshot() {
+ var builder = new TemplateBuilder(range.start());
+ sections.forEach(section -> section.appendCopyTo(builder.topLevelSectionList()));
+ return builder.build();
+ }
+
+ Optional<String> getVariableValue(String name) {
+ String value = values.get(name);
+ if (value != null) return Optional.of(value);
+ if (parent != null) return parent.getVariableValue(name);
+ return Optional.empty();
+ }
}
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/TemplateBuilder.java
index cae5279f68a..8041a17fe74 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/TemplateBuilder.java
@@ -11,15 +11,15 @@ import java.util.Map;
/**
* @author hakonhall
*/
-class FormBuilder {
- /** The top-level section list in this form. */
+class TemplateBuilder {
+ /** The top-level section list in this template. */
private final SectionList sectionList;
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, ListSection> lists = new HashMap<>();
- FormBuilder(Cursor start) {
+ TemplateBuilder(Cursor start) {
this.sectionList = new SectionList(start, this);
}
@@ -31,7 +31,7 @@ class FormBuilder {
void addVariableSection(VariableSection section) {
// 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).
+ // determined when set on a template).
ListSection existing = lists.get(section.name());
if (existing != null)
@@ -44,7 +44,7 @@ class FormBuilder {
void addIfSection(IfSection section) {
// 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).
+ // determined when set on a template).
ListSection list = lists.get(section.name());
if (list != null)
@@ -73,9 +73,9 @@ class FormBuilder {
allSections.add(section);
}
- Form build() {
- var form = new Form(sectionList.range(), sectionList.sections(), lists);
- allSections.forEach(section -> section.setForm(form));
- return form;
+ Template build() {
+ var template = new Template(sectionList.range(), sectionList.sections(), lists);
+ allSections.forEach(section -> section.setTemplate(template));
+ return template;
}
}
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 a1a48c5e282..c2202dea4a0 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
@@ -15,7 +15,7 @@ class TemplateParser {
private final TemplateDescriptor descriptor;
private final Cursor start;
private final Cursor current;
- private final FormBuilder formBuilder;
+ private final TemplateBuilder templateBuilder;
static TemplateParser parse(String text, TemplateDescriptor descriptor) {
return parse(new TemplateDescriptor(descriptor), new Cursor(text), EnumSet.of(Sentinel.EOT));
@@ -23,7 +23,7 @@ class TemplateParser {
private static TemplateParser parse(TemplateDescriptor descriptor, Cursor start, EnumSet<Sentinel> sentinel) {
var parser = new TemplateParser(descriptor, start);
- parser.parse(parser.formBuilder.topLevelSectionList(), sentinel);
+ parser.parse(parser.templateBuilder.topLevelSectionList(), sentinel);
return parser;
}
@@ -33,10 +33,10 @@ class TemplateParser {
this.descriptor = descriptor;
this.start = new Cursor(start);
this.current = new Cursor(start);
- this.formBuilder = new FormBuilder(start);
+ this.templateBuilder = new TemplateBuilder(start);
}
- Template template() { return new Template(formBuilder.build()); }
+ Template template() { return templateBuilder.build(); }
private Sentinel parse(SectionList sectionList, EnumSet<Sentinel> sentinels) {
do {
@@ -113,7 +113,7 @@ class TemplateParser {
TemplateParser bodyParser = parse(descriptor, current, EnumSet.of(Sentinel.END));
current.set(bodyParser.current);
- sectionList.appendListSection(name, startOfName, current, bodyParser.formBuilder.build());
+ sectionList.appendListSection(name, startOfName, current, bodyParser.templateBuilder.build());
}
private void parseIfSection(SectionList sectionList) {
@@ -124,12 +124,12 @@ class TemplateParser {
String name = parseId();
parseEndDelimiter(true);
- SectionList ifSectionList = new SectionList(current, formBuilder);
+ SectionList ifSectionList = new SectionList(current, templateBuilder);
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));
+ elseSectionList = Optional.of(new SectionList(current, templateBuilder));
parse(elseSectionList.get(), EnumSet.of(Sentinel.END));
}
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/VariableSection.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/VariableSection.java
index bf211a01190..6a7bec2e485 100644
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/VariableSection.java
+++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/VariableSection.java
@@ -25,8 +25,8 @@ class VariableSection extends Section {
@Override
void appendTo(StringBuilder buffer) {
- String value = form().getVariableValue(name)
- .orElseThrow(() -> new TemplateNameNotSetException(name, nameOffset));
+ String value = template().getVariableValue(name)
+ .orElseThrow(() -> new TemplateNameNotSetException(name, nameOffset));
buffer.append(value);
}
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 40913184a67..8c276ff0491 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
@@ -13,61 +13,61 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
class TemplateFileTest {
@Test
void verifyVariableSection() {
- Form form = getForm("template1.tmp");
- form.set("varname", "varvalue");
+ Template template = getTemplate("template1.tmp");
+ template.set("varname", "varvalue");
assertEquals("variable section 'varvalue'\n" +
- "end of text\n", form.render());
+ "end of text\n", template.render());
}
@Test
void verifySimpleListSection() {
- Form form = getForm("template1.tmp");
- form.set("varname", "varvalue")
- .add("listname")
- .set("varname", "different varvalue")
- .set("varname2", "varvalue2");
+ Template template = getTemplate("template1.tmp");
+ template.set("varname", "varvalue")
+ .add("listname")
+ .set("varname", "different varvalue")
+ .set("varname2", "varvalue2");
assertEquals("variable section 'varvalue'\n" +
"same variable section 'different varvalue'\n" +
"different variable section 'varvalue2'\n" +
"between ends\n" +
- "end of text\n", form.render());
+ "end of text\n", template.render());
}
@Test
void verifyNestedListSection() {
- Form form = getForm("template2.tmp");
- Form A0 = form.add("listA");
- Form A0B0 = A0.add("listB");
- Form A0B1 = A0.add("listB");
+ Template template = getTemplate("template2.tmp");
+ Template A0 = template.add("listA");
+ Template A0B0 = A0.add("listB");
+ Template A0B1 = A0.add("listB");
- Form A1 = form.add("listA");
- Form A1B0 = A1.add("listB");
+ Template A1 = template.add("listA");
+ Template A1B0 = A1.add("listB");
assertEquals("body A\n" +
"body B\n" +
"body B\n" +
"body A\n" +
"body B\n",
- form.render());
+ template.render());
}
@Test
void verifyVariableReferences() {
- Form form = getForm("template3.tmp");
- form.set("varname", "varvalue")
- .set("innerVarSetAtTop", "val2");
- form.add("l");
- form.add("l")
- .set("varname", "varvalue2");
+ Template template = getTemplate("template3.tmp");
+ template.set("varname", "varvalue")
+ .set("innerVarSetAtTop", "val2");
+ template.add("l");
+ template.add("l")
+ .set("varname", "varvalue2");
assertEquals("varvalue\n" +
"varvalue\n" +
"inner varvalue\n" +
"val2\n" +
"inner varvalue2\n" +
"val2\n",
- form.render());
+ template.render());
}
- private Form getForm(String filename) {
- return TemplateFile.read(Path.of("src/test/resources/" + filename)).newForm();
+ private Template getTemplate(String filename) {
+ return TemplateFile.read(Path.of("src/test/resources/" + filename));
}
} \ No newline at end of file
diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateTest.java
index 8d503dd4784..fd73d315af6 100644
--- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateTest.java
+++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateTest.java
@@ -11,25 +11,25 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class TemplateTest {
@Test
void verifyNewlineRemoval() {
- Form form = makeForm("a%{list a}\n" +
- "b%{end}\n" +
- "c%{list c-}\n" +
- "d%{end-}\n" +
- "e\n");
- form.add("a");
- form.add("c");
+ Template template = Template.from("a%{list a}\n" +
+ "b%{end}\n" +
+ "c%{list c-}\n" +
+ "d%{end-}\n" +
+ "e\n");
+ template.add("a");
+ template.add("c");
assertEquals("a\n" +
"b\n" +
"cde\n",
- form.render());
+ template.render());
}
@Test
void verifyIfSection() {
Template template = Template.from("Hello%{if cond} world%{end}!");
- assertEquals("Hello world!", template.newForm().set("cond", true).render());
- assertEquals("Hello!", template.newForm().set("cond", false).render());
+ assertEquals("Hello world!", template.snapshot().set("cond", true).render());
+ assertEquals("Hello!", template.snapshot().set("cond", false).render());
}
@Test
@@ -40,26 +40,26 @@ public class TemplateTest {
"list: %{list formname}element%{end}\n" +
"%{end-}\n");
- assertEquals("", template.newForm().set("cond", false).render());
+ assertEquals("", template.snapshot().set("cond", false).render());
assertEquals("var: varvalue\n" +
"if: \n" +
"list: \n",
- template.newForm()
+ template.snapshot()
.set("cond", true)
.set("varname", "varvalue")
.set("inner", true)
.render());
- Form form = template.newForm()
- .set("cond", true)
- .set("varname", "varvalue")
- .set("inner", false);
- form.add("formname");
+ Template template2 = template.snapshot()
+ .set("cond", true)
+ .set("varname", "varvalue")
+ .set("inner", false);
+ template2.add("formname");
assertEquals("var: varvalue\n" +
"if: inner is false\n" +
- "list: element\n", form.render());
+ "list: element\n", template2.render());
}
@Test
@@ -69,11 +69,7 @@ public class TemplateTest {
"%{else-}\n" +
"else body\n" +
"%{end-}\n");
- assertEquals("if body\n", template.newForm().set("cond", true).render());
- assertEquals("else body\n", template.newForm().set("cond", false).render());
- }
-
- private Form makeForm(String templateText) {
- return Template.from(templateText).newForm();
+ assertEquals("if body\n", template.snapshot().set("cond", true).render());
+ assertEquals("else body\n", template.snapshot().set("cond", false).render());
}
}