diff options
author | Valerij Fredriksen <freva@users.noreply.github.com> | 2022-01-12 12:07:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-12 12:07:08 +0100 |
commit | 0ea79872374efcefcd4ba60e3cd7ccd8f7f07d18 (patch) | |
tree | 30c6dd808986ed37faf75780e8ba2b0a34d9a926 | |
parent | 55e9ce71399cded65a9ded3aaf45ae71718bf966 (diff) | |
parent | 66436434a7ceb272852fcb431f0d4271f3365b62 (diff) |
Merge pull request #20770 from vespa-engine/hakonhall/hide-snapshot-and-render-from-template-list-elements
Hide snapshot() and render() from template list elements
6 files changed, 132 insertions, 122 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 new file mode 100644 index 00000000000..42e13d63c19 --- /dev/null +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Form.java @@ -0,0 +1,30 @@ +// 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; + +/** + * Public methods common to both Template and ListElement. + * + * @author hakonhall + */ +public interface Form { + /** Set the value of a variable, e.g. %{=color}. */ + Template set(String name, String value); + + /** Set the value of a variable and/or if-condition. */ + default Template set(String name, boolean value) { return set(name, Boolean.toString(value)); } + + default Template set(String name, int value) { return set(name, Integer.toString(value)); } + default Template set(String name, long value) { return set(name, Long.toString(value)); } + + default 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) */ + ListElement add(String name); +} diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/ListElement.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/ListElement.java new file mode 100644 index 00000000000..24bb9ea6523 --- /dev/null +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/ListElement.java @@ -0,0 +1,17 @@ +// 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; + +/** + * @author hakonhall + */ +public class ListElement implements Form { + private final Template template; + + ListElement(Template template) { this.template = template; } + + @Override + public Template set(String name, String value) { return template.set(name, value); } + + @Override + public ListElement add(String name) { return new ListElement(template.addElement(name)); } +} 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 77d50a392c8..41e8c3e65ce 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,8 +1,10 @@ // 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.file.UnixPath; import com.yahoo.vespa.hosted.node.admin.task.util.text.CursorRange; +import java.nio.file.Path; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -29,10 +31,9 @@ import java.util.Optional; * * <p>To reuse a template, create the template and work on snapshots of that ({@link #snapshot()}).</p> * - * @see TemplateFile * @author hakonhall */ -public class Template { +public class Template implements Form { private Template parent = null; private final CursorRange range; private final List<Section> sections; @@ -40,8 +41,13 @@ public class Template { 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()); } + public static Template at(Path path) { return at(path, new TemplateDescriptor()); } + public static Template at(Path path, TemplateDescriptor descriptor) { + String content = new UnixPath(path).readUtf8File(); + return Template.from(content, descriptor); + } + public static Template from(String text) { return from(text, new TemplateDescriptor()); } public static Template from(String text, TemplateDescriptor descriptor) { return TemplateParser.parse(text, descriptor).template(); } @@ -52,38 +58,15 @@ public class Template { 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}. */ + @Override public Template set(String name, String value) { values.put(name, value); return this; } - /** 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(); - } + @Override + public ListElement add(String name) { return new ListElement(addElement(name)); } public String render() { var buffer = new StringBuilder((int) (range.length() * 1.2 + 128)); @@ -102,6 +85,17 @@ public class Template { return template; } + /** Must be called (if there is a parent) before any other method. */ + void setParent(Template parent) { this.parent = parent; } + + Template addElement(String name) { + var section = lists.get(name); + if (section == null) { + throw new NoSuchNameTemplateException(range, name); + } + return section.add(); + } + Optional<String> getVariableValue(String name) { String value = values.get(name); if (value != null) return Optional.of(value); diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateFile.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateFile.java deleted file mode 100644 index 0c1a26f4f65..00000000000 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateFile.java +++ /dev/null @@ -1,20 +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.file.UnixPath; - -import java.nio.file.Path; - -/** - * Parses a template file, see {@link Template} for details. - * - * @author hakonhall - */ -public class TemplateFile { - public static Template read(Path path) { return read(path, new TemplateDescriptor()); } - - public static Template read(Path path, TemplateDescriptor descriptor) { - String content = new UnixPath(path).readUtf8File(); - return Template.from(content, descriptor); - } -} 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 deleted file mode 100644 index 8c276ff0491..00000000000 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateFileTest.java +++ /dev/null @@ -1,73 +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 org.junit.jupiter.api.Test; - -import java.nio.file.Path; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * @author hakonhall - */ -class TemplateFileTest { - @Test - void verifyVariableSection() { - Template template = getTemplate("template1.tmp"); - template.set("varname", "varvalue"); - assertEquals("variable section 'varvalue'\n" + - "end of text\n", template.render()); - } - - @Test - void verifySimpleListSection() { - 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", template.render()); - } - - @Test - void verifyNestedListSection() { - Template template = getTemplate("template2.tmp"); - Template A0 = template.add("listA"); - Template A0B0 = A0.add("listB"); - Template A0B1 = A0.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", - template.render()); - } - - @Test - void verifyVariableReferences() { - 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", - template.render()); - } - - 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 fb5f8e74b73..e010b9780c6 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 @@ -3,6 +3,8 @@ package com.yahoo.vespa.hosted.node.admin.task.util.template; import org.junit.jupiter.api.Test; +import java.nio.file.Path; + import static org.junit.jupiter.api.Assertions.assertEquals; /** @@ -88,4 +90,64 @@ public class TemplateTest { .set("area", "Norway"); assertEquals("hello worldhello Norway", snapshot.render()); } + + @Test + void verifyVariableSection() { + Template template = getTemplate("template1.tmp"); + template.set("varname", "varvalue"); + assertEquals("variable section 'varvalue'\n" + + "end of text\n", template.render()); + } + + @Test + void verifySimpleListSection() { + 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", template.render()); + } + + @Test + void verifyNestedListSection() { + Template template = getTemplate("template2.tmp"); + ListElement A0 = template.add("listA"); + ListElement A0B0 = A0.add("listB"); + ListElement A0B1 = A0.add("listB"); + + ListElement A1 = template.add("listA"); + ListElement A1B0 = A1.add("listB"); + assertEquals("body A\n" + + "body B\n" + + "body B\n" + + "body A\n" + + "body B\n", + template.render()); + } + + @Test + void verifyVariableReferences() { + 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", + template.render()); + } + + private Template getTemplate(String filename) { + return Template.at(Path.of("src/test/resources/" + filename)); + } } |