Skip to content

Commit

Permalink
removed custom processing for double in TOML parser, added an extra t…
Browse files Browse the repository at this point in the history
…est for int as double (#4948)

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>

Signed-off-by: Sally MacFarlane <macfarla.github@gmail.com>
  • Loading branch information
macfarla authored Jan 18, 2023
1 parent aa53f22 commit f0c2380
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,17 @@ private String getConfigurationValue(final OptionSpec optionSpec) {
} else if (optionSpec.isMultiValue() || isArray) {
defaultValue = getListEntryAsString(optionSpec);
} else if (optionSpec.type().equals(Integer.class) || optionSpec.type().equals(int.class)) {
defaultValue = getIntegerEntryAsString(optionSpec);
defaultValue = getNumericEntryAsString(optionSpec);
} else if (optionSpec.type().equals(Long.class) || optionSpec.type().equals(long.class)) {
defaultValue = getIntegerEntryAsString(optionSpec);
defaultValue = getNumericEntryAsString(optionSpec);
} else if (optionSpec.type().equals(Wei.class)) {
defaultValue = getIntegerEntryAsString(optionSpec);
defaultValue = getNumericEntryAsString(optionSpec);
} else if (optionSpec.type().equals(BigInteger.class)) {
defaultValue = getIntegerEntryAsString(optionSpec);
defaultValue = getNumericEntryAsString(optionSpec);
} else if (optionSpec.type().equals(Double.class) || optionSpec.type().equals(double.class)) {
defaultValue = getDoubleEntryAsString(optionSpec);
defaultValue = getNumericEntryAsString(optionSpec);
} else if (optionSpec.type().equals(Float.class) || optionSpec.type().equals(float.class)) {
defaultValue = getNumericEntryAsString(optionSpec);
} else { // else will be treated as String
defaultValue = getEntryAsString(optionSpec);
}
Expand Down Expand Up @@ -129,20 +131,13 @@ private String getBooleanEntryAsString(final OptionSpec spec) {
return getKeyName(spec).map(result::getBoolean).map(Object::toString).orElse(null);
}

private String getIntegerEntryAsString(final OptionSpec spec) {
// return the string representation of the integer value corresponding to the option in toml
// file
private String getNumericEntryAsString(final OptionSpec spec) {
// return the string representation of the numeric value corresponding to the option in toml
// file - this works for integer, double, and float
// or null if not present in the config
return getKeyName(spec).map(result::get).map(String::valueOf).orElse(null);
}

private String getDoubleEntryAsString(final OptionSpec spec) {
// return the string representation of the double value corresponding to the option in toml
// file
// or null if not present in the config
return getKeyName(spec).map(result::getDouble).map(String::valueOf).orElse(null);
}

private void checkConfigurationValidity() {
if (result == null || result.isEmpty())
throw new ParameterException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public void defaultValueForOptionMustMatchType() throws IOException {
validOptionsMap.put("--a-string-value-option", null);
validOptionsMap.put("--a-nested-multi-value-option", null);
validOptionsMap.put("--a-double-value-option", null);
validOptionsMap.put("--a-double-value-option-int", null);

when(mockCommandSpec.optionsMap()).thenReturn(validOptionsMap);

Expand Down Expand Up @@ -149,6 +150,8 @@ public void defaultValueForOptionMustMatchType() throws IOException {
"a-nested-multi-value-option=[ [\"value1\", \"value2\"], [\"value3\", \"value4\"] ]");
fileWriter.newLine();
fileWriter.write("a-double-value-option=0.01");
fileWriter.newLine();
fileWriter.write("a-double-value-option-int=1"); // should be able to parse int as double
fileWriter.flush();

final TomlConfigFileDefaultProvider providerUnderTest =
Expand Down Expand Up @@ -206,6 +209,11 @@ public void defaultValueForOptionMustMatchType() throws IOException {
OptionSpec.builder("a-double-value-option").type(Double.class).build()))
.isEqualTo("0.01");

assertThat(
providerUnderTest.defaultValue(
OptionSpec.builder("a-double-value-option-int").type(Double.class).build()))
.isEqualTo("1");

assertThat(
providerUnderTest.defaultValue(
OptionSpec.builder("a-nested-multi-value-option").type(Collection.class).build()))
Expand Down

0 comments on commit f0c2380

Please sign in to comment.