Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Fix handling of elements with name "class"
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrimes committed Nov 11, 2019
1 parent 1d91b95 commit 1751f8f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
23 changes: 13 additions & 10 deletions bunsen-core/src/main/scala/com/cerner/bunsen/EncoderBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,29 @@ private[bunsen] class EncoderBuilder(fhirContext: FhirContext,
* Returns the accessor method for the given child field.
*/
private def accessorFor(field: BaseRuntimeChildDefinition): String = {
// Elements called `class` have an underscore appended to the end of their name within getters.
val elementName = if (field.getElementName.equals("class"))
field.getElementName.capitalize + "_"
else field.getElementName.capitalize

// Primitive single-value types typically use the Element suffix in their
// accessors, with the exception of the "div" field for reasons that are not clear.
if (field.isInstanceOf[RuntimeChildPrimitiveDatatypeDefinition] &&
field.getMax == 1 &&
field.getElementName != "div")
"get" + field.getElementName.capitalize + "Element"
else {
if (field.getElementName.equals("class")) {
"get" + field.getElementName.capitalize + "_"
} else {
"get" + field.getElementName.capitalize
}
}
"get" + elementName + "Element"
else
"get" + elementName
}

/**
* Returns the setter for the given field name.s
*/
private def setterFor(field: BaseRuntimeChildDefinition): String = {
// Elements called `class` have an underscore appended to the end of their name within getters.
val elementName = if (field.getElementName.equals("class"))
field.getElementName.capitalize + "_"
else field.getElementName.capitalize

// Primitive single-value types typically use the Element suffix in their
// setters, with the exception of the "div" field for reasons that are not clear.
Expand All @@ -140,9 +143,9 @@ private[bunsen] class EncoderBuilder(fhirContext: FhirContext,
// Enumerations are set directly rather than via elements.
!field.isInstanceOf[RuntimeChildPrimitiveEnumerationDatatypeDefinition] &&
field.getMax == 1 && field.getElementName != "div")
"set" + field.getElementName.capitalize + "Element"
"set" + elementName + "Element"
else
"set" + field.getElementName.capitalize
"set" + elementName
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hl7.fhir.dstu3.model.Annotation;
import org.hl7.fhir.dstu3.model.Coding;
import org.hl7.fhir.dstu3.model.Condition;
import org.hl7.fhir.dstu3.model.Coverage;
import org.hl7.fhir.dstu3.model.DateTimeType;
import org.hl7.fhir.dstu3.model.IntegerType;
import org.hl7.fhir.dstu3.model.Medication;
Expand Down Expand Up @@ -60,6 +61,10 @@ public class FhirEncodersTest {
private static Dataset<MedicationRequest> medDataset;
private static MedicationRequest decodedMedRequest;

private static Coverage coverage = TestData.newCoverage();
private static Dataset<Coverage> coverageDataset;
private static Coverage decodedCoverage;

/**
* Set up Spark.
*/
Expand All @@ -85,6 +90,9 @@ public static void setUp() {
medDataset = spark.createDataset(ImmutableList.of(medRequest),
encoders.of(MedicationRequest.class, Medication.class, Provenance.class));
decodedMedRequest = medDataset.head();

coverageDataset = spark.createDataset(ImmutableList.of(coverage), encoders.of(Coverage.class));
decodedCoverage = coverageDataset.head();
}

/**
Expand Down Expand Up @@ -333,4 +341,12 @@ public void testEncoderCached() throws IOException {
Assert.assertSame(encoders.of(Patient.class),
encoders.of(Patient.class));
}

@Test
public void testPrimitiveClassDecoding() {
Assert.assertEquals(coverage.getGrouping().getClass_(),
coverageDataset.select("grouping.class").head().get(0));
Assert.assertEquals(coverage.getGrouping().getClass_(),
decodedCoverage.getGrouping().getClass_());
}
}
19 changes: 19 additions & 0 deletions bunsen-stu3/src/test/java/com/cerner/bunsen/stu3/TestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.hl7.fhir.dstu3.model.Annotation;
import org.hl7.fhir.dstu3.model.CodeableConcept;
import org.hl7.fhir.dstu3.model.Condition;
import org.hl7.fhir.dstu3.model.Coverage;
import org.hl7.fhir.dstu3.model.DateTimeType;
import org.hl7.fhir.dstu3.model.Identifier;
import org.hl7.fhir.dstu3.model.IntegerType;
Expand Down Expand Up @@ -192,4 +193,22 @@ public static MedicationRequest newMedRequest() {

return medReq;
}

/**
* Returns a FHIR Coverage resource for testing purposes.
*/
public static Coverage newCoverage() {
Coverage coverage = new Coverage();

coverage.setId("test-coverage");
coverage.setStatus(Coverage.CoverageStatus.ACTIVE);
coverage.setSubscriber(new Reference("Patient/test-patient"));

Coverage.GroupComponent groupComponent = new Coverage.GroupComponent();
groupComponent.setGroup("some-group");
groupComponent.setClass_("some-group-class");
coverage.setGrouping(groupComponent);

return coverage;
}
}

0 comments on commit 1751f8f

Please sign in to comment.