-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove choice type specifier type if empty.
- Loading branch information
Showing
6 changed files
with
138 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 2 additions & 39 deletions
41
Src/java/cql-to-elm/src/main/java/org/cqframework/cql/cql2elm/elm/ElmEdit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,7 @@ | ||
package org.cqframework.cql.cql2elm.elm; | ||
|
||
import org.hl7.cql_annotations.r1.Annotation; | ||
import org.hl7.elm.r1.Element; | ||
|
||
public enum ElmEdit { | ||
REMOVE_LOCATOR { | ||
@Override | ||
public void edit(Element element) { | ||
element.setLocator(null); | ||
} | ||
}, | ||
REMOVE_ANNOTATION { | ||
@Override | ||
public void edit(Element element) { | ||
element.setLocalId(null); | ||
if (element.getAnnotation() != null) { | ||
for (int i = 0; i < element.getAnnotation().size(); i++) { | ||
var x = element.getAnnotation().get(i); | ||
if (x instanceof Annotation) { | ||
var a = (Annotation) x; | ||
// TODO: Remove narrative but _not_ tags | ||
// Tags are necessary for `allowFluent` compiler resolution | ||
// to work correctly | ||
a.setS(null); | ||
if (a.getT().isEmpty()) { | ||
element.getAnnotation().remove(i); | ||
i--; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
REMOVE_RESULT_TYPE { | ||
@Override | ||
public void edit(Element element) { | ||
element.setResultTypeName(null); | ||
element.setResultTypeSpecifier(null); | ||
} | ||
}; | ||
|
||
public abstract void edit(Element element); | ||
public interface ElmEdit { | ||
void edit(Element element); | ||
} |
58 changes: 58 additions & 0 deletions
58
Src/java/cql-to-elm/src/main/java/org/cqframework/cql/cql2elm/elm/ElmEditEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.cqframework.cql.cql2elm.elm; | ||
|
||
import org.hl7.cql_annotations.r1.Annotation; | ||
import org.hl7.elm.r1.ChoiceTypeSpecifier; | ||
import org.hl7.elm.r1.Element; | ||
|
||
public enum ElmEditEnum implements ElmEdit { | ||
REMOVE_LOCATOR { | ||
public void edit(Element element) { | ||
element.setLocator(null); | ||
} | ||
}, | ||
REMOVE_ANNOTATION { | ||
public void edit(Element element) { | ||
element.setLocalId(null); | ||
if (element.getAnnotation() != null) { | ||
for (int i = 0; i < element.getAnnotation().size(); i++) { | ||
var x = element.getAnnotation().get(i); | ||
if (x instanceof Annotation) { | ||
var a = (Annotation) x; | ||
// TODO: Remove narrative but _not_ tags | ||
// Tags are necessary for `allowFluent` compiler resolution | ||
// to work correctly | ||
a.setS(null); | ||
if (a.getT().isEmpty()) { | ||
element.getAnnotation().remove(i); | ||
i--; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
REMOVE_RESULT_TYPE { | ||
public void edit(Element element) { | ||
element.setResultTypeName(null); | ||
element.setResultTypeSpecifier(null); | ||
} | ||
}, | ||
REMOVE_CHOICE_TYPE_SPECIFIER_TYPE_IF_EMPTY { | ||
// The ChoiceTypeSpecifier ELM node has a deprecated `type` element which, if not null, clashes with the | ||
// `"type" : "ChoiceTypeSpecifier"` field in the JSON serialization of the node. This does not happen in the XML | ||
// serialization which nests <type> tags inside <ChoiceTypeSpecifier>. | ||
// Because the `type` element is deprecated, it is not normally populated by the compiler anymore and | ||
// stays null in the ChoiceTypeSpecifier instance. It is however set to an empty list if you just call | ||
// ChoiceTypeSpecifier.getType() (which we do during the ELM optimization stage in the compiler), so | ||
// this edit is needed to "protect" the downstream JSON serialization if it can be done without data loss. | ||
|
||
public void edit(Element element) { | ||
if (element instanceof ChoiceTypeSpecifier) { | ||
var choice = (ChoiceTypeSpecifier) element; | ||
if (choice.getType().isEmpty()) { | ||
choice.setType(null); | ||
} | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/elm/ElmEditTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.cqframework.cql.cql2elm.elm; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.List; | ||
import org.hl7.elm.r1.ChoiceTypeSpecifier; | ||
import org.hl7.elm.r1.NamedTypeSpecifier; | ||
import org.hl7.elm.r1.TypeSpecifier; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ElmEditTest { | ||
|
||
@Test | ||
void removeChoiceTypeSpecifierTypeIfEmpty() { | ||
var extChoiceTypeSpecifier = new ExtChoiceTypeSpecifier(); | ||
|
||
extChoiceTypeSpecifier.setType(List.of()); | ||
ElmEditEnum.REMOVE_CHOICE_TYPE_SPECIFIER_TYPE_IF_EMPTY.edit(extChoiceTypeSpecifier); | ||
assertNull(extChoiceTypeSpecifier.getType()); | ||
|
||
var typeSpecifiers = List.of((TypeSpecifier) new NamedTypeSpecifier()); | ||
extChoiceTypeSpecifier.setType(typeSpecifiers); | ||
ElmEditEnum.REMOVE_CHOICE_TYPE_SPECIFIER_TYPE_IF_EMPTY.edit(extChoiceTypeSpecifier); | ||
assertSame(typeSpecifiers, extChoiceTypeSpecifier.getType()); | ||
} | ||
|
||
private static class ExtChoiceTypeSpecifier extends ChoiceTypeSpecifier { | ||
public List<TypeSpecifier> getType() { | ||
return type; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Src/java/cql-to-elm/src/test/java/org/cqframework/cql/cql2elm/elm/ElmEditorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.cqframework.cql.cql2elm.elm; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.List; | ||
import org.hl7.elm.r1.ChoiceTypeSpecifier; | ||
import org.hl7.elm.r1.Element; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ElmEditorTest { | ||
boolean editCalled = false; | ||
|
||
@Test | ||
void applyEdits() { | ||
editCalled = false; | ||
var editCounter = new ElmEdit() { | ||
public void edit(Element element) { | ||
editCalled = true; | ||
} | ||
}; | ||
var edits = List.of((ElmEdit) editCounter); | ||
ElmEditor editor = new ElmEditor(edits); | ||
|
||
editCalled = false; | ||
editor.applyEdits(null, null); | ||
assertFalse(editCalled); | ||
|
||
editCalled = false; | ||
editor.applyEdits(null, new ChoiceTypeSpecifier()); | ||
assertTrue(editCalled); | ||
} | ||
} |