Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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,28 @@ public void testFilteredBooleanSetting() {
assertNull(e.getCause());
}

public void testFloatSettingWithOtherSettingAsDefault() {
float defaultValue = randomFloat();
Setting<Float> defaultSetting = Setting.floatSetting("float_val.default", defaultValue);
Setting<Float> floatSetting = Setting.floatSetting("float_val", defaultSetting, Float.MIN_VALUE);
assertThat(floatSetting.get(Settings.builder().build()), equalTo(defaultValue));
float configuredDefaultValue = randomValueOtherThan(defaultValue, ESTestCase::randomFloat);
assertThat(
floatSetting.get(Settings.builder().put("float_val.default", configuredDefaultValue).build()),
equalTo(configuredDefaultValue)
);
float configuredSettingValue = randomValueOtherThanMany(
v -> v != configuredDefaultValue && v != defaultValue,
ESTestCase::randomFloat
);
assertThat(
floatSetting.get(
Settings.builder().put("float_val.default", configuredDefaultValue).put("float_val", configuredSettingValue).build()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this would have been maybe more useful if it was something else, e.g.

assertThat(
  floatSetting.get(
    Settings.builder().put("float_val.default", someOtherDefaultValue).put("float_val", configuredSettingValue).build()),
  equalTo(configuredSettingValue)
);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is, but my naming is not great.

i.e. defaultSetting (the default for float_val.default) != configuredDefault (the configured value for float_val.default != configuredSetting (the configured value for float_val). I'll fix the naming up to make that clearer.

),
equalTo(configuredSettingValue)
);
}

private enum TestEnum {
ON,
OFF
Expand Down
Loading