diff options
author | Lester Solbakken <lesters@oath.com> | 2018-12-18 10:37:27 +0100 |
---|---|---|
committer | Lester Solbakken <lesters@oath.com> | 2018-12-18 10:37:27 +0100 |
commit | 7caa60913e4db267f7d7bdfe0e1de90ec12db13f (patch) | |
tree | bbb2e77e81d2823fc7c1048a39a7d38c20edfc44 /config | |
parent | 305d22637387d183be17a0582b1ced76f2b44982 (diff) |
Add url config type
Diffstat (limited to 'config')
3 files changed, 77 insertions, 21 deletions
diff --git a/config/src/main/java/com/yahoo/vespa/config/ConfigDefinition.java b/config/src/main/java/com/yahoo/vespa/config/ConfigDefinition.java index 4c4a1b69082..b969220ee05 100644 --- a/config/src/main/java/com/yahoo/vespa/config/ConfigDefinition.java +++ b/config/src/main/java/com/yahoo/vespa/config/ConfigDefinition.java @@ -34,6 +34,7 @@ public class ConfigDefinition implements Comparable<ConfigDefinition> { private Map<String, RefDef> referenceDefs = new LinkedHashMap<String, RefDef>(); private Map<String, FileDef> fileDefs = new LinkedHashMap<String, FileDef>(); private Map<String, PathDef> pathDefs = new LinkedHashMap<>(); + private Map<String, UrlDef> urlDefs = new LinkedHashMap<>(); private Map<String, StructDef> structDefs = new LinkedHashMap<String, StructDef>(); private Map<String, InnerArrayDef> innerArrayDefs = new LinkedHashMap<String, InnerArrayDef>(); private Map<String, ArrayDef> arrayDefs = new LinkedHashMap<String, ArrayDef>(); @@ -100,6 +101,8 @@ public class ConfigDefinition implements Comparable<ConfigDefinition> { verifyFile(id); } else if (pathDefs.containsKey(id)) { verifyPath(id); + } else if (urlDefs.containsKey(id)) { + verifyUrl(id); } else if (boolDefs.containsKey(id)) { verifyBool(id, val); } else if (intDefs.containsKey(id)) { @@ -605,6 +608,19 @@ public class ConfigDefinition implements Comparable<ConfigDefinition> { } } + public static class UrlDef implements DefaultValued<String>{ + private String defVal; + + public UrlDef(String defVal) { + this.defVal = defVal; + } + + @Override + public String getDefVal() { + return defVal; + } + } + public void addEnumDef(String id, EnumDef def) { enumDefs.put(id, def); } @@ -706,6 +722,14 @@ public class ConfigDefinition implements Comparable<ConfigDefinition> { pathDefs.put(refId, new PathDef(null)); } + public void addUrlDef(String url, String defVal) { + urlDefs.put(url, new UrlDef(defVal)); + } + + public void addUrlDef(String url) { + urlDefs.put(url, new UrlDef(null)); + } + public Map<String, StringDef> getStringDefs() { return stringDefs; } @@ -969,6 +993,14 @@ public class ConfigDefinition implements Comparable<ConfigDefinition> { return true; } + private boolean verifyUrl(String id) { + if (!urlDefs.containsKey(id)) { + defFail("No such url in " + verifyWarning(id)); + return false; + } + return true; + } + private boolean verifyBool(String id) { if (!boolDefs.containsKey(id)) { defFail("No such bool in " + verifyWarning(id)); diff --git a/config/src/main/java/com/yahoo/vespa/config/ConfigDefinitionBuilder.java b/config/src/main/java/com/yahoo/vespa/config/ConfigDefinitionBuilder.java index c4a8bf8f864..0121e47b9ae 100644 --- a/config/src/main/java/com/yahoo/vespa/config/ConfigDefinitionBuilder.java +++ b/config/src/main/java/com/yahoo/vespa/config/ConfigDefinitionBuilder.java @@ -48,8 +48,8 @@ public class ConfigDefinitionBuilder { new ConfigDefinition.TypeSpec(name, ((LeafCNode) node).getType(), null, enumValues, null, null)); } else if (node.isMap) { - //System.out.println("Adding leaf map node " + name); - def.leafMapDef(name).setTypeSpec(new ConfigDefinition.TypeSpec(name, ((LeafCNode) node).getType(), null, null, null, null)); + //System.out.println("Adding leaf map node " + name); + def.leafMapDef(name).setTypeSpec(new ConfigDefinition.TypeSpec(name, ((LeafCNode) node).getType(), null, null, null, null)); } else { //System.out.println("Adding basic node " + name); if (node instanceof LeafCNode.IntegerLeaf) { @@ -67,7 +67,9 @@ public class ConfigDefinitionBuilder { addNode(def, (LeafCNode.FileLeaf) node); } else if (node instanceof LeafCNode.PathLeaf) { addNode(def, (LeafCNode.PathLeaf) node); - }else if (node instanceof LeafCNode.StringLeaf) { + } else if (node instanceof LeafCNode.UrlLeaf) { + addNode(def, (LeafCNode.UrlLeaf) node); + } else if (node instanceof LeafCNode.StringLeaf) { addNode(def, (LeafCNode.StringLeaf) node); } else if (node instanceof LeafCNode.EnumLeaf) { addNode(def, (LeafCNode.EnumLeaf) node); @@ -87,9 +89,9 @@ public class ConfigDefinitionBuilder { } } } else if (node.isMap) { - //System.out.println("Adding struct map node " + name); - newDef = def.structMapDef(name); - if (node.getChildren() != null && node.getChildren().length > 0) { + //System.out.println("Adding struct map node " + name); + newDef = def.structMapDef(name); + if (node.getChildren() != null && node.getChildren().length > 0) { for (CNode childNode : node.getChildren()) { //System.out.println("\tChild node " + childNode.getName()); addNode(newDef, childNode); @@ -174,6 +176,14 @@ public class ConfigDefinitionBuilder { } } + static void addNode(ConfigDefinition def, LeafCNode.UrlLeaf leaf) { + if (leaf.getDefaultValue() != null) { + def.addUrlDef(leaf.getName(), leaf.getDefaultValue().getValue()); + } else { + def.addUrlDef(leaf.getName(), null); + } + } + static void addNode(ConfigDefinition def, LeafCNode.EnumLeaf leaf) { if (leaf.getDefaultValue() != null) { def.addEnumDef(leaf.getName(), Arrays.asList(leaf.getLegalValues()), leaf.getDefaultValue().getValue()); diff --git a/config/src/main/java/com/yahoo/vespa/config/ConfigPayloadApplier.java b/config/src/main/java/com/yahoo/vespa/config/ConfigPayloadApplier.java index d406dfe2d77..8a12405d505 100644 --- a/config/src/main/java/com/yahoo/vespa/config/ConfigPayloadApplier.java +++ b/config/src/main/java/com/yahoo/vespa/config/ConfigPayloadApplier.java @@ -4,6 +4,7 @@ package com.yahoo.vespa.config; import com.yahoo.config.ConfigBuilder; import com.yahoo.config.ConfigInstance; import com.yahoo.config.FileReference; +import com.yahoo.config.UrlReference; import com.yahoo.log.LogLevel; import com.yahoo.yolean.Exceptions; import com.yahoo.slime.ArrayTraverser; @@ -254,6 +255,11 @@ public class ConfigPayloadApplier<T extends ConfigInstance.Builder> { if (isPathField(builder, methodName)) { FileReference wrappedPath = resolvePath(Utf8.toString(value.asUtf8())); invokeSetter(builder, methodName, wrappedPath); + + // Need to convert url into actual file if 'url' type is used + } else if (isUrlField(builder, methodName)) { + throw new UnsupportedOperationException("'url' type is not yet implemented"); + } else { Object object = getValueFromInspector(value); invokeSetter(builder, methodName, object); @@ -337,42 +343,50 @@ public class ConfigPayloadApplier<T extends ConfigInstance.Builder> { * Checks whether or not this field is of type 'path', in which * case some special handling might be needed. Caches the result. */ - private Set<String> pathFieldSet = new HashSet<>(); private boolean isPathField(Object builder, String methodName) { - String key = pathFieldKey(builder, methodName); - if (pathFieldSet.contains(key)) { + // Paths are stored as FileReference in Builder. + return isFieldType(builder, methodName, FileReference.class); + } + + private boolean isUrlField(Object builder, String methodName) { + // Urls are stored as UrlReference in Builder. + return isFieldType(builder, methodName, UrlReference.class); + } + + private Set<String> fieldSet = new HashSet<>(); + private boolean isFieldType(Object builder, String methodName, java.lang.reflect.Type type) { + String key = fieldKey(builder, methodName); + if (fieldSet.contains(key)) { return true; } - boolean isPath = false; + boolean isType = false; try { Field field = builder.getClass().getDeclaredField(methodName); - //Paths are stored as FileReference in Builder. java.lang.reflect.Type fieldType = field.getGenericType(); - if (fieldType instanceof Class<?> && fieldType == FileReference.class) { - isPath = true; + if (fieldType instanceof Class<?> && fieldType == type) { + isType = true; } else if (fieldType instanceof ParameterizedType) { - isPath = isParameterizedWithPath((ParameterizedType) fieldType); + isType = isParameterizedWith((ParameterizedType) fieldType, type); } } catch (NoSuchFieldException e) { } - if (isPath) { - pathFieldSet.add(key); + if (isType) { + fieldSet.add(key); } - return isPath; + return isType; } - private static String pathFieldKey(Object builder, String methodName) { + private static String fieldKey(Object builder, String methodName) { return builder.getClass().getName() + "." + methodName; } - private boolean isParameterizedWithPath(ParameterizedType fieldType) { + private boolean isParameterizedWith(ParameterizedType fieldType, java.lang.reflect.Type type) { int numTypeArgs = fieldType.getActualTypeArguments().length; if (numTypeArgs > 0) - return fieldType.getActualTypeArguments()[numTypeArgs - 1] == FileReference.class; + return fieldType.getActualTypeArguments()[numTypeArgs - 1] == type; return false; } - private String capitalize(String name) { StringBuilder sb = new StringBuilder(); sb.append(name.substring(0, 1).toUpperCase()).append(name.substring(1)); |