Merge pull request #1020 from ahdis/oe_r5_structuremap_cast

FHIR Mapping Language: cast support for more types #1019
This commit is contained in:
Grahame Grieve 2022-12-01 08:18:27 +11:00 committed by GitHub
commit 8d6f2ff57d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 4 deletions

View File

@ -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++)

View File

@ -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());