mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-06-28 17:34:17 -04:00
Allow float settings to be configured with other settings as default (#126751)
Relates ES-11367
This commit is contained in:
parent
718315c5f8
commit
dfaf3de96e
3 changed files with 43 additions and 2 deletions
5
docs/changelog/126751.yaml
Normal file
5
docs/changelog/126751.yaml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
pr: 126751
|
||||||
|
summary: Allow float settings to be configured with other settings as default
|
||||||
|
area: Infra/Settings
|
||||||
|
type: enhancement
|
||||||
|
issues: []
|
|
@ -1367,8 +1367,16 @@ public class Setting<T> implements ToXContentObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Setting<Float> floatSetting(String key, float defaultValue, float minValue, Property... properties) {
|
public static Setting<Float> floatSetting(String key, float defaultValue, float minValue, Property... properties) {
|
||||||
|
return new Setting<>(key, Float.toString(defaultValue), floatParser(key, minValue, properties), properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Setting<Float> floatSetting(String key, Setting<Float> fallbackSetting, float minValue, Property... properties) {
|
||||||
|
return new Setting<>(key, fallbackSetting, floatParser(key, minValue, properties), properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Function<String, Float> floatParser(String key, float minValue, Property... properties) {
|
||||||
final boolean isFiltered = isFiltered(properties);
|
final boolean isFiltered = isFiltered(properties);
|
||||||
return new Setting<>(key, Float.toString(defaultValue), (s) -> {
|
return (s) -> {
|
||||||
float value = Float.parseFloat(s);
|
float value = Float.parseFloat(s);
|
||||||
if (value < minValue) {
|
if (value < minValue) {
|
||||||
String err = "Failed to parse value"
|
String err = "Failed to parse value"
|
||||||
|
@ -1380,7 +1388,7 @@ public class Setting<T> implements ToXContentObject {
|
||||||
throw new IllegalArgumentException(err);
|
throw new IllegalArgumentException(err);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}, properties);
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isFiltered(Property[] properties) {
|
private static boolean isFiltered(Property[] properties) {
|
||||||
|
|
|
@ -387,6 +387,34 @@ public class SettingTests extends ESTestCase {
|
||||||
assertNull(e.getCause());
|
assertNull(e.getCause());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testFloatSettingWithOtherSettingAsDefault() {
|
||||||
|
float defaultFallbackValue = randomFloat();
|
||||||
|
Setting<Float> fallbackSetting = Setting.floatSetting("fallback_setting", defaultFallbackValue);
|
||||||
|
Setting<Float> floatSetting = Setting.floatSetting("float_setting", fallbackSetting, Float.MIN_VALUE);
|
||||||
|
|
||||||
|
// Neither float_setting nor fallback_setting specified
|
||||||
|
assertThat(floatSetting.get(Settings.builder().build()), equalTo(defaultFallbackValue));
|
||||||
|
|
||||||
|
// Only fallback_setting specified
|
||||||
|
float explicitFallbackValue = randomValueOtherThan(defaultFallbackValue, ESTestCase::randomFloat);
|
||||||
|
assertThat(
|
||||||
|
floatSetting.get(Settings.builder().put("fallback_setting", explicitFallbackValue).build()),
|
||||||
|
equalTo(explicitFallbackValue)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Both float_setting and fallback_setting specified
|
||||||
|
float explicitFloatValue = randomValueOtherThanMany(
|
||||||
|
v -> v != explicitFallbackValue && v != defaultFallbackValue,
|
||||||
|
ESTestCase::randomFloat
|
||||||
|
);
|
||||||
|
assertThat(
|
||||||
|
floatSetting.get(
|
||||||
|
Settings.builder().put("fallback_setting", explicitFallbackValue).put("float_setting", explicitFloatValue).build()
|
||||||
|
),
|
||||||
|
equalTo(explicitFloatValue)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private enum TestEnum {
|
private enum TestEnum {
|
||||||
ON,
|
ON,
|
||||||
OFF
|
OFF
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue