Merge pull request #17949 from jaymode/list_setting_exists

fix exists method for list settings when using numbered setting format
This commit is contained in:
Jay Modi 2016-04-28 12:25:48 -04:00
commit 34b5839487
2 changed files with 10 additions and 2 deletions

View File

@ -599,7 +599,6 @@ public class Setting<T> extends ToXContentToBytes {
return new Setting<List<T>>(new ListKey(key), return new Setting<List<T>>(new ListKey(key),
(s) -> arrayToParsableString(defaultStringValue.apply(s).toArray(Strings.EMPTY_ARRAY)), parser, properties) { (s) -> arrayToParsableString(defaultStringValue.apply(s).toArray(Strings.EMPTY_ARRAY)), parser, properties) {
private final Pattern pattern = Pattern.compile(Pattern.quote(key)+"(\\.\\d+)?");
@Override @Override
public String getRaw(Settings settings) { public String getRaw(Settings settings) {
String[] array = settings.getAsArray(getKey(), null); String[] array = settings.getAsArray(getKey(), null);
@ -610,6 +609,12 @@ public class Setting<T> extends ToXContentToBytes {
boolean hasComplexMatcher() { boolean hasComplexMatcher() {
return true; return true;
} }
@Override
public boolean exists(Settings settings) {
boolean exists = super.exists(settings);
return exists || settings.get(getKey() + ".0") != null;
}
}; };
} }

View File

@ -294,11 +294,13 @@ public class SettingTests extends ESTestCase {
Setting<List<String>> listSetting = Setting.listSetting("foo.bar", Arrays.asList("foo,bar"), (s) -> s.toString(), Setting<List<String>> listSetting = Setting.listSetting("foo.bar", Arrays.asList("foo,bar"), (s) -> s.toString(),
Property.Dynamic, Property.NodeScope); Property.Dynamic, Property.NodeScope);
List<String> value = listSetting.get(Settings.EMPTY); List<String> value = listSetting.get(Settings.EMPTY);
assertFalse(listSetting.exists(Settings.EMPTY));
assertEquals(1, value.size()); assertEquals(1, value.size());
assertEquals("foo,bar", value.get(0)); assertEquals("foo,bar", value.get(0));
List<String> input = Arrays.asList("test", "test1, test2", "test", ",,,,"); List<String> input = Arrays.asList("test", "test1, test2", "test", ",,,,");
Settings.Builder builder = Settings.builder().putArray("foo.bar", input.toArray(new String[0])); Settings.Builder builder = Settings.builder().putArray("foo.bar", input.toArray(new String[0]));
assertTrue(listSetting.exists(builder.build()));
value = listSetting.get(builder.build()); value = listSetting.get(builder.build());
assertEquals(input.size(), value.size()); assertEquals(input.size(), value.size());
assertArrayEquals(value.toArray(new String[0]), input.toArray(new String[0])); assertArrayEquals(value.toArray(new String[0]), input.toArray(new String[0]));
@ -311,6 +313,7 @@ public class SettingTests extends ESTestCase {
value = listSetting.get(builder.build()); value = listSetting.get(builder.build());
assertEquals(input.size(), value.size()); assertEquals(input.size(), value.size());
assertArrayEquals(value.toArray(new String[0]), input.toArray(new String[0])); assertArrayEquals(value.toArray(new String[0]), input.toArray(new String[0]));
assertTrue(listSetting.exists(builder.build()));
AtomicReference<List<String>> ref = new AtomicReference<>(); AtomicReference<List<String>> ref = new AtomicReference<>();
AbstractScopedSettings.SettingUpdater<List<String>> settingUpdater = listSetting.newUpdater(ref::set, logger); AbstractScopedSettings.SettingUpdater<List<String>> settingUpdater = listSetting.newUpdater(ref::set, logger);