diff options
author | Håkon Hallingstad <hakon@yahooinc.com> | 2022-01-10 20:12:24 +0100 |
---|---|---|
committer | Håkon Hallingstad <hakon@yahooinc.com> | 2022-01-10 20:12:28 +0100 |
commit | 234e243e84ffd7d7467ffd0e6c92e2982d1deab6 (patch) | |
tree | d7cfb39b55850fe6ae82491857b20ebe68646334 /node-admin | |
parent | 3c9f5a714250876dbd31482c363b5486baa3e361 (diff) |
Fail if variable name is used for subform name and vice versa
Diffstat (limited to 'node-admin')
2 files changed, 25 insertions, 14 deletions
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 a497d54ba3a..47ff28b2d6b 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 @@ -40,12 +40,15 @@ import java.util.function.Consumer; public class Template { private final CursorRange range; private final List<Consumer<FormBuilder>> sections; - private final Map<String, Cursor> names; + private final Map<String, Cursor> variables; + private final Map<String, Cursor> subforms; - public Template(CursorRange range, List<Consumer<FormBuilder>> sections, Map<String, Cursor> names) { + public Template(CursorRange range, List<Consumer<FormBuilder>> sections, + Map<String, Cursor> variables, Map<String, Cursor> subforms) { this.range = new CursorRange(range); this.sections = List.copyOf(sections); - this.names = Map.copyOf(names); + this.variables = Map.copyOf(variables); + this.subforms = Map.copyOf(subforms); } public static Template from(String text) { return from(text, new TemplateDescriptor()); } 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 07c87e3833c..17a813fd9f3 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 @@ -19,8 +19,11 @@ class TemplateBuilder { private final List<Consumer<FormBuilder>> sections = new ArrayList<>(); - /** The value contains the location of the name of a sample variable section (with that name). */ - private final Map<String, Cursor> names = new HashMap<>(); + /** Example location (value) of a variable name (key). */ + private final Map<String, Cursor> variables = new HashMap<>(); + + /** Location of the name of each subform. */ + private final Map<String, Cursor> subforms = new HashMap<>(); TemplateBuilder(Cursor start) { this.start = new Cursor(start); @@ -34,18 +37,30 @@ class TemplateBuilder { void appendVariableSection(String name, Cursor nameOffset, Cursor end) { CursorRange range = verifyAndUpdateEnd(end); + 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); - verifyNewName(name, nameCursor); + + Cursor existing = variables.get(name); + if (existing != null) + throw new NameAlreadyExistsTemplateException(name, existing, nameCursor); + + 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, names); + return new Template(range, sections, variables, subforms); } private CursorRange verifyAndUpdateEnd(Cursor newEnd) { @@ -53,11 +68,4 @@ class TemplateBuilder { this.end.set(newEnd); return range; } - - private void verifyNewName(String name, Cursor cursor) { - Cursor alreadyDefinedNameCursor = names.put(name, cursor); - if (alreadyDefinedNameCursor != null) { - throw new NameAlreadyExistsTemplateException(name, alreadyDefinedNameCursor, cursor); - } - } } |