Skip to content

Commit

Permalink
also use the fast path in coerce and fix default-value behaviour.
Browse files Browse the repository at this point in the history
Therefore add missing tests for coerce with default values and always trim to be less strict

BREAKING: Value#coerce(Boolean.class, true) now return true as expected and no longer false if the value is empty

- fixes: SE-13483
  • Loading branch information
ymo-sci committed Mar 21, 2024
1 parent f77b852 commit ebaf186
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
9 changes: 5 additions & 4 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 @@ -813,9 +814,9 @@ public boolean asBoolean(boolean defaultValue) {
return booleanValue;
}

String stringValue = String.valueOf(data);
String stringValue = String.valueOf(data).trim();
return fastPathBoolean(stringValue).orElseGet(() -> {
return Objects.requireNonNullElse(NLS.parseUserString(Boolean.class, stringValue.trim()), defaultValue);
return Objects.requireNonNullElse(NLS.parseUserString(Boolean.class, stringValue), defaultValue);
});
}

Expand Down
26 changes: 25 additions & 1 deletion src/test/kotlin/sirius/kernel/commons/ValueTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,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

0 comments on commit ebaf186

Please sign in to comment.