From 2606066e30b62b8cbbca23da7ac275868009f405 Mon Sep 17 00:00:00 2001 From: jaymode Date: Sun, 24 Apr 2016 20:36:58 -0400 Subject: [PATCH] fix exists method for list settings when using numbered setting format The list settings parser supports retrieving lists defined in settings that use a key followed by a `.` and a number (for example `foo.bar.0`). However, the exists method would indicate that the provided settings do not contain a value for this setting. This change makes it so that the exists method now handles this format. --- .../java/org/elasticsearch/common/settings/Setting.java | 9 +++++++-- .../org/elasticsearch/common/settings/SettingTests.java | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/settings/Setting.java b/core/src/main/java/org/elasticsearch/common/settings/Setting.java index 52ff7475c88..975c7edb230 100644 --- a/core/src/main/java/org/elasticsearch/common/settings/Setting.java +++ b/core/src/main/java/org/elasticsearch/common/settings/Setting.java @@ -344,7 +344,7 @@ public class Setting extends ToXContentToBytes { return get(primary); } if (fallbackSetting == null) { - return get(secondary); + return get(secondary); } if (exists(secondary)) { return get(secondary); @@ -596,7 +596,6 @@ public class Setting extends ToXContentToBytes { return new Setting>(new ListKey(key), (s) -> arrayToParsableString(defaultStringValue.apply(s).toArray(Strings.EMPTY_ARRAY)), parser, properties) { - private final Pattern pattern = Pattern.compile(Pattern.quote(key)+"(\\.\\d+)?"); @Override public String getRaw(Settings settings) { String[] array = settings.getAsArray(getKey(), null); @@ -607,6 +606,12 @@ public class Setting extends ToXContentToBytes { boolean hasComplexMatcher() { return true; } + + @Override + public boolean exists(Settings settings) { + boolean exists = super.exists(settings); + return exists || settings.get(getKey() + ".0") != null; + } }; } diff --git a/core/src/test/java/org/elasticsearch/common/settings/SettingTests.java b/core/src/test/java/org/elasticsearch/common/settings/SettingTests.java index 29c27222ff8..3d627dc3f0f 100644 --- a/core/src/test/java/org/elasticsearch/common/settings/SettingTests.java +++ b/core/src/test/java/org/elasticsearch/common/settings/SettingTests.java @@ -290,11 +290,13 @@ public class SettingTests extends ESTestCase { Setting> listSetting = Setting.listSetting("foo.bar", Arrays.asList("foo,bar"), (s) -> s.toString(), Property.Dynamic, Property.NodeScope); List value = listSetting.get(Settings.EMPTY); + assertFalse(listSetting.exists(Settings.EMPTY)); assertEquals(1, value.size()); assertEquals("foo,bar", value.get(0)); List input = Arrays.asList("test", "test1, test2", "test", ",,,,"); Settings.Builder builder = Settings.builder().putArray("foo.bar", input.toArray(new String[0])); + assertTrue(listSetting.exists(builder.build())); value = listSetting.get(builder.build()); assertEquals(input.size(), value.size()); assertArrayEquals(value.toArray(new String[0]), input.toArray(new String[0])); @@ -307,6 +309,7 @@ public class SettingTests extends ESTestCase { value = listSetting.get(builder.build()); assertEquals(input.size(), value.size()); assertArrayEquals(value.toArray(new String[0]), input.toArray(new String[0])); + assertTrue(listSetting.exists(builder.build())); AtomicReference> ref = new AtomicReference<>(); AbstractScopedSettings.SettingUpdater> settingUpdater = listSetting.newUpdater(ref::set, logger);