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.
This commit is contained in:
jaymode 2016-04-24 20:36:58 -04:00
parent 5fe1916be9
commit 2606066e30
2 changed files with 10 additions and 2 deletions

View File

@ -344,7 +344,7 @@ public class Setting<T> extends ToXContentToBytes {
return get(primary); return get(primary);
} }
if (fallbackSetting == null) { if (fallbackSetting == null) {
return get(secondary); return get(secondary);
} }
if (exists(secondary)) { if (exists(secondary)) {
return get(secondary); return get(secondary);
@ -596,7 +596,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);
@ -607,6 +606,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

@ -290,11 +290,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]));
@ -307,6 +309,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);