From 832906da1337238d5443d1c8c772e35a40b70f29 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Wed, 23 Oct 2024 22:59:54 +1030 Subject: [PATCH] update tests for changes to PE code generation --- .../r4/profilemodel/gen/PECodeGenerator.java | 46 ++++++++++++++++--- .../org/hl7/fhir/r5/profiles/PETests.java | 16 +++---- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/profilemodel/gen/PECodeGenerator.java b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/profilemodel/gen/PECodeGenerator.java index 945269162..5292000cd 100644 --- a/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/profilemodel/gen/PECodeGenerator.java +++ b/org.hl7.fhir.r4/src/main/java/org/hl7/fhir/r4/profilemodel/gen/PECodeGenerator.java @@ -60,6 +60,29 @@ import org.hl7.fhir.utilities.CommaSeparatedStringBuilder; import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.Utilities; +/** + * + * The easiest way to generate code is to use the FHIR Validator, which can generate java classes for profiles + * using this code. Parameters: + * + * -codegen -version r4 -ig hl7.fhir.dk.core#3.2.0 -profiles http://hl7.dk/fhir/core/StructureDefinition/dk-core-gln-identifier,http://hl7.dk/fhir/core/StructureDefinition/dk-core-patient -output /Users/grahamegrieve/temp/codegen -package-name org.hl7.fhir.test + * + * Parameter Documentation: + * -codegen: tells the validator to generate code + * -version {r4|5}: which version to generate for + * -ig {name}: loads an IG (and it's dependencies) - see -ig documentation for the validator + * -profiles {list}: a comma separated list of profile URLs to generate code for + * -output {folder}: the folder where to generate the output java class source code + * -package-name {name}: the name of the java package to generate in + * + * options + * -option {name}: a code generation option, one of: + * + * narrative: generate code for the resource narrative (recommended: don't - leave that for the native resource level) + * meta: generate code the what's in meta + * contained: generate code for contained resources + * all-elements: generate code for all elements, not just the key elements (makes the code verbose) + */ public class PECodeGenerator { @@ -229,7 +252,7 @@ public class PECodeGenerator { w(enums, " public enum "+name+" {"); for (int i = 0; i < vse.getValueset().getExpansion().getContains().size(); i++) { ValueSetExpansionContainsComponent cc = vse.getValueset().getExpansion().getContains().get(i); - String code = Utilities.nmtokenize(cc.getCode()).toUpperCase(); + String code = Utilities.javaTokenize(cc.getCode(), true).toUpperCase(); if (cc.getAbstract()) { code = "_"+code; } @@ -375,7 +398,12 @@ public class PECodeGenerator { private void genLoad(boolean isPrim, boolean isAbstract, String name, String sname, String type, String init, String ptype, String ltype, String cname, String csname, boolean isList, boolean isFixed, PEType typeInfo, boolean isEnum) { if (isList) { w(load, " for (PEInstance item : src.children(\""+sname+"\")) {"); - w(load, " "+name+".add(("+type+") item.asDataType());"); + + if ("BackboneElement".equals(type)) { + w(load, " "+name+".add(("+type+") item.asElement());"); + } else { + w(load, " "+name+".add(("+type+") item.asDataType());"); + } w(load, " }"); } else if (isEnum) { w(load, " if (src.hasChild(\""+name+"\")) {"); @@ -384,7 +412,7 @@ public class PECodeGenerator { } else if ("Coding".equals(typeInfo.getName())) { w(load, " "+name+" = "+type+".fromCoding((Coding) src.child(\""+name+"\").asDataType());"); } else { - w(load, " "+name+" = "+type+".fromCode(src.child(\""+name+"\").asDataType()).primitiveValue());"); + w(load, " "+name+" = "+type+".fromCode(src.child(\""+name+"\").asDataType().primitiveValue());"); } w(load, " }"); } else if (isPrim) { @@ -401,8 +429,12 @@ public class PECodeGenerator { w(load, " "+name+" = "+type+".fromSource(src.child(\""+name+"\"));"); w(load, " }"); } else { - w(load, " if (src.hasChild(\""+name+"\")) {"); - w(load, " "+name+" = ("+type+") src.child(\""+name+"\").asDataType();"); + w(load, " if (src.hasChild(\""+name+"\")) {"); + if ("BackboneElement".equals(type)) { + w(load, " "+name+" = ("+type+") src.child(\""+name+"\").asElement();"); + } else { + w(load, " "+name+" = ("+type+") src.child(\""+name+"\").asDataType();"); + } w(load, " }"); } } @@ -424,7 +456,7 @@ public class PECodeGenerator { } else if ("Coding".equals(typeInfo.getName())) { w(save, " tgt.addChild(\""+sname+"\", "+name+".toCoding());"); } else { - w(save, " tgt.addChild(\""+sname+"\", "+name+").toCode();"); + w(save, " tgt.addChild(\""+sname+"\", "+name+".toCode());"); } w(save, " }"); } else if (isPrim) { @@ -593,7 +625,7 @@ public class PECodeGenerator { private IWorkerContext workerContext; private String canonical; private String pkgName; - private String version; + private String version = "r4"; // options: private ExtensionPolicy extensionPolicy; diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/profiles/PETests.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/profiles/PETests.java index 58e16476a..40e3b7667 100644 --- a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/profiles/PETests.java +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/profiles/PETests.java @@ -101,7 +101,7 @@ public class PETests { checkElement(children.get(2), "language", "language", 0, 1, false, "http://hl7.org/fhir/StructureDefinition/code", 2, "language"); checkElement(children.get(3), "text", "text", 0, 1, false, "http://hl7.org/fhir/StructureDefinition/Narrative", 3, "text"); checkElement(children.get(4), "contained", "contained", 0, Integer.MAX_VALUE, false, "http://hl7.org/fhir/StructureDefinition/Resource", 4, "contained"); - checkElement(children.get(5), "extension", "extension", 0, Integer.MAX_VALUE, false, "http://hl7.org/fhir/StructureDefinition/Extension", 3, "extension.where(((url = 'http://hl7.org/fhir/test/StructureDefinition/pe-extension-simple') or (url = 'http://hl7.org/fhir/test/StructureDefinition/pe-extension-complex')).not())"); + checkElement(children.get(5), "extension", "extension", 0, Integer.MAX_VALUE, false, "http://hl7.org/fhir/StructureDefinition/Extension", 2, "extension.where(((url = 'http://hl7.org/fhir/test/StructureDefinition/pe-extension-simple') or (url = 'http://hl7.org/fhir/test/StructureDefinition/pe-extension-complex')).not())"); checkElement(children.get(6), "extension", "simple", 0, 1, false, "http://hl7.org/fhir/StructureDefinition/code", 2, "extension('http://hl7.org/fhir/test/StructureDefinition/pe-extension-simple').value"); checkElement(children.get(7), "extension", "complex", 0, 1, false, "http://hl7.org/fhir/test/StructureDefinition/pe-extension-complex", 4, "extension('http://hl7.org/fhir/test/StructureDefinition/pe-extension-complex')"); checkElement(children.get(8), "identifier", "identifier", 0, 1, false, "http://hl7.org/fhir/StructureDefinition/Identifier", 7, "identifier"); @@ -116,7 +116,7 @@ public class PETests { checkElement(children.get(17), "value[x]", "valueCodeableConcept", 0, 1, false, "http://hl7.org/fhir/test/StructureDefinition/pe-profile2", 4, "value.ofType(CodeableConcept)"); List gchildren = children.get(11).children(); - checkElement(gchildren.get(0), "extension", "extension", 0, Integer.MAX_VALUE, true, "http://hl7.org/fhir/StructureDefinition/Extension", 3, "extension"); + checkElement(gchildren.get(0), "extension", "extension", 0, Integer.MAX_VALUE, true, "http://hl7.org/fhir/StructureDefinition/Extension", 2, "extension"); checkElement(gchildren.get(1), "coding", "coding", 0, Integer.MAX_VALUE, true, "http://hl7.org/fhir/StructureDefinition/Coding", 6, "coding"); checkElement(gchildren.get(2), "text", "text", 0, 1, true, "http://hl7.org/fhir/StructureDefinition/string", 2, "text"); @@ -128,17 +128,17 @@ public class PETests { checkElement(gchildren.get(3), "text", "text", 1, 1, false, "http://hl7.org/fhir/StructureDefinition/string", 2, "text"); List ggchildren = gchildren.get(3).children("http://hl7.org/fhir/StructureDefinition/string"); - checkElement(ggchildren.get(0), "extension", "extension", 0, Integer.MAX_VALUE, false, "http://hl7.org/fhir/StructureDefinition/Extension", 3, "extension"); + checkElement(ggchildren.get(0), "extension", "extension", 0, Integer.MAX_VALUE, false, "http://hl7.org/fhir/StructureDefinition/Extension", 2, "extension"); checkElement(ggchildren.get(1), "value", "value", 1, 1, false, null, 3, "value"); gchildren = children.get(7).children("http://hl7.org/fhir/StructureDefinition/Extension"); - checkElement(gchildren.get(0), "extension", "extension", 0, Integer.MAX_VALUE, false, "http://hl7.org/fhir/StructureDefinition/Extension", 3, "extension.where(((url = 'slice1') or (url = 'slice2') or (url = 'slice3')).not())"); + checkElement(gchildren.get(0), "extension", "extension", 0, Integer.MAX_VALUE, false, "http://hl7.org/fhir/StructureDefinition/Extension", 2, "extension.where(((url = 'slice1') or (url = 'slice2') or (url = 'slice3')).not())"); checkElement(gchildren.get(1), "extension", "slice1", 0, 2, false, "http://hl7.org/fhir/StructureDefinition/Coding", 6, "extension('slice1').value"); checkElement(gchildren.get(2), "extension", "slice2", 0, Integer.MAX_VALUE, false, "http://hl7.org/fhir/StructureDefinition/string", 2, "extension('slice2').value"); checkElement(gchildren.get(3), "extension", "slice3", 1, 1, false, "http://hl7.org/fhir/StructureDefinition/Extension", 3, "extension('slice3')"); ggchildren = gchildren.get(3).children("http://hl7.org/fhir/StructureDefinition/Extension"); - checkElement(ggchildren.get(0), "extension", "extension", 0, Integer.MAX_VALUE, false, "http://hl7.org/fhir/StructureDefinition/Extension", 3, "extension"); + checkElement(ggchildren.get(0), "extension", "extension", 0, Integer.MAX_VALUE, false, "http://hl7.org/fhir/StructureDefinition/Extension", 2, "extension"); checkElement(ggchildren.get(1), "extension", "slice3a", 0, 2, false, "http://hl7.org/fhir/StructureDefinition/Coding", 6, "extension('slice3a').value"); checkElement(ggchildren.get(2), "extension", "slice3b", 0, Integer.MAX_VALUE, false, "http://hl7.org/fhir/StructureDefinition/string", 2, "extension('slice3b').value"); } @@ -177,7 +177,7 @@ public class PETests { Assertions.assertEquals("\\-", pe.documentation()); List children = pe.children("Patient"); - Assertions.assertEquals(28, children.size()); + Assertions.assertEquals(26, children.size()); pe = children.get(9); Assertions.assertEquals("birthsex", pe.name()); @@ -269,7 +269,7 @@ public class PETests { Assertions.assertEquals("May be used to represent additional information that is not part of the basic definition of the resource. To make the use of extensions safe and managable, there is a strict set of governance applied to the definition and use of extensions. Though any implementer can define an extension, there is a set of requirements that SHALL be met as part of the definition of the extension.", pe.documentation()); iChildren = pe.children("http://hl7.org/fhir/StructureDefinition/Extension"); - Assertions.assertEquals(4, iChildren.size()); + Assertions.assertEquals(3, iChildren.size()); pe = iChildren.get(1); Assertions.assertEquals("extension", pe.name()); Assertions.assertEquals("extension", pe.schemaName()); @@ -283,7 +283,7 @@ public class PETests { iChildren = pe.children("http://hl7.org/fhir/StructureDefinition/Extension"); - Assertions.assertEquals(4, iChildren.size()); + Assertions.assertEquals(3, iChildren.size()); pe = iChildren.get(1); Assertions.assertEquals("extension", pe.name()); Assertions.assertEquals("extension", pe.schemaName());