From 60d3cabea2223095ddf2960fef1d3986f91bcd00 Mon Sep 17 00:00:00 2001 From: jonmv Date: Thu, 25 Aug 2022 13:54:58 +0200 Subject: Use package name instead of general namespace --- .../com/yahoo/vespa/config/util/ConfigUtils.java | 27 ++++++++++++++-------- .../yahoo/vespa/config/util/ConfigUtilsTest.java | 27 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 9 deletions(-) (limited to 'config') diff --git a/config/src/main/java/com/yahoo/vespa/config/util/ConfigUtils.java b/config/src/main/java/com/yahoo/vespa/config/util/ConfigUtils.java index ea92738cc26..869b8a511ae 100644 --- a/config/src/main/java/com/yahoo/vespa/config/util/ConfigUtils.java +++ b/config/src/main/java/com/yahoo/vespa/config/util/ConfigUtils.java @@ -41,6 +41,9 @@ public class ConfigUtils { private static final Pattern intPattern = Pattern.compile(".*int.*range.*"); private static final Pattern doublePattern = Pattern.compile(".*double.*range.*"); private static final Pattern spaceBeforeCommaPatter = Pattern.compile("\\s,"); + private static final Pattern packageDirectivePattern = Pattern.compile("^\\s*package\\s*=(.*)$"); + private static final Pattern namespaceDirectivePattern = Pattern.compile("^\\s*namespace\\s*=(.*)$"); + private static final Pattern packagePattern = Pattern.compile("^(([a-z][a-z0-9_]*)+([.][a-z][a-z0-9_]*)*)$"); private static final String intFormattedMax = new DecimalFormat("#.#").format(0x7fffffff); private static final String intFormattedMin = new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.ENGLISH)).format(-0x80000000); private static final String doubleFormattedMax = new DecimalFormat("#.#").format(1e308); @@ -194,21 +197,27 @@ public class ConfigUtils { */ public static String getDefNamespace(Reader in) { List defLines = getDefLines(in); - String defPackage = getDefKeyword(defLines, "package"); - if (!defPackage.isEmpty()) return defPackage; - return getDefKeyword(defLines, "namespace"); + String declaredPackage = getDirective(defLines, packageDirectivePattern); + String declaredNamespace = getDirective(defLines, namespaceDirectivePattern); + return declaredPackage != null ? declaredPackage : declaredNamespace != null ? declaredNamespace : ""; } - private static String getDefKeyword(List defLines, String keyword) { - for (String line : defLines) { - if (line.startsWith(keyword)) { - String[] v = line.split("="); - return v[1].trim(); + static String getDirective(List defLines, Pattern directivePattern) { + Matcher matcher; + for (String defLine : defLines) { + if ((matcher = directivePattern.matcher(defLine)).matches()) { + if ((matcher = packagePattern.matcher(matcher.group(1))).matches()) + return matcher.group(1); + else + throw new IllegalArgumentException("package (or namespace) must consist of one or more segments joined by single dots (.), " + + "each starting with a lowercase letter (a-z), and then containing one or more " + + "lowercase letters (a-z), digits (0-9), or underscores (_)"); } } - return ""; + return null; } + private static List getDefLines(Reader in) { if (null == in) { throw new IllegalArgumentException("Null reader."); diff --git a/config/src/test/java/com/yahoo/vespa/config/util/ConfigUtilsTest.java b/config/src/test/java/com/yahoo/vespa/config/util/ConfigUtilsTest.java index f22016ca3e6..be0fe150bea 100644 --- a/config/src/test/java/com/yahoo/vespa/config/util/ConfigUtilsTest.java +++ b/config/src/test/java/com/yahoo/vespa/config/util/ConfigUtilsTest.java @@ -18,6 +18,7 @@ import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.fail; /** @@ -112,12 +113,38 @@ public class ConfigUtilsTest { @Test public void testGetNamespace() { + // namespace after version StringReader reader = new StringReader("version=1\nnamespace=a\nint a default=0"); assertEquals("a", ConfigUtils.getDefNamespace(reader)); + // namespace first reader = new StringReader("namespace=a\nversion=1\nint a default=0"); assertEquals("a", ConfigUtils.getDefNamespace(reader)); + // package after namespace + reader = new StringReader("namespace=a\npackage=b\nint a default=0"); + assertEquals("b", ConfigUtils.getDefNamespace(reader)); + + // package before namespace + reader = new StringReader("package=b\nnamespace=a\nint a default=0"); + assertEquals("b", ConfigUtils.getDefNamespace(reader)); + + // no actual package + assertEquals("package (or namespace) must consist of one or more segments joined by single dots (.), " + + "each starting with a lowercase letter (a-z), and then containing one or more lowercase letters (a-z), " + + "digits (0-9), or underscores (_)", + assertThrows(IllegalArgumentException.class, + () -> ConfigUtils.getDefNamespace(new StringReader("package= \t \nint a default=0"))) + .getMessage()); + + // too relaxed namespace + assertEquals("package (or namespace) must consist of one or more segments joined by single dots (.), " + + "each starting with a lowercase letter (a-z), and then containing one or more lowercase letters (a-z), " + + "digits (0-9), or underscores (_)", + assertThrows(IllegalArgumentException.class, + () -> ConfigUtils.getDefNamespace(new StringReader("namespace=a/b\nint a default=0"))) + .getMessage()); + // No namespace reader = new StringReader("version=1\nint a default=0"); assertEquals("", ConfigUtils.getDefNamespace(reader)); -- cgit v1.2.3