* Create group settings with fallback. Signed-off-by: dblock <dblock@amazon.com> * Use protected fallbackSetting in Setting. Signed-off-by: dblock <dblock@amazon.com>
This commit is contained in:
parent
edbe5ae7e8
commit
a8c5e71eb9
|
@ -158,7 +158,7 @@ public class Setting<T> implements ToXContentObject {
|
|||
private final Key key;
|
||||
protected final Function<Settings, String> defaultValue;
|
||||
@Nullable
|
||||
private final Setting<T> fallbackSetting;
|
||||
protected final Setting<T> fallbackSetting;
|
||||
private final Function<String, T> parser;
|
||||
private final Validator<T> validator;
|
||||
private final EnumSet<Property> properties;
|
||||
|
@ -1016,6 +1016,12 @@ public class Setting<T> implements ToXContentObject {
|
|||
this.validator = validator;
|
||||
}
|
||||
|
||||
private GroupSetting(String key, Setting<Settings> fallback, Consumer<Settings> validator, Property... properties) {
|
||||
super(new GroupKey(key), fallback, (s) -> null, properties);
|
||||
this.key = key;
|
||||
this.validator = validator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isGroupSetting() {
|
||||
return true;
|
||||
|
@ -1038,6 +1044,9 @@ public class Setting<T> implements ToXContentObject {
|
|||
@Override
|
||||
public Settings get(Settings settings) {
|
||||
Settings byPrefix = settings.getByPrefix(getKey());
|
||||
if (byPrefix.size() == 0 && this.fallbackSetting != null) {
|
||||
byPrefix = fallbackSetting.get(settings);
|
||||
}
|
||||
validator.accept(byPrefix);
|
||||
return byPrefix;
|
||||
}
|
||||
|
@ -1049,8 +1058,12 @@ public class Setting<T> implements ToXContentObject {
|
|||
return true;
|
||||
}
|
||||
}
|
||||
if (this.fallbackSetting != null) {
|
||||
return fallbackSetting.exists(settings);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void diff(Settings.Builder builder, Settings source, Settings defaultSettings) {
|
||||
|
@ -1748,14 +1761,55 @@ public class Setting<T> implements ToXContentObject {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a group of settings prefixed by a key.
|
||||
*
|
||||
* @param key the group key for the setting
|
||||
* @param properties properties properties for this setting like scope, filtering...
|
||||
* @return the group setting object
|
||||
*/
|
||||
public static Setting<Settings> groupSetting(String key, Property... properties) {
|
||||
return groupSetting(key, (s) -> {}, properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a group of settings prefixed by a key.
|
||||
*
|
||||
* @param key the group key for the setting
|
||||
* @param validator a {@link Validator} for validating this setting
|
||||
* @param properties properties properties for this setting like scope, filtering...
|
||||
* @return the group setting object
|
||||
*/
|
||||
public static Setting<Settings> groupSetting(String key, Consumer<Settings> validator, Property... properties) {
|
||||
return new GroupSetting(key, validator, properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a group of settings prefixed by a key.
|
||||
*
|
||||
* @param key the group key for the setting
|
||||
* @param fallback a {@link GroupSetting} to use as fallback when no group key values exist
|
||||
* @param properties properties properties for this setting like scope, filtering...
|
||||
* @return the group setting object
|
||||
*/
|
||||
public static Setting<Settings> groupSetting(String key, final Setting<Settings> fallback, Property... properties) {
|
||||
return groupSetting(key, fallback, (s) -> {}, properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a group of settings prefixed by a key.
|
||||
*
|
||||
* @param key the group key for the setting
|
||||
* @param fallback a {@link GroupSetting} to use as fallback when no group key values exist
|
||||
* @param validator a {@link Validator} for validating this setting
|
||||
* @param properties properties properties for this setting like scope, filtering...
|
||||
* @return the group setting object
|
||||
*/
|
||||
public static Setting<Settings> groupSetting(String key, final Setting<Settings> fallback,
|
||||
Consumer<Settings> validator, Property... properties) {
|
||||
return new GroupSetting(key, fallback, validator, properties);
|
||||
}
|
||||
|
||||
public static Setting<TimeValue> timeSetting(
|
||||
final String key,
|
||||
final Setting<TimeValue> fallbackSetting,
|
||||
|
|
|
@ -514,6 +514,26 @@ public class SettingTests extends OpenSearchTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void testGroupSettingFallback() {
|
||||
Setting<Settings> fallbackSetting = Setting.groupSetting("old.bar.", Property.Dynamic, Property.NodeScope);
|
||||
assertFalse(fallbackSetting.exists(Settings.EMPTY));
|
||||
|
||||
Setting<Settings> groupSetting = Setting.groupSetting("new.bar.", fallbackSetting, Property.Dynamic, Property.NodeScope);
|
||||
assertFalse(groupSetting.exists(Settings.EMPTY));
|
||||
|
||||
Settings values = Settings.builder()
|
||||
.put("old.bar.1.value", "value 1")
|
||||
.put("old.bar.2.value", "value 2")
|
||||
.build();
|
||||
|
||||
assertTrue(groupSetting.exists(values));
|
||||
|
||||
Map<String, Settings> asMap = groupSetting.get(values).getAsGroups();
|
||||
assertEquals(2, asMap.size());
|
||||
assertEquals("value 1", asMap.get("1").get("value"));
|
||||
assertEquals("value 2", asMap.get("2").get("value"));
|
||||
}
|
||||
|
||||
public void testFilteredGroups() {
|
||||
AtomicReference<Settings> ref = new AtomicReference<>(null);
|
||||
Setting<Settings> setting = Setting.groupSetting("foo.bar.", Property.Filtered, Property.Dynamic);
|
||||
|
|
Loading…
Reference in New Issue