Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/126751.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 126751
summary: Allow float settings to be configured with other settings as default
area: Infra/Settings
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -1367,8 +1367,16 @@ public static Setting<Float> floatSetting(String key, float defaultValue, Proper
}

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"
Expand All @@ -1380,7 +1388,7 @@ public static Setting<Float> floatSetting(String key, float defaultValue, float
throw new IllegalArgumentException(err);
}
return value;
}, properties);
};
}

private static boolean isFiltered(Property[] properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,34 @@ public void testFilteredBooleanSetting() {
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
Expand Down
Loading