From 381eb0e91ad2b784314d38223c8883adb2b60930 Mon Sep 17 00:00:00 2001 From: jdidion Date: Wed, 26 Jan 2022 09:17:54 -0800 Subject: [PATCH 1/2] fix evaluation of multi-type --- src/main/scala/dx/cwl/Evaluator.scala | 51 ++++++++++++++++----------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/main/scala/dx/cwl/Evaluator.scala b/src/main/scala/dx/cwl/Evaluator.scala index 72d5b2f..8c6344e 100644 --- a/src/main/scala/dx/cwl/Evaluator.scala +++ b/src/main/scala/dx/cwl/Evaluator.scala @@ -966,28 +966,37 @@ case class Evaluator(jsEnabled: Boolean = false, .to(TreeSeqMap) (CwlOutputRecord(fields), value) case (CwlMulti(types), _) => - types.iterator - .map { - case CwlAny => None - case t => - try { - Some(inner(innerValue, t)) - } catch { - case _: Throwable => None - } - } - .collectFirst { - case Some(value) => value - } - .getOrElse( - if (types.contains(CwlAny)) { - (CwlAny, innerValue) - } else { - throw new Exception( - s"${innerValue} does not evaluate to any of ${types}" - ) + types.flatMap { + case CwlAny => None + case t => + try { + Some(inner(innerValue, t)) + } catch { + case _: Throwable => None + } + } match { + case Vector(result) => result + case Vector() if types.contains(CwlAny) => (CwlAny, innerValue) + case Vector() => + throw new Exception( + s"${innerValue} does not evaluate to any of ${types}" + ) + case v => + // we have multiple evaluation results - for the type, create a CwlMulti from all the types, and for + // the value, if there are multiple we first prefer string (because it is coercible to the most other + // types and then just pick the first one + val (types, values) = v.unzip + val distinctValues = values.toSet + val value = Option + .when(distinctValues.size > 1) { + distinctValues.collectFirst { + case s: StringValue => s + } } - ) + .flatten + .getOrElse(distinctValues.head) + (CwlType.flatten(types), value) + } case _ => checkCoercibleTo(innerValue, innerType) } } From e75f587ce6f6efb03a9974ce68deff2e50360616 Mon Sep 17 00:00:00 2001 From: jdidion Date: Wed, 26 Jan 2022 16:56:07 -0800 Subject: [PATCH 2/2] update changelog --- RELEASE_NOTES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index e87c877..1140766 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -16,6 +16,7 @@ * `Evaluator.finalizeInputValue` loads file contents from remote file source if file does not exist locally * `CwlType.coerceTo` now returns both the coerced-to type and value * Added `CwlType.CwlGenericRecord`, which is coercible to either `CwlInputRecord` or `CwlOutputRecord` +* Fixes evaluation of values with multiple possible types ## 0.7.4 (2021-01-05)