From 2d8885d4e368f7f8f1794a62fc02fc83195309cf Mon Sep 17 00:00:00 2001 From: Oliver Egger Date: Tue, 29 Nov 2022 22:15:02 +0100 Subject: [PATCH] FHIR Mapping Language: cast support for more types #1019 --- .../structuremap/StructureMapUtilities.java | 47 +++++++++++++++++-- .../r5/test/StructureMapUtilitiesTest.java | 20 ++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/structuremap/StructureMapUtilities.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/structuremap/StructureMapUtilities.java index 0933e93e0..045eb89e1 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/structuremap/StructureMapUtilities.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/structuremap/StructureMapUtilities.java @@ -1707,10 +1707,49 @@ public class StructureMapUtilities { if (tgt.getParameter().size() == 1) throw new FHIRException("Implicit type parameters on cast not yet supported"); String t = getParamString(vars, tgt.getParameter().get(1)); - if (t.equals("string")) - return new StringType(src); - else - throw new FHIRException("cast to " + t + " not yet supported"); + switch(t) { + case "boolean": + return new BooleanType(src); + case "integer": + return new IntegerType(src); + case "integer64": + return new Integer64Type(src); + case "string": + return new StringType(src); + case "decimal": + return new DecimalType(src); + case "uri": + return new UriType(src); + case "base64Binary": + return new Base64BinaryType(src); + case "instant": + return new InstantType(src); + case "date": + return new DateType(src); + case "dateTime": + return new DateTimeType(src); + case "time": + return new TimeType(src); + case "code": + return new CodeType(src); + case "oid": + return new OidType(src); + case "id": + return new IdType(src); + case "markdown": + return new MarkdownType(src); + case "unsignedInt": + return new UnsignedIntType(src); + case "positiveInt": + return new PositiveIntType(src); + case "uuid": + return new UuidType(src); + case "url": + return new UrlType(src); + case "canonical": + return new CanonicalType(src); + } + throw new FHIRException("cast to " + t + " not yet supported"); case APPEND: StringBuilder sb = new StringBuilder(getParamString(vars, tgt.getParameter().get(0))); for (int i = 1; i < tgt.getParameter().size(); i++) diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/StructureMapUtilitiesTest.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/StructureMapUtilitiesTest.java index eeb9dc21b..04163b4f0 100644 --- a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/StructureMapUtilitiesTest.java +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/StructureMapUtilitiesTest.java @@ -5,11 +5,15 @@ import java.util.List; import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.r5.context.SimpleWorkerContext; +import org.hl7.fhir.r5.elementmodel.Element; +import org.hl7.fhir.r5.elementmodel.Manager; +import org.hl7.fhir.r5.elementmodel.Manager.FhirFormat; import org.hl7.fhir.r5.model.Base; import org.hl7.fhir.r5.model.Coding; import org.hl7.fhir.r5.model.StructureMap; import org.hl7.fhir.r5.model.StructureMap.StructureMapGroupRuleTargetComponent; import org.hl7.fhir.r5.test.utils.TestingUtilities; +import org.hl7.fhir.r5.utils.FHIRPathEngine; import org.hl7.fhir.r5.utils.structuremap.ITransformerServices; import org.hl7.fhir.r5.utils.structuremap.StructureMapUtilities; import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager; @@ -37,6 +41,22 @@ public class StructureMapUtilitiesTest implements ITransformerServices { // StructureMap/ActivityDefinition3to4: StructureMap.group[3].rule[2].name error id value '"expression"' is not valid Assertions.assertEquals("expression", structureMap.getGroup().get(2).getRule().get(1).getName()); } + + @Test + public void testCast() throws IOException, FHIRException { + // org.hl7.fhir.exceptions.FHIRException: Exception executing transform ext.value = cast(value, 'positiveInt') on Rule "item": cast to positiveInt not yet supported + StructureMapUtilities scu = new StructureMapUtilities(context, this); + String fileMap = TestingUtilities.loadTestResource("r5", "structure-mapping", "cast.map"); + Element source = Manager.parseSingle(context, TestingUtilities.loadTestResourceStream("r5", "structure-mapping", "qrext.json"), FhirFormat.JSON); + StructureMap structureMap = scu.parse(fileMap, "cast"); + Element target = Manager.build(context, scu.getTargetType(structureMap)); + scu.transform(null, source, structureMap, target); + FHIRPathEngine fp = new FHIRPathEngine(context); + Assertions.assertEquals("implicit",fp.evaluateToString(target, "extension[0].value")); + Assertions.assertEquals("explicit",fp.evaluateToString(target, "extension[1].value")); + Assertions.assertEquals("2147483647",fp.evaluateToString(target, "extension[2].value")); + Assertions.assertEquals("2147483647",fp.evaluateToString(target, "extension[3].value")); + } private void assertSerializeDeserialize(StructureMap structureMap) { Assertions.assertEquals("syntax", structureMap.getName());