fix code generation for enums

This commit is contained in:
Grahame Grieve 2024-11-20 12:14:52 +11:00
parent 2d59c27418
commit a7d0b645dc
1 changed files with 34 additions and 16 deletions

View File

@ -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()); org.hl7.fhir.r5.model.ValueSet vs = workerContext.fetchResource(org.hl7.fhir.r5.model.ValueSet.class, binding.getValueSet(), field.getProfile());
if (vs != null) { if (vs != null) {
ValueSetExpansionOutcome vse = workerContext.expandVS(vs, false, false); ValueSetExpansionOutcome vse = workerContext.expandVS(vs, false, false);
Set<String> codes = new HashSet<>();
boolean hasDups = false;
if (vse.isOk()) { if (vse.isOk()) {
String baseName = Utilities.nmtokenize(Utilities.singularise(vs.getName())); String baseName = Utilities.nmtokenize(Utilities.singularise(vs.getName()));
String name = baseName; String name = baseName;
@ -259,13 +261,26 @@ public class PECodeGenerator {
for (int i = 0; i < vse.getValueset().getExpansion().getContains().size(); i++) { for (int i = 0; i < vse.getValueset().getExpansion().getContains().size(); i++) {
ValueSetExpansionContainsComponent cc = vse.getValueset().getExpansion().getContains().get(i); ValueSetExpansionContainsComponent cc = vse.getValueset().getExpansion().getContains().get(i);
String code = Utilities.javaTokenize(cc.getCode(), true).toUpperCase(); String code = Utilities.javaTokenize(cc.getCode(), true).toUpperCase();
if (Utilities.isInteger(code)) {
code = "C_"+code;
}
if (cc.getAbstract()) { if (cc.getAbstract()) {
code = "_"+code; 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); cc.setUserData(UserDataNames.java_code, code);
w(enums, " "+code+(i < vse.getValueset().getExpansion().getContains().size() - 1 ? "," : ";")+" // \""+cc.getDisplay()+"\" = "+cc.getSystem()+"#"+cc.getCode()); w(enums, " "+code+(i < vse.getValueset().getExpansion().getContains().size() - 1 ? "," : ";")+" // \""+cc.getDisplay()+"\" = "+cc.getSystem()+"#"+cc.getCode());
} }
w(enums, ""); w(enums, "");
if (!hasDups) {
w(enums, " public static "+name+" fromCode(String s) {"); w(enums, " public static "+name+" fromCode(String s) {");
w(enums, " switch (s) {"); w(enums, " switch (s) {");
for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) { for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) {
@ -275,6 +290,7 @@ public class PECodeGenerator {
w(enums, " }"); w(enums, " }");
w(enums, " }"); w(enums, " }");
w(enums, ""); w(enums, "");
}
w(enums, " public static "+name+" fromCoding(Coding c) {"); w(enums, " public static "+name+" fromCoding(Coding c) {");
for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) { for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) {
if (cc.hasVersion()) { if (cc.hasVersion()) {
@ -309,6 +325,7 @@ public class PECodeGenerator {
w(enums, " }"); w(enums, " }");
w(enums, ""); w(enums, "");
if (!hasDups) {
w(enums, " public String toCode() {"); w(enums, " public String toCode() {");
w(enums, " switch (this) {"); w(enums, " switch (this) {");
for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) { for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) {
@ -318,6 +335,7 @@ public class PECodeGenerator {
w(enums, " }"); w(enums, " }");
w(enums, " }"); w(enums, " }");
w(enums, ""); w(enums, "");
}
w(enums, " public Coding toCoding() {"); w(enums, " public Coding toCoding() {");
w(enums, " switch (this) {"); w(enums, " switch (this) {");
for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) { for (ValueSetExpansionContainsComponent cc : vse.getValueset().getExpansion().getContains()) {