Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow 1 and 0 for boolean values in Value#asBoolean #517

Merged
merged 5 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* A flexible parser for dates in various formats.
* <p>
* It can parse formats like DD.MM.YYYY, DD-MM-YYYY, MM/DD/YYYY or ISO dates like YYYY-MM-DDTHH:MM:SS along with some
* modifiers as decribed below.
* modifiers as described below.
* <p>
* A valid expression is defined by the following grammar:
* <ul>
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/sirius/kernel/commons/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,13 @@ public Value first() {
public <T> T coerce(Class<T> targetClazz, T defaultValue) {
if (Boolean.class.equals(targetClazz) || boolean.class.equals(targetClazz)) {
if (isEmptyString()) {
return (T) Boolean.FALSE;
return defaultValue != null ? defaultValue : (T) Boolean.FALSE;
}
if (data instanceof Boolean) {
return (T) data;
}
return (T) NLS.parseMachineString(Boolean.class, String.valueOf(data));
String stringValue = String.valueOf(data).trim();
return (T) fastPathBoolean(stringValue).orElseGet(() -> NLS.parseMachineString(Boolean.class, stringValue));
}
if (data == null) {
return defaultValue;
Expand Down Expand Up @@ -802,7 +803,7 @@ public String asSmartRoundedString() {
*
* @param defaultValue the value to be used if the wrapped value cannot be converted to a boolean.
* @return <tt>true</tt> if the wrapped value is <tt>true</tt>
* or if the string representation of it is {@code "true"}. Returns <tt>false</tt> otherwise,
* or if the string representation of it is {@code "true"} or {@code "1!"}. Returns <tt>false</tt> otherwise,
ymo-sci marked this conversation as resolved.
Show resolved Hide resolved
* especially if the wrapped value is <tt>null</tt>
*/
public boolean asBoolean(boolean defaultValue) {
Expand All @@ -813,16 +814,21 @@ public boolean asBoolean(boolean defaultValue) {
return booleanValue;
}

// fast-track for common cases without the need to involve NLS framework
if ("true".equalsIgnoreCase(String.valueOf(data))) {
return true;
String stringValue = String.valueOf(data).trim();
return fastPathBoolean(stringValue).orElseGet(() -> {
return Objects.requireNonNullElse(NLS.parseUserString(Boolean.class, stringValue), defaultValue);
});
}

// fast-track for common boolean cases without the need to involve NLS framework
ymo-sci marked this conversation as resolved.
Show resolved Hide resolved
private Optional<Boolean> fastPathBoolean(String value) {
ymo-sci marked this conversation as resolved.
Show resolved Hide resolved
if ("true".equalsIgnoreCase(value) || "1".equals(value)) {
return Optional.of(true);
}
if ("false".equalsIgnoreCase(String.valueOf(data))) {
return false;
if ("false".equalsIgnoreCase(value) || "0".equals(value)) {
return Optional.of(false);
}

return Objects.requireNonNullElse(NLS.parseUserString(Boolean.class, String.valueOf(data).trim()),
defaultValue);
return Optional.empty();
}

/**
Expand Down
36 changes: 32 additions & 4 deletions src/test/kotlin/sirius/kernel/commons/ValueTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,16 @@ class ValueTest {
@Test
fun `Test asBoolean with default`() {
assertEquals(false, Value.of("").asBoolean(false))
assertEquals(false, Value.of("false").asBoolean(false))
assertEquals(true, Value.of("true").asBoolean(false))
assertEquals(false, Value.of(false).asBoolean(false))
assertEquals(false, Value.of("false").asBoolean(false))
assertEquals(false, Value.of(0).asBoolean(false))
assertEquals(false, Value.of("0").asBoolean(false))
assertEquals(false, Value.of(NLS.get("NLS.no")).asBoolean(false))
assertEquals(true, Value.of(true).asBoolean(false))
assertEquals(true, Value.of("true").asBoolean(false))
assertEquals(true, Value.of(1).asBoolean(false))
assertEquals(true, Value.of("1").asBoolean(false))
assertEquals(true, Value.of(NLS.get("NLS.yes")).asBoolean(false))
assertEquals(false, Value.of(NLS.get("NLS.no")).asBoolean(false))
}

@Test
Expand All @@ -131,14 +135,38 @@ class ValueTest {
}

@Test
fun `Test coerce boolean and without default`() {
fun `Test coerce boolean without default`() {
assertEquals(false, Value.of("").coerce(Boolean::class.java, null))
assertEquals(false, Value.of("false").coerce(Boolean::class.java, null))
assertEquals(true, Value.of("true").coerce(Boolean::class.java, null))
assertEquals(false, Value.of("0").coerce(Boolean::class.java, null))
assertEquals(true, Value.of("1").coerce(Boolean::class.java, null))
assertEquals(false, Value.of(false).coerce(Boolean::class.java, null))
assertEquals(true, Value.of(true).coerce(Boolean::class.java, null))
}

@Test
fun `Test coerce boolean with default true`() {
assertEquals(true, Value.of("").coerce(Boolean::class.java, true))
assertEquals(false, Value.of("false").coerce(Boolean::class.java, true))
assertEquals(true, Value.of("true").coerce(Boolean::class.java, true))
assertEquals(false, Value.of("0").coerce(Boolean::class.java, true))
assertEquals(true, Value.of("1").coerce(Boolean::class.java, true))
assertEquals(false, Value.of(false).coerce(Boolean::class.java, true))
assertEquals(true, Value.of(true).coerce(Boolean::class.java, true))
}

@Test
fun `Test coerce boolean with default false`() {
assertEquals(false, Value.of("").coerce(Boolean::class.java, false))
assertEquals(false, Value.of("false").coerce(Boolean::class.java, false))
assertEquals(true, Value.of("true").coerce(Boolean::class.java, false))
assertEquals(false, Value.of("0").coerce(Boolean::class.java, false))
assertEquals(true, Value.of("1").coerce(Boolean::class.java, false))
assertEquals(false, Value.of(false).coerce(Boolean::class.java, false))
assertEquals(true, Value.of(true).coerce(Boolean::class.java, false))
}

@Test
fun `Boxing and retrieving an amount works`() {
assertEquals(Amount.of(0.00001), Value.of(Amount.of(0.00001)).getAmount())
Expand Down