Allow float settings to be configured with other settings as default (#126751)

Relates ES-11367
This commit is contained in:
Nick Tindall 2025-04-15 13:41:01 +10:00 committed by GitHub
parent 718315c5f8
commit dfaf3de96e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 43 additions and 2 deletions

View 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: []

View file

@ -1367,8 +1367,16 @@ public class Setting<T> implements ToXContentObject {
}
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);
return new Setting<>(key, Float.toString(defaultValue), (s) -> {
return (s) -> {
float value = Float.parseFloat(s);
if (value < minValue) {
String err = "Failed to parse value"
@ -1380,7 +1388,7 @@ public class Setting<T> implements ToXContentObject {
throw new IllegalArgumentException(err);
}
return value;
}, properties);
};
}
private static boolean isFiltered(Property[] properties) {

View file

@ -387,6 +387,34 @@ public class SettingTests extends ESTestCase {
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 {
ON,
OFF