From 7caa60913e4db267f7d7bdfe0e1de90ec12db13f Mon Sep 17 00:00:00 2001 From: Lester Solbakken Date: Tue, 18 Dec 2018 10:37:27 +0100 Subject: Add url config type --- .../main/java/com/yahoo/config/LeafNodeMaps.java | 10 +++- .../main/java/com/yahoo/config/LeafNodeVector.java | 11 +++- .../src/main/java/com/yahoo/config/UrlNode.java | 65 ++++++++++++++++++++++ .../main/java/com/yahoo/config/UrlReference.java | 40 +++++++++++++ .../yahoo/config/ConfigInstanceBuilderTest.java | 7 ++- .../com/yahoo/config/ConfigInstanceEqualsTest.java | 4 ++ .../test/java/com/yahoo/config/UrlNodeTest.java | 26 +++++++++ .../resources/configdefinitions/function-test.def | 4 ++ 8 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 config-lib/src/main/java/com/yahoo/config/UrlNode.java create mode 100755 config-lib/src/main/java/com/yahoo/config/UrlReference.java create mode 100644 config-lib/src/test/java/com/yahoo/config/UrlNodeTest.java (limited to 'config-lib') diff --git a/config-lib/src/main/java/com/yahoo/config/LeafNodeMaps.java b/config-lib/src/main/java/com/yahoo/config/LeafNodeMaps.java index ae6040babc6..b6132a44e3c 100644 --- a/config-lib/src/main/java/com/yahoo/config/LeafNodeMaps.java +++ b/config-lib/src/main/java/com/yahoo/config/LeafNodeMaps.java @@ -1,11 +1,10 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.config; -import java.nio.file.Path; import java.util.Collections; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; +import java.util.stream.Collectors; /** * @author gjoranv @@ -62,4 +61,11 @@ public class LeafNodeMaps { return Collections.unmodifiableMap(pathNodeMap); } + public static Map asUrlNodeMap(Map urlReferenceMap) { + return Collections.unmodifiableMap( + urlReferenceMap.entrySet().stream().collect( + Collectors.toMap(Map.Entry::getKey, e -> new UrlNode(e.getValue())) + )); + } + } diff --git a/config-lib/src/main/java/com/yahoo/config/LeafNodeVector.java b/config-lib/src/main/java/com/yahoo/config/LeafNodeVector.java index 9bbd9b594f8..259afefdd69 100644 --- a/config-lib/src/main/java/com/yahoo/config/LeafNodeVector.java +++ b/config-lib/src/main/java/com/yahoo/config/LeafNodeVector.java @@ -1,6 +1,7 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.config; +import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -68,7 +69,15 @@ public class LeafNodeVector> extends NodeVecto List paths = new ArrayList<>(); for (FileReference fileReference : values) paths.add(Paths.get(fileReference.value())); - return new LeafNodeVector<>(paths, new PathNode()); } + + public static LeafNodeVector createUrlNodeVector(Collection values) { + List files = new ArrayList<>(); + for (UrlReference urlReference : values) + files.add(new File(urlReference.value())); + return new LeafNodeVector<>(files, new UrlNode()); + } + + } diff --git a/config-lib/src/main/java/com/yahoo/config/UrlNode.java b/config-lib/src/main/java/com/yahoo/config/UrlNode.java new file mode 100644 index 00000000000..0ed70ce0f50 --- /dev/null +++ b/config-lib/src/main/java/com/yahoo/config/UrlNode.java @@ -0,0 +1,65 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config; + +import edu.umd.cs.findbugs.annotations.NonNull; + +import java.io.File; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Represents a 'url' in a {@link ConfigInstance}, which will be downloaded + * and made available as a {@link File}. Stored in the config builder as a + * {@link UrlReference} to identify fields of this type for special handling + * in the ConfigPayloadApplier. + * + * @author lesters + */ +public class UrlNode extends LeafNode { + + private final UrlReference url; + + public UrlNode() { + url = null; + } + + public UrlNode(UrlReference url) { + super(true); + this.url = url; + this.value = new File(url.value()); + } + + public File value() { + return value; + } + + @Override + public String toString() { + return (value == null) ? "(null)" : '"' + getValue() + '"'; + } + + @Override + public String getValue() { + return value.toString(); + } + + @Override + protected boolean doSetValue(@NonNull String value) { + throw new UnsupportedOperationException("doSetValue should not be necessary since the library anymore!"); + } + + public UrlReference getUrlReference() { + return url; + } + + public static List toUrlReferences(List urlNodes) { + return urlNodes.stream().map(UrlNode::getUrlReference).collect(Collectors.toList()); + } + + public static Map toUrlReferenceMap(Map urlNodeMap) { + return urlNodeMap.entrySet().stream().collect( + Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getUrlReference())); + } + +} diff --git a/config-lib/src/main/java/com/yahoo/config/UrlReference.java b/config-lib/src/main/java/com/yahoo/config/UrlReference.java new file mode 100755 index 00000000000..0ec4fd8f8b8 --- /dev/null +++ b/config-lib/src/main/java/com/yahoo/config/UrlReference.java @@ -0,0 +1,40 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config; + +import java.util.Objects; + +/** + * Similar to {@link FileReference}, holds either a URL or a file path to the + * downloaded file depending on state. + * + * @author lesters + */ +public final class UrlReference { + + private final String value; + + public UrlReference(String value) { + this.value = Objects.requireNonNull(value); + } + + public String value() { + return value; + } + + @Override + public int hashCode() { + return value.hashCode(); + } + + @Override + public boolean equals(Object other) { + return other instanceof UrlReference && + value.equals(((UrlReference)other).value); + } + + @Override + public String toString() { + return "url '" + value + "'"; + } + +} diff --git a/config-lib/src/test/java/com/yahoo/config/ConfigInstanceBuilderTest.java b/config-lib/src/test/java/com/yahoo/config/ConfigInstanceBuilderTest.java index cc3515f95db..dc365cc60e7 100644 --- a/config-lib/src/test/java/com/yahoo/config/ConfigInstanceBuilderTest.java +++ b/config-lib/src/test/java/com/yahoo/config/ConfigInstanceBuilderTest.java @@ -159,6 +159,7 @@ public class ConfigInstanceBuilderTest refwithdef(":parent:"). fileVal("etc"). pathVal(FileReference.mockFileReferenceForUnitTesting(new File("pom.xml"))). + urlVal(new UrlReference("http://docs.vespa.ai")). boolarr(false). longarr(9223372036854775807L). longarr(-9223372036854775808L). @@ -173,6 +174,8 @@ public class ConfigInstanceBuilderTest stringMap("one", "first"). filemap("f1", "/var"). filemap("f2", "/store"). + urlMap("u1", new UrlReference("http://docs.vespa.ai/1")). + urlMap("u2", new UrlReference("http://docs.vespa.ai/2")). basicStruct(new BasicStruct.Builder(). foo("basicFoo"). @@ -198,6 +201,7 @@ public class ConfigInstanceBuilderTest enumval(Myarray.Enumval.INNER). refval(":parent:"). fileVal("file0"). + urlVal(new UrlReference("http://docs.vespa.ai/1")). anotherarray(new Myarray.Anotherarray.Builder(). foo(7)). myStruct(new Myarray.MyStruct.Builder(). @@ -209,6 +213,7 @@ public class ConfigInstanceBuilderTest enumval(Myarray.Enumval.INNER). refval(":parent:"). fileVal("file1"). + urlVal(new UrlReference("http://docs.vespa.ai/2")). anotherarray(new Myarray.Anotherarray.Builder(). foo(1). foo(2)). @@ -344,7 +349,7 @@ public class ConfigInstanceBuilderTest funcBuilder.intMap.put("three", 3); funcBuilder.myarray.get(1).intval(17); funcBuilder.myarray.get(0).anotherarray.get(0).foo(32); - funcBuilder.myarray.add(new Myarray.Builder().refval("refval").fileVal("fileval").myStruct(new Myarray.MyStruct.Builder().a(4))); + funcBuilder.myarray.add(new Myarray.Builder().refval("refval").fileVal("fileval").urlVal(new UrlReference("urlval")).myStruct(new Myarray.MyStruct.Builder().a(4))); funcBuilder.myStructMap.put("new", new MyStructMap.Builder().myString("string").myInt(13)); funcBuilder.basicStruct(new BasicStruct.Builder().bar(1234)); FunctionTestConfig function2 = new FunctionTestConfig(funcBuilder); diff --git a/config-lib/src/test/java/com/yahoo/config/ConfigInstanceEqualsTest.java b/config-lib/src/test/java/com/yahoo/config/ConfigInstanceEqualsTest.java index ca8b9aea2fe..06db64fb20c 100644 --- a/config-lib/src/test/java/com/yahoo/config/ConfigInstanceEqualsTest.java +++ b/config-lib/src/test/java/com/yahoo/config/ConfigInstanceEqualsTest.java @@ -131,6 +131,7 @@ public class ConfigInstanceEqualsTest { refval(":parent:"). fileVal("etc"). pathVal(FileReference.mockFileReferenceForUnitTesting(new File("pom.xml"))). + urlVal(new UrlReference("http://docs.vespa.ai")). boolarr(false). longarr(9223372036854775807L). longarr(-9223372036854775808L). @@ -140,6 +141,7 @@ public class ConfigInstanceEqualsTest { enumarr(Enumarr.VALUES). refarr(Arrays.asList(":parent:", ":parent", "parent:")). // test collection based setter fileArr("bin"). + urlArr(new UrlReference("http://docs.vespa.ai")). basicStruct(new BasicStruct.Builder(). foo("basicFoo"). @@ -162,6 +164,7 @@ public class ConfigInstanceEqualsTest { enumval(Myarray.Enumval.INNER). refval(":parent:"). fileVal("file0"). + urlVal(new UrlReference("http://docs.vespa.ai/1")). anotherarray(new Myarray.Anotherarray.Builder(). foo(7)). myStruct(new Myarray.MyStruct.Builder(). @@ -173,6 +176,7 @@ public class ConfigInstanceEqualsTest { enumval(Myarray.Enumval.INNER). refval(":parent:"). fileVal("file1"). + urlVal(new UrlReference("http://docs.vespa.ai/2")). anotherarray(new Myarray.Anotherarray.Builder(). foo(1). foo(2)). diff --git a/config-lib/src/test/java/com/yahoo/config/UrlNodeTest.java b/config-lib/src/test/java/com/yahoo/config/UrlNodeTest.java new file mode 100644 index 00000000000..d8d6cc0f1f1 --- /dev/null +++ b/config-lib/src/test/java/com/yahoo/config/UrlNodeTest.java @@ -0,0 +1,26 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +/** + * @author lesters + */ +public class UrlNodeTest { + + @Test + public void testSetValue() { + UrlNode url = new UrlNode(); + assertThat(url.toString(), is("(null)")); + + url = new UrlNode(new UrlReference("https://docs.vespa.ai/")); + assertThat(url.getUrlReference().value(), is("https://docs.vespa.ai/")); + + url = new UrlNode(new UrlReference("pom.xml")); + assertThat(url.getValue(), is("pom.xml")); + } + +} diff --git a/config-lib/src/test/resources/configdefinitions/function-test.def b/config-lib/src/test/resources/configdefinitions/function-test.def index 04d040a910b..73681d95544 100644 --- a/config-lib/src/test/resources/configdefinitions/function-test.def +++ b/config-lib/src/test/resources/configdefinitions/function-test.def @@ -44,6 +44,7 @@ refval reference restart refwithdef reference default=":parent:" restart fileVal file restart pathVal path restart +urlVal url boolarr[] bool restart intarr[] int restart @@ -54,12 +55,14 @@ enumarr[] enum { ARRAY, VALUES } restart refarr[] reference restart fileArr[] file restart pathArr[] path restart +urlArr[] url #This is a map of ints. intMap{} int restart stringMap{} string restart filemap{} file restart pathMap{} path restart +urlMap{} url # A basic struct basicStruct.foo string default="basic" restart @@ -80,6 +83,7 @@ myarray[].stringval[] string restart myarray[].enumval enum { INNER, ENUM, TYPE } default=TYPE restart myarray[].refval reference # Value in array without default restart myarray[].fileVal file restart +myarray[].urlVal url myarray[].anotherarray[].foo int default=-4 restart myarray[].myStruct.a int restart myarray[].myStruct.b int default=2 restart -- cgit v1.2.3