From e3dd645b759812db6f49979acddd94bbff680274 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Mon, 14 Aug 2023 15:36:08 +1000 Subject: [PATCH] more differencing improvements --- .../fhir/r5/utils/DefinitionNavigator.java | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/DefinitionNavigator.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/DefinitionNavigator.java index 773a03051..1d549286a 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/DefinitionNavigator.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/DefinitionNavigator.java @@ -54,21 +54,24 @@ public class DefinitionNavigator { private List names = new ArrayList(); private TypeRefComponent typeOfChildren; private String path; + private boolean diff; - public DefinitionNavigator(IWorkerContext context, StructureDefinition structure) throws DefinitionException { - if (!structure.hasSnapshot()) + public DefinitionNavigator(IWorkerContext context, StructureDefinition structure, boolean diff) throws DefinitionException { + if (!diff && !structure.hasSnapshot()) throw new DefinitionException("Snapshot required"); this.context = context; this.structure = structure; this.index = 0; + this.diff = diff; this.path = current().getPath(); names.add(nameTail()); } - private DefinitionNavigator(IWorkerContext context, StructureDefinition structure, int index, String path, List names, String type) { + private DefinitionNavigator(IWorkerContext context, StructureDefinition structure, boolean diff, int index, String path, List names, String type) { this.path = path; this.context = context; this.structure = structure; + this.diff = diff; this.index = index; if (type == null) for (String name : names) @@ -96,8 +99,16 @@ public class DefinitionNavigator { public List getNames() { return names; } + + private List list() { + if (diff) { + return structure.getDifferential().getElement(); + } else { + return structure.getSnapshot().getElement(); + } + } public ElementDefinition current() { - return structure.getSnapshot().getElement().get(index); + return list().get(index); } public List slices() throws DefinitionException { @@ -119,14 +130,15 @@ public class DefinitionNavigator { String prefix = current().getPath()+"."; Map nameMap = new HashMap(); - for (int i = index + 1; i < structure.getSnapshot().getElement().size(); i++) { - String path = structure.getSnapshot().getElement().get(i).getPath(); + for (int i = index + 1; i < list().size(); i++) { + String path = list().get(i).getPath(); if (path.startsWith(prefix) && !path.substring(prefix.length()).contains(".")) { - DefinitionNavigator dn = new DefinitionNavigator(context, structure, i, this.path+"."+tail(path), names, null); + DefinitionNavigator dn = new DefinitionNavigator(context, structure, diff, i, this.path+"."+tail(path), names, null); if (nameMap.containsKey(path)) { DefinitionNavigator master = nameMap.get(path); - if (!master.current().hasSlicing()) + ElementDefinition cm = master.current(); + if (!cm.hasSlicing()) throw new DefinitionException("Found slices with no slicing details at "+dn.current().getPath()); if (master.slices == null) master.slices = new ArrayList(); @@ -180,7 +192,7 @@ public class DefinitionNavigator { typeOfChildren = null; StructureDefinition sd = context.fetchResource(StructureDefinition.class, /* GF#13465 : this somehow needs to be revisited type.hasProfile() ? type.getProfile() : */ type.getWorkingCode(), src); if (sd != null) { - DefinitionNavigator dn = new DefinitionNavigator(context, sd, 0, path, names, sd.getType()); + DefinitionNavigator dn = new DefinitionNavigator(context, sd, diff, 0, path, names, sd.getType()); typeChildren = dn.children(); } else throw new DefinitionException("Unable to find definition for "+type.getWorkingCode()+(type.hasProfile() ? "("+type.getProfile()+")" : ""));