You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ch...@apache.org on 2019/02/13 09:46:09 UTC

[flink] branch release-1.7 updated: [FLINK-11585][docs] Fix prefix matching

This is an automated email from the ASF dual-hosted git repository.

chesnay pushed a commit to branch release-1.7
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/release-1.7 by this push:
     new 26abcfd  [FLINK-11585][docs] Fix prefix matching
26abcfd is described below

commit 26abcfd4934d5ddae517f66f4485ad5d0c2f3593
Author: zentol <ch...@apache.org>
AuthorDate: Tue Feb 12 15:16:47 2019 +0100

    [FLINK-11585][docs] Fix prefix matching
---
 .../configuration/ConfigOptionsDocGenerator.java   | 21 +++++----
 .../ConfigOptionsDocGeneratorTest.java             | 53 ++++++++++++++++++++++
 2 files changed, 66 insertions(+), 8 deletions(-)

diff --git a/flink-docs/src/main/java/org/apache/flink/docs/configuration/ConfigOptionsDocGenerator.java b/flink-docs/src/main/java/org/apache/flink/docs/configuration/ConfigOptionsDocGenerator.java
index 36a2203..ed8c237 100644
--- a/flink-docs/src/main/java/org/apache/flink/docs/configuration/ConfigOptionsDocGenerator.java
+++ b/flink-docs/src/main/java/org/apache/flink/docs/configuration/ConfigOptionsDocGenerator.java
@@ -332,11 +332,20 @@ public class ConfigOptionsDocGenerator {
 
 		private Node findGroupRoot(String key) {
 			String[] keyComponents = key.split("\\.");
+			Node lastRootNode = root;
 			Node currentNode = root;
 			for (String keyComponent : keyComponents) {
-				currentNode = currentNode.findChild(keyComponent);
+				final Node childNode = currentNode.getChild(keyComponent);
+				if (childNode == null) {
+					break;
+				} else {
+					currentNode = childNode;
+					if (currentNode.isGroupRoot()) {
+						lastRootNode = currentNode;
+					}
+				}
 			}
-			return currentNode.isGroupRoot() ? currentNode : root;
+			return lastRootNode;
 		}
 
 		private static class Node {
@@ -353,12 +362,8 @@ public class ConfigOptionsDocGenerator {
 				return child;
 			}
 
-			private Node findChild(String keyComponent) {
-				Node child = children.get(keyComponent);
-				if (child == null) {
-					return this;
-				}
-				return child;
+			private Node getChild(String keyComponent) {
+				return children.get(keyComponent);
 			}
 
 			private void assignOption(OptionWithMetaInfo option) {
diff --git a/flink-docs/src/test/java/org/apache/flink/docs/configuration/ConfigOptionsDocGeneratorTest.java b/flink-docs/src/test/java/org/apache/flink/docs/configuration/ConfigOptionsDocGeneratorTest.java
index c0fb7c9..438c108 100644
--- a/flink-docs/src/test/java/org/apache/flink/docs/configuration/ConfigOptionsDocGeneratorTest.java
+++ b/flink-docs/src/test/java/org/apache/flink/docs/configuration/ConfigOptionsDocGeneratorTest.java
@@ -38,7 +38,10 @@ import java.nio.charset.StandardCharsets;
 import java.nio.file.Paths;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
 
 /**
@@ -91,6 +94,56 @@ public class ConfigOptionsDocGeneratorTest {
 	}
 
 	@ConfigGroups(groups = {
+		@ConfigGroup(name = "group1", keyPrefix = "a.b"),
+		@ConfigGroup(name = "group2", keyPrefix = "a.b.c.d")})
+	static class TestConfigPrefix {
+		// should end up in the default group
+		public static ConfigOption<Integer> option1 = ConfigOptions
+			.key("a.option")
+			.defaultValue(2);
+
+		// should end up in group1, perfect key-prefix match
+		public static ConfigOption<String> option2 = ConfigOptions
+			.key("a.b.option")
+			.noDefaultValue();
+
+		// should end up in group1, full key-prefix match
+		public static ConfigOption<Integer> option3 = ConfigOptions
+			.key("a.b.c.option")
+			.defaultValue(2);
+
+		// should end up in group1, full key-prefix match for group 1, partial match for group 2
+		// checks that the generator remembers the last encountered root node
+		public static ConfigOption<Integer> option4 = ConfigOptions
+			.key("a.b.c.e.option")
+			.defaultValue(2);
+
+		// should end up in the default group, since no group exists with prefix "a.c"
+		// checks that the generator does not ignore components (like ignoring "c" to find a match "a.b")
+		public static ConfigOption<String> option5 = ConfigOptions
+			.key("a.c.b.option")
+			.noDefaultValue();
+	}
+
+	@Test
+	public void testLongestPrefixMatching() {
+		final List<Tuple2<ConfigGroup, String>> tables = ConfigOptionsDocGenerator.generateTablesForClass(
+			TestConfigPrefix.class);
+
+		assertEquals(3, tables.size());
+		final Map<String, String> tablesConverted = new HashMap<>(tables.size());
+		for (final Tuple2<ConfigGroup, String> table : tables) {
+			tablesConverted.put(table.f0 != null ? table.f0.name() : "default", table.f1);
+		}
+
+		assertThat(tablesConverted.get("group1"), containsString("a.b.option"));
+		assertThat(tablesConverted.get("group1"), containsString("a.b.c.option"));
+		assertThat(tablesConverted.get("group1"), containsString("a.b.c.e.option"));
+		assertThat(tablesConverted.get("default"), containsString("a.option"));
+		assertThat(tablesConverted.get("default"), containsString("a.c.b.option"));
+	}
+
+	@ConfigGroups(groups = {
 		@ConfigGroup(name = "firstGroup", keyPrefix = "first"),
 		@ConfigGroup(name = "secondGroup", keyPrefix = "second")})
 	static class TestConfigMultipleSubGroup {