-
Notifications
You must be signed in to change notification settings - Fork 51
Fix model hierarchy backward compatibility #1214
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ | |
| */ | ||
| package io.serverlessworkflow.impl; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.BigInteger; | ||
| import java.time.OffsetDateTime; | ||
| import java.util.Collection; | ||
| import java.util.Map; | ||
|
|
@@ -24,7 +26,33 @@ public abstract class AbstractWorkflowModel implements WorkflowModel { | |
|
|
||
| protected abstract <T> Optional<T> convert(Class<T> clazz); | ||
|
|
||
| protected abstract <N extends Number> Optional<N> asNumber(Class<N> targetNumberClass); | ||
| protected final <N extends Number> Optional<N> asSubclass( | ||
| Number num, Class<N> targetNumberClass) { | ||
| if (targetNumberClass.isInstance(num)) { | ||
| return Optional.of(targetNumberClass.cast(num)); | ||
| } else if (targetNumberClass == Integer.class) { | ||
| return Optional.of(targetNumberClass.cast(num.intValue())); | ||
| } else if (targetNumberClass == Long.class) { | ||
| return Optional.of(targetNumberClass.cast(num.longValue())); | ||
| } else if (targetNumberClass == Double.class) { | ||
| return Optional.of(targetNumberClass.cast(num.doubleValue())); | ||
| } else if (targetNumberClass == Float.class) { | ||
| return Optional.of(targetNumberClass.cast(num.floatValue())); | ||
| } else if (targetNumberClass == Short.class) { | ||
| return Optional.of(targetNumberClass.cast(num.shortValue())); | ||
| } else if (targetNumberClass == Byte.class) { | ||
| return Optional.of(targetNumberClass.cast(num.byteValue())); | ||
| } else if (targetNumberClass == BigDecimal.class) { | ||
| return Optional.of(targetNumberClass.cast(BigDecimal.valueOf(num.doubleValue()))); | ||
| } else if (targetNumberClass == BigInteger.class) { | ||
| return Optional.of(targetNumberClass.cast(BigInteger.valueOf(num.longValue()))); | ||
| } | ||
|
Comment on lines
+45
to
+49
|
||
| return Optional.empty(); | ||
| } | ||
|
|
||
| protected <N extends Number> Optional<N> asNumber(Class<N> targetNumberClass) { | ||
| return asNumber().flatMap(n -> asSubclass(n, targetNumberClass)); | ||
| } | ||
|
|
||
| @Override | ||
| public <T> Optional<T> as(Class<T> clazz) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,9 +47,9 @@ public static WorkflowModel modelFromOutput( | |
| case ALL -> | ||
| modelFactory.fromAny( | ||
| new ProcessResult( | ||
| 0, streamSuppliers.outputStream().get(), streamSuppliers.errorStream().get())); | ||
| code, streamSuppliers.outputStream().get(), streamSuppliers.errorStream().get())); | ||
| case NONE -> model; | ||
| case CODE -> modelFactory.from(0); | ||
| case CODE -> modelFactory.from(code); | ||
| case STDOUT -> modelFactory.from(streamSuppliers.outputStream().get()); | ||
|
Comment on lines
47
to
53
|
||
| case STDERR -> modelFactory.from(streamSuppliers.errorStream().get()); | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
asSubclassconverts toBigDecimalviaBigDecimal.valueOf(num.doubleValue()), which (a) can throwNumberFormatExceptionforNaN/Infinityinputs and (b) loses precision for large integral values (e.g.,Long> 2^53). Consider using type-specific exact conversions (e.g.,BigDecimal.valueOf(long)for integral types,new BigDecimal((BigInteger) num)forBigInteger,BigDecimal.valueOf(double)only for floating types) and returningOptional.empty()when conversion is not representable (including non-finite doubles).