diff options
author | Håkon Hallingstad <hakon@yahooinc.com> | 2022-01-10 20:42:56 +0100 |
---|---|---|
committer | Håkon Hallingstad <hakon@yahooinc.com> | 2022-01-10 20:42:58 +0100 |
commit | b20322290c63941e18e70a09923a306f185c4e3c (patch) | |
tree | 0ee8fa621f9b8f1b2db3e07a19a0d6190818aeb2 /node-admin | |
parent | ebfe0d07546b72d8a624fc57b6bf3d1f85d975d3 (diff) |
Moves adding sections to a SectionList
Diffstat (limited to 'node-admin')
3 files changed, 69 insertions, 33 deletions
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 new file mode 100644 index 00000000000..b5017246f31 --- /dev/null +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/SectionList.java @@ -0,0 +1,59 @@ +// 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; +import java.util.function.Consumer; + +/** + * A mutable list of sections at the same level that can be used to build a form, e.g. the if-body. + * + * @author hakonhall + */ +class SectionList { + private TemplateBuilder templateBuilder; + + private final Cursor start; + private final Cursor end; + + private final List<Consumer<FormBuilder>> sections = new ArrayList<>(); + + SectionList(Cursor start) { + this.start = new Cursor(start); + this.end = new Cursor(start); + } + + /** Must be invoked once before any other method. */ + void setTemplateBuilder(TemplateBuilder templateBuilder) { + this.templateBuilder = templateBuilder; + } + + void appendLiteralSection(Cursor end) { + CursorRange range = verifyAndUpdateEnd(end); + sections.add((FormBuilder builder) -> builder.addLiteralSection(range)); + } + + void appendVariableSection(String name, Cursor nameOffset, Cursor end) { + CursorRange range = verifyAndUpdateEnd(end); + templateBuilder.addVariable(name, nameOffset); + sections.add(formBuilder -> formBuilder.addVariableSection(range, name, nameOffset)); + } + + void appendSubformSection(String name, Cursor nameCursor, Cursor end, Template body) { + CursorRange range = verifyAndUpdateEnd(end); + templateBuilder.addSubform(name, nameCursor); + sections.add(formBuilder -> formBuilder.addSubformSection(range, name, body)); + } + + CursorRange range() { return new CursorRange(start, end); } + List<Consumer<FormBuilder>> sections() { return List.copyOf(sections); } + + private CursorRange verifyAndUpdateEnd(Cursor newEnd) { + var range = new CursorRange(this.end, newEnd); + this.end.set(newEnd); + return range; + } +} diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateBuilder.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateBuilder.java index 17a813fd9f3..d63ed7c9e70 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateBuilder.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateBuilder.java @@ -2,22 +2,15 @@ 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.HashMap; -import java.util.List; import java.util.Map; -import java.util.function.Consumer; /** * @author hakonhall */ class TemplateBuilder { - private final Cursor start; - private final Cursor end; - - private final List<Consumer<FormBuilder>> sections = new ArrayList<>(); + private final SectionList sections; /** Example location (value) of a variable name (key). */ private final Map<String, Cursor> variables = new HashMap<>(); @@ -26,27 +19,20 @@ class TemplateBuilder { private final Map<String, Cursor> subforms = new HashMap<>(); TemplateBuilder(Cursor start) { - this.start = new Cursor(start); - this.end = new Cursor(start); + sections = new SectionList(start); + sections.setTemplateBuilder(this); } - void appendLiteralSection(Cursor end) { - CursorRange range = verifyAndUpdateEnd(end); - sections.add((FormBuilder builder) -> builder.addLiteralSection(range)); - } + SectionList sectionList() { return sections; } - void appendVariableSection(String name, Cursor nameOffset, Cursor end) { - CursorRange range = verifyAndUpdateEnd(end); + void addVariable(String name, Cursor nameOffset) { Cursor existing = subforms.get(name); if (existing != null) throw new NameAlreadyExistsTemplateException(name, existing, nameOffset); variables.put(name, new Cursor(nameOffset)); - sections.add(formBuilder -> formBuilder.addVariableSection(range, name, nameOffset)); } - void appendSubformSection(String name, Cursor nameCursor, Cursor end, Template body) { - CursorRange range = verifyAndUpdateEnd(end); - + void addSubform(String name, Cursor nameCursor) { Cursor existing = variables.get(name); if (existing != null) throw new NameAlreadyExistsTemplateException(name, existing, nameCursor); @@ -54,18 +40,9 @@ class TemplateBuilder { existing = subforms.put(name, nameCursor); if (existing != null) throw new NameAlreadyExistsTemplateException(name, existing, nameCursor); - - sections.add(formBuilder -> formBuilder.addSubformSection(range, name, body)); } Template build() { - var range = new CursorRange(start, end); - return new Template(range, sections, variables, subforms); - } - - private CursorRange verifyAndUpdateEnd(Cursor newEnd) { - var range = new CursorRange(this.end, newEnd); - this.end.set(newEnd); - return range; + return new Template(sections.range(), sections.sections(), variables, subforms); } } 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 6e6baaefbbb..b39ca4c85fb 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 @@ -43,7 +43,7 @@ class TemplateParser { do { current.advanceTo(descriptor.startDelimiter()); if (!current.equals(start)) { - templateBuilder.appendLiteralSection(current); + templateBuilder.sectionList().appendLiteralSection(current); } if (current.eot()) { @@ -91,7 +91,7 @@ class TemplateParser { var nameStart = new Cursor(current); String name = parseId(); parseEndDelimiter(true); - templateBuilder.appendVariableSection(name, nameStart, current); + templateBuilder.sectionList().appendVariableSection(name, nameStart, current); } private void parseEndDirective() { @@ -107,7 +107,7 @@ class TemplateParser { TemplateParser bodyParser = parse(descriptor, current, FormEndsIn.END); current.set(bodyParser.current); - templateBuilder.appendSubformSection(name, startOfName, current, bodyParser.template()); + templateBuilder.sectionList().appendSubformSection(name, startOfName, current, bodyParser.template()); } private void skipRequiredWhitespaces() { |