Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract more specific types in OnlyRules with target profiles #229

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/extractor/OnlyRuleExtractor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fshrules } from 'fsh-sushi';
import { ProcessableElementDefinition } from '../processor';
import { ExportableOnlyRule } from '../exportable';
import { getPath } from '../utils';
Expand All @@ -9,9 +10,17 @@ export class OnlyRuleExtractor {
}
const onlyRule = new ExportableOnlyRule(getPath(input));
input.type.forEach((t, i) => {
if (['Reference', 'CodeableReference'].includes(t.code) && t.targetProfile) {
if (['Reference', 'CodeableReference', 'canonical'].includes(t.code) && t.targetProfile) {
const targeting: Partial<fshrules.OnlyRuleType> = {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh. We haven't leveraged Partial much before. Cool.

if (t.code === 'Reference') {
targeting.isReference = true;
} else if (t.code === 'CodeableReference') {
targeting.isCodeableReference = true;
} else {
targeting.isCanonical = true;
}
t.targetProfile.forEach((tp, tpi) => {
onlyRule.types.push({ type: tp, isReference: true });
onlyRule.types.push(Object.assign({ type: tp }, targeting));
input.processedPaths.push(`type[${i}].targetProfile[${tpi}]`);
});
} else if (t.profile) {
Expand Down
100 changes: 98 additions & 2 deletions test/extractor/OnlyRuleExtractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ describe('OnlyRuleExtractor', () => {
const onlyRule = OnlyRuleExtractor.process(element);
const expectedRule = new ExportableOnlyRule('activity.performedActivity');
expectedRule.types = [
{ type: 'http://hl7.org/fhir/StructureDefinition/Observation', isReference: true },
{ type: 'http://hl7.org/fhir/StructureDefinition/MolecularSequence', isReference: true }
{ type: 'http://hl7.org/fhir/StructureDefinition/Observation', isCodeableReference: true },
{
type: 'http://hl7.org/fhir/StructureDefinition/MolecularSequence',
isCodeableReference: true
}
];
expect(onlyRule).toEqual<ExportableOnlyRule>(expectedRule);
expect(element.processedPaths).toEqual([
Expand All @@ -107,6 +110,99 @@ describe('OnlyRuleExtractor', () => {
expect(element.processedPaths).toEqual(['type[0].code', 'type[1].profile[0]', 'type[1].code']);
});

it('should extract an only rule with a subset of a canonical type', () => {
const element = ProcessableElementDefinition.fromJSON(
sdWithCodeableReference.differential.element[4]
);
const onlyRule = OnlyRuleExtractor.process(element);
const expectedRule = new ExportableOnlyRule('extension[toast].value[x]');
expectedRule.types = [
{ type: 'http://hl7.org/fhir/StructureDefinition/Observation', isCanonical: true },
{
type: 'http://hl7.org/fhir/StructureDefinition/Patient',
isCanonical: true
}
];
expect(onlyRule).toEqual<ExportableOnlyRule>(expectedRule);
expect(element.processedPaths).toEqual([
'type[0].targetProfile[0]',
'type[0].targetProfile[1]',
'type[0].code'
]);
});

it('should extract an only rule on an element with both Reference and CodeableReference types', () => {
const element = ProcessableElementDefinition.fromJSON(
sdWithCodeableReference.differential.element[2]
);
const onlyRule = OnlyRuleExtractor.process(element);
const expectedRule = new ExportableOnlyRule('extension[bar].value[x]');
expectedRule.types = [
{ type: 'http://hl7.org/fhir/StructureDefinition/Practitioner', isReference: true },
{ type: 'http://hl7.org/fhir/StructureDefinition/Patient', isReference: true },
{ type: 'http://hl7.org/fhir/StructureDefinition/Observation', isCodeableReference: true },
{
type: 'http://hl7.org/fhir/StructureDefinition/DiagnosticReport',
isCodeableReference: true
}
];
expect(onlyRule).toEqual<ExportableOnlyRule>(expectedRule);
expect(element.processedPaths).toEqual([
'type[0].targetProfile[0]',
'type[0].targetProfile[1]',
'type[0].code',
'type[1].targetProfile[0]',
'type[1].targetProfile[1]',
'type[1].code'
]);
});

it('should extract an only rule on an element with CodeableReference and non-Reference types', () => {
const element = ProcessableElementDefinition.fromJSON(
sdWithCodeableReference.differential.element[3]
);
const onlyRule = OnlyRuleExtractor.process(element);
const expectedRule = new ExportableOnlyRule('extension[cookie].value[x]');
expectedRule.types = [
{ type: 'string' },
{ type: 'http://hl7.org/fhir/StructureDefinition/Observation', isCodeableReference: true },
{
type: 'http://hl7.org/fhir/StructureDefinition/DiagnosticReport',
isCodeableReference: true
}
];
expect(onlyRule).toEqual<ExportableOnlyRule>(expectedRule);
expect(element.processedPaths).toEqual([
'type[0].code',
'type[1].targetProfile[0]',
'type[1].targetProfile[1]',
'type[1].code'
]);
});

it('should extract an only rule with canonical and non-canonical types', () => {
const element = ProcessableElementDefinition.fromJSON(
sdWithCodeableReference.differential.element[5]
);
const onlyRule = OnlyRuleExtractor.process(element);
const expectedRule = new ExportableOnlyRule('extension[blank].value[x]');
expectedRule.types = [
{ type: 'http://hl7.org/fhir/StructureDefinition/Patient', isCanonical: true },
{
type: 'http://hl7.org/fhir/StructureDefinition/Observation',
isCanonical: true
},
{ type: 'Annotation' }
];
expect(onlyRule).toEqual<ExportableOnlyRule>(expectedRule);
expect(element.processedPaths).toEqual([
'type[0].targetProfile[0]',
'type[0].targetProfile[1]',
'type[0].code',
'type[1].code'
]);
});

it('should return null when the element has no type info', () => {
const element = ProcessableElementDefinition.fromJSON(looseSD.differential.element[4]);
const onlyRule = OnlyRuleExtractor.process(element);
Expand Down
65 changes: 65 additions & 0 deletions test/extractor/fixtures/only-profile-with-codeablereference.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,71 @@
"versioning": "either"
}
]
},
{
"id": "CarePlan.extension:bar.value[x]",
"path": "CarePlan.extension:bar.value[x]",
"type": [
{
"code": "Reference",
"targetProfile": [
"http://hl7.org/fhir/StructureDefinition/Practitioner",
"http://hl7.org/fhir/StructureDefinition/Patient"
]
},
{
"code": "CodeableReference",
"targetProfile": [
"http://hl7.org/fhir/StructureDefinition/Observation",
"http://hl7.org/fhir/StructureDefinition/DiagnosticReport"
]
}
]
},
{
"id": "CarePlan.extension:cookie.value[x]",
"path": "CarePlan.extension:cookie.value[x]",
"type": [
{
"code": "string"
},
{
"code": "CodeableReference",
"targetProfile": [
"http://hl7.org/fhir/StructureDefinition/Observation",
"http://hl7.org/fhir/StructureDefinition/DiagnosticReport"
]
}
]
},
{
"id": "CarePlan.extension:toast.value[x]",
"path": "CarePlan.extension:toast.value[x]",
"type": [
{
"code": "canonical",
"targetProfile": [
"http://hl7.org/fhir/StructureDefinition/Observation",
"http://hl7.org/fhir/StructureDefinition/Patient"
]
}
]
},
{
"id": "CarePlan.extension:blank.value[x]",
"path": "CarePlan.extension:blank.value[x]",
"type": [
{
"code": "canonical",
"targetProfile": [
"http://hl7.org/fhir/StructureDefinition/Patient",
"http://hl7.org/fhir/StructureDefinition/Observation"
]
},
{
"code": "Annotation"
}
]
}
]
}
Expand Down