Skip to content

Commit

Permalink
Accept "1" as true for boolean properties
Browse files Browse the repository at this point in the history
  • Loading branch information
randomanderson committed Oct 29, 2021
1 parent 6f1aeb4 commit 1807faf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,28 @@ static BitSet parseIntegerRangeSet(@Nonnull String str, final String settingName
return set;
}

public static Boolean booleanValueOf(String value) {
if ("1".equals(value)) {
return Boolean.TRUE;
} else {
return Boolean.valueOf(value);
}
}

private static class ValueOfLookup extends ClassValue<MethodHandle> {
private static final MethodHandles.Lookup PUBLIC_LOOKUP = MethodHandles.publicLookup();

@Override
protected MethodHandle computeValue(Class<?> type) {
try {
if (Boolean.class.equals(type)) {
return MethodHandles.lookup()
.findStatic(
ConfigConverter.class,
"booleanValueOf",
MethodType.methodType(Boolean.class, String.class));
}

return PUBLIC_LOOKUP.findStatic(type, "valueOf", MethodType.methodType(type, String.class));
} catch (final NoSuchMethodException | IllegalAccessException e) {
log.debug("Can't invoke or access 'valueOf': ", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package datadog.trace.bootstrap.config.provider

import datadog.trace.test.util.DDSpecification

class ConfigConverterTest extends DDSpecification {

def "Convert boolean properties"() {
when:
def value = ConfigConverter.valueOf(stringValue, Boolean.class)

then:
value == expectedConvertedValue

where:
stringValue | expectedConvertedValue
"true" | true
"TRUE" | true
"True" | true
"1" | true
"false" | false
null | null
"" | null
"0" | false
}
}

0 comments on commit 1807faf

Please sign in to comment.