From a7d0b645dc2250f791508a4c7116cc43f88dcf58 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Wed, 20 Nov 2024 12:14:52 +1100 Subject: [PATCH] fix code generation for enums --- .../r5/profilemodel/gen/PECodeGenerator.java | 50 +++++++++++++------ 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/profilemodel/gen/PECodeGenerator.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/profilemodel/gen/PECodeGenerator.java index 2be24f354..d7ab86c29 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/profilemodel/gen/PECodeGenerator.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/profilemodel/gen/PECodeGenerator.java @@ -247,6 +247,8 @@ public class PECodeGenerator { org.hl7.fhir.r5.model.ValueSet vs = workerContext.fetchResource(org.hl7.fhir.r5.model.ValueSet.class, binding.getValueSet(), field.getProfile()); if (vs != null) { ValueSetExpansionOutcome vse = workerContext.expandVS(vs, false, false); + Set codes = new HashSet<>(); + boolean hasDups = false; if (vse.isOk()) { String baseName = Utilities.nmtokenize(Utilities.singularise(vs.getName())); String name = baseName; @@ -259,22 +261,36 @@ public class PECodeGenerator { for (int i = 0; i < vse.getValueset().getExpansion().getContains().size(); i++) { ValueSetExpansionContainsComponent cc = vse.getValueset().getExpansion().getContains().get(i); String code = Utilities.javaTokenize(cc.getCode(), true).toUpperCase(); + if (Utilities.isInteger(code)) { + code = "C_"+code; + } if (cc.getAbstract()) { code = "_"+code; } + if (codes.contains(code)) { + char sfx = 'A'; + while (codes.contains(code+sfx)) { + sfx++; + } + code = code + sfx; + hasDups = true; + } + codes.add(code); cc.setUserData(UserDataNames.java_code, code); w(enums, " "+code+(i < vse.getValueset().getExpansion().getContains().size() - 1 ? "," : ";")+" // \""+cc.getDisplay()+"\" = "+cc.getSystem()+"#"+cc.getCode()); } w(enums, ""); - w(enums, " public static "+name+" fromCode(String s) {"); - w(enums, " switch (s) {"); - for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) { - w(enums, " case \""+cc.getCode()+"\": return "+cc.getUserString(UserDataNames.java_code)+";"); + if (!hasDups) { + w(enums, " public static "+name+" fromCode(String s) {"); + w(enums, " switch (s) {"); + for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) { + w(enums, " case \""+cc.getCode()+"\": return "+cc.getUserString(UserDataNames.java_code)+";"); + } + w(enums, " default: return null;"); + w(enums, " }"); + w(enums, " }"); + w(enums, ""); } - w(enums, " default: return null;"); - w(enums, " }"); - w(enums, " }"); - w(enums, ""); w(enums, " public static "+name+" fromCoding(Coding c) {"); for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) { if (cc.hasVersion()) { @@ -309,15 +325,17 @@ public class PECodeGenerator { w(enums, " }"); w(enums, ""); - w(enums, " public String toCode() {"); - w(enums, " switch (this) {"); - for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) { - w(enums, " case "+cc.getUserString(UserDataNames.java_code)+": return \""+cc.getCode()+"\";"); + if (!hasDups) { + w(enums, " public String toCode() {"); + w(enums, " switch (this) {"); + for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) { + w(enums, " case "+cc.getUserString(UserDataNames.java_code)+": return \""+cc.getCode()+"\";"); + } + w(enums, " default: return null;"); + w(enums, " }"); + w(enums, " }"); + w(enums, ""); } - w(enums, " default: return null;"); - w(enums, " }"); - w(enums, " }"); - w(enums, ""); w(enums, " public Coding toCoding() {"); w(enums, " switch (this) {"); for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) {