Deal with data type changes between versions for cross version extensions

This commit is contained in:
Grahame Grieve 2019-12-19 17:35:54 +11:00
parent ed84384205
commit b83302092b
1 changed files with 48 additions and 8 deletions

View File

@ -17,6 +17,8 @@ import org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind;
import org.hl7.fhir.r5.model.StructureDefinition.TypeDerivationRule; import org.hl7.fhir.r5.model.StructureDefinition.TypeDerivationRule;
import org.hl7.fhir.r5.model.UriType; import org.hl7.fhir.r5.model.UriType;
import org.hl7.fhir.r5.validation.XVerExtensionManager.XVerExtensionStatus; import org.hl7.fhir.r5.validation.XVerExtensionManager.XVerExtensionStatus;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.VersionUtilities;
import org.hl7.fhir.utilities.json.JsonTrackingParser; import org.hl7.fhir.utilities.json.JsonTrackingParser;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
@ -88,7 +90,7 @@ public class XVerExtensionManager {
sd.getDifferential().addElement().setPath("Extension.extension").setMax("0"); sd.getDifferential().addElement().setPath("Extension.extension").setMax("0");
sd.getDifferential().addElement().setPath("Extension.url").setFixed(new UriType(url)); sd.getDifferential().addElement().setPath("Extension.url").setFixed(new UriType(url));
ElementDefinition val = sd.getDifferential().addElement().setPath("Extension.value[x]").setMin(1); ElementDefinition val = sd.getDifferential().addElement().setPath("Extension.value[x]").setMin(1);
populateTypes(path, val); populateTypes(path, val, v);
} else if (path.has("elements")) { } else if (path.has("elements")) {
for (JsonElement i : path.getAsJsonArray("elements")) { for (JsonElement i : path.getAsJsonArray("elements")) {
String s = i.getAsString(); String s = i.getAsString();
@ -100,7 +102,7 @@ public class XVerExtensionManager {
if (!elt.has("types")) { if (!elt.has("types")) {
throw new FHIRException("Internal error - nested elements not supported yet"); throw new FHIRException("Internal error - nested elements not supported yet");
} }
populateTypes(elt, val); populateTypes(elt, val, v);
} }
sd.getDifferential().addElement().setPath("Extension.url").setFixed(new UriType(url)); sd.getDifferential().addElement().setPath("Extension.url").setFixed(new UriType(url));
sd.getDifferential().addElement().setPath("Extension.value[x]").setMax("0"); sd.getDifferential().addElement().setPath("Extension.value[x]").setMax("0");
@ -110,22 +112,60 @@ public class XVerExtensionManager {
return sd; return sd;
} }
public void populateTypes(JsonObject path, ElementDefinition val) { public void populateTypes(JsonObject path, ElementDefinition val, String v) {
for (JsonElement i : path.getAsJsonArray("types")) { for (JsonElement i : path.getAsJsonArray("types")) {
String s = i.getAsString(); String s = i.getAsString();
if (s.contains("(")) { if (s.contains("(")) {
String t = s.substring(0, s.indexOf("(")); String t = s.substring(0, s.indexOf("("));
TypeRefComponent tr = val.addType().setCode(s); TypeRefComponent tr = val.addType().setCode(translateDataType(v, s));
s = s.substring(t.length()+1); if (hasTargets(tr.getCode()) ) {
for (String p : s.substring(0, s.length()-1).split("\\|")) { s = s.substring(t.length()+1);
tr.addTargetProfile("http://hl7.org/fhir/StructureDefinition/"+p); for (String p : s.substring(0, s.length()-1).split("\\|")) {
tr.addTargetProfile("http://hl7.org/fhir/StructureDefinition/"+p);
}
} }
} else { } else {
val.addType().setCode(s); val.addType().setCode(translateDataType(v, s));
} }
} }
} }
private boolean hasTargets(String dt) {
return Utilities.existsInList(dt, "canonical", "Reference", "CodeableReference");
}
private String translateDataType(String v, String dt) {
if (VersionUtilities.versionsCompatible("1.0", v) || VersionUtilities.versionsCompatible("1.4", v)) {
return translateToR2(dt);
} else if (VersionUtilities.versionsCompatible("3.0", v)) {
return translateToR3(dt);
} else {
return dt;
}
}
private String translateToR3(String dt) {
if ("canonical".equals(dt)) {
return "uri";
} else if ("url".equals(dt)) {
return "uri";
} else {
return dt;
}
}
private String translateToR2(String dt) {
if ("canonical".equals(dt)) {
return "uri";
} else if ("url".equals(dt)) {
return "uri";
} else if ("uuid".equals(dt)) {
return "id";
} else {
return dt;
}
}
private String head(String id) { private String head(String id) {
if (id.contains(".")) { if (id.contains(".")) {
return id.substring(0, id.lastIndexOf(".")); return id.substring(0, id.lastIndexOf("."));