Skip to content

Commit

Permalink
Fish With Version (#11)
Browse files Browse the repository at this point in the history
* Support getting definitions with a version

Support passing in a |version along with an id/name/url when getting definitions.
If the version does not match the specified version, don't return the definition.

* When fishing with a version, only return definition if it has a version and it matches
  • Loading branch information
jafeltra authored May 15, 2023
1 parent 428268c commit 9e0f64c
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 12 deletions.
9 changes: 7 additions & 2 deletions src/FHIRDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,14 @@ export class FHIRDefinitions {
const defsToSearch: FHIRDefinitions[] = [this];
while (defsToSearch.length > 0) {
const currentFHIRDefs = defsToSearch.shift();
const def = currentFHIRDefs[map].get(item);
const [base, ...versionParts] = item?.split('|') ?? ['', ''];
const version = versionParts.join('|') || null;
const def = currentFHIRDefs[map].get(base);
if (def) {
return def;
if (version == null || version === def?.version) {
// Only return the found definition if the version matches (if provided)
return def;
}
}
if (currentFHIRDefs.childFHIRDefs.length > 0) {
defsToSearch.push(...currentFHIRDefs.childFHIRDefs);
Expand Down
63 changes: 53 additions & 10 deletions test/FHIRDefinitions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ describe('FHIRDefinitions', () => {
beforeAll(() => {
defs = new FHIRDefinitions();
loadFromPath(path.join(__dirname, 'testhelpers', 'testdefs'), 'r4-definitions', defs);
defs.fishForFHIR('Condition');
defs.fishForFHIR('eLTSSServiceModel');
defs.fishForFHIR('boolean');
defs.fishForFHIR('Address');
defs.fishForFHIR('vitalsigns');
defs.fishForFHIR('patient-mothersMaidenName');
defs.fishForFHIR('allergyintolerance-clinical', Type.ValueSet);
defs.fishForFHIR('allergyintolerance-clinical', Type.CodeSystem);
defs.fishForFHIR('w3c-provenance-activity-type');
});

beforeEach(() => {
Expand Down Expand Up @@ -126,7 +117,7 @@ describe('FHIRDefinitions', () => {
).toEqual(allergyStatusValueSetByID);
});

it('should find base FHIR code sytems', () => {
it('should find base FHIR code systems', () => {
// Surprise! It turns out that the AllergyIntolerance status value set and code system
// have the same ID!
const allergyStatusCodeSystemByID = defs.fishForFHIR(
Expand Down Expand Up @@ -364,5 +355,57 @@ describe('FHIRDefinitions', () => {
const conditionByID = defsWithChildDefs.fishForFHIR('Condition', Type.Resource);
expect(conditionByID.version).toEqual('4.0.2');
});

it('should find definitions when fished by id with version', () => {
const vitalSignsById = defs.fishForFHIR('vitalsigns|4.0.1', Type.Profile);
expect(vitalSignsById).toBeDefined();
expect(vitalSignsById.name).toBe('observation-vitalsigns');
expect(vitalSignsById.url).toBe('http://hl7.org/fhir/StructureDefinition/vitalsigns');
expect(vitalSignsById.version).toBe('4.0.1');
});

it('should find definitions when fished by name with version', () => {
const vitalSignsByName = defs.fishForFHIR('observation-vitalsigns|4.0.1', Type.Profile);
expect(vitalSignsByName).toBeDefined();
expect(vitalSignsByName.id).toBe('vitalsigns');
expect(vitalSignsByName.url).toBe('http://hl7.org/fhir/StructureDefinition/vitalsigns');
expect(vitalSignsByName.version).toBe('4.0.1');
});

it('should find definitions when fished by url with version', () => {
const vitalSignsByUrl = defs.fishForFHIR(
'http://hl7.org/fhir/StructureDefinition/vitalsigns|4.0.1',
Type.Profile
);
expect(vitalSignsByUrl).toBeDefined();
expect(vitalSignsByUrl.id).toBe('vitalsigns');
expect(vitalSignsByUrl.name).toBe('observation-vitalsigns');
expect(vitalSignsByUrl.version).toBe('4.0.1');
});

it('should find definitions with a version with | in the version', () => {
const simpleProfileById = defs.fishForFHIR('SimpleProfile|1.0.0|a');
expect(simpleProfileById).toBeDefined();
expect(simpleProfileById.id).toBe('SimpleProfile');
expect(simpleProfileById.name).toBe('SimpleProfile');
expect(simpleProfileById.version).toBe('1.0.0|a');
});

it('should return nothing if a definition with matching version is not found', () => {
const vitalSignsById = defs.fishForFHIR('vitalsigns|1.0.0', Type.Profile);
const vitalSignsByName = defs.fishForFHIR('observation-vitalsigns|1.0.0', Type.Profile);
const vitalSignsByUrl = defs.fishForFHIR(
'http://hl7.org/fhir/StructureDefinition/vitalsigns|1.0.0',
Type.Profile
);
expect(vitalSignsById).toBeUndefined();
expect(vitalSignsByName).toBeUndefined();
expect(vitalSignsByUrl).toBeUndefined();
});

it('should return nothing if a definition without a version is found when fishing with a version', () => {
const simpleProfileById = defs.fishForFHIR('SimpleProfileNoVersion|1.0.0');
expect(simpleProfileById).toBeUndefined();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"resourceType": "StructureDefinition",
"id": "SimpleProfile",
"url": "http://example.org/StructureDefinition/SimpleProfile",
"version": "1.0.0|a",
"name": "SimpleProfile",
"status": "active",
"fhirVersion": "4.0.1",
"mapping": [
{
"identity": "rim",
"uri": "http://hl7.org/v3",
"name": "RIM Mapping"
},
{
"identity": "cda",
"uri": "http://hl7.org/v3/cda",
"name": "CDA (R2)"
},
{
"identity": "w5",
"uri": "http://hl7.org/fhir/fivews",
"name": "FiveWs Pattern Mapping"
},
{
"identity": "v2",
"uri": "http://hl7.org/v2",
"name": "HL7 v2 Mapping"
},
{
"identity": "loinc",
"uri": "http://loinc.org",
"name": "LOINC code for the element"
}
],
"kind": "resource",
"abstract": false,
"type": "Patient",
"baseDefinition": "http://hl7.org/fhir/StructureDefinition/Patient",
"derivation": "constraint",
"differential": {
"element": [
{
"id": "Patient",
"path": "Patient"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"resourceType": "StructureDefinition",
"id": "SimpleProfileNoVersion",
"url": "http://example.org/StructureDefinition/SimpleProfileNoVersion",
"name": "SimpleProfileNoVersion",
"status": "active",
"fhirVersion": "4.0.1",
"mapping": [
{
"identity": "rim",
"uri": "http://hl7.org/v3",
"name": "RIM Mapping"
},
{
"identity": "cda",
"uri": "http://hl7.org/v3/cda",
"name": "CDA (R2)"
},
{
"identity": "w5",
"uri": "http://hl7.org/fhir/fivews",
"name": "FiveWs Pattern Mapping"
},
{
"identity": "v2",
"uri": "http://hl7.org/v2",
"name": "HL7 v2 Mapping"
},
{
"identity": "loinc",
"uri": "http://loinc.org",
"name": "LOINC code for the element"
}
],
"kind": "resource",
"abstract": false,
"type": "Patient",
"baseDefinition": "http://hl7.org/fhir/StructureDefinition/Patient",
"derivation": "constraint",
"differential": {
"element": [
{
"id": "Patient",
"path": "Patient"
}
]
}
}

0 comments on commit 9e0f64c

Please sign in to comment.