Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
feat: Add ImplementationGuides compile method (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
carvantes authored Jan 29, 2021
1 parent 17f0580 commit e0024a4
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 145 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
lib/
lib/
src/implementationGuides/reducedFHIRPath.ts
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
},
rules: {
'import/extensions': ['error', 'ignorePackages', { ts: 'never' }],
'no-console': ['warn', { allow: ['log', 'error'] }],
'no-console': ['warn', { allow: ['log', 'error', 'warn'] }],
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'error',
'no-useless-constructor': 'off',
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"lint-fix": "eslint --fix . --ext .ts,.tsx",
"build": "tsc",
"watch": "tsc -w",
"test": "jest src --silent && jest scripts --silent",
"test": "jest src --silent",
"test-coverage": "jest --coverage",
"release": "yarn run build && yarn run lint && yarn run test",
"clean": "rm -rf build/* node_modules/* dist/* .serverless/* .nyc_output/* lib/*",
Expand All @@ -31,13 +31,14 @@
"@elastic/elasticsearch": "7",
"aws-elasticsearch-connector": "^8.2.0",
"aws-sdk": "^2.610.0",
"fhir-works-on-aws-interface": "^7.0.1",
"fhir-works-on-aws-interface": "^7.1.0",
"lodash": "^4.17.20",
"nearley": "^2.20.0"
},
"devDependencies": {
"@types/jest": "^26.0.19",
"@types/lodash": "^4.14.161",
"@types/nearley": "^2.11.1",
"@types/node": "^12",
"@typescript-eslint/eslint-plugin": "^4.11.1",
"@typescript-eslint/parser": "^4.11.1",
Expand Down
82 changes: 0 additions & 82 deletions scripts/searchParamsCompiler/compile.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,41 @@ the search-parameters.json file into this directory.
You can download the latest FHIR definition from https://www.hl7.org/fhir/downloads.html or find older FHIR versions at http://hl7.org/fhir/directory.html
It is recommended to install ts-node to execute .ts files in the command line
> npm install -g ts-node
If you are modifying the grammar at reducedFHIRPath.ne you need to compile it. The nearley compiler needs to be installed separately:
> npm install -g nearley
> nearleyc reducedFHIRPath.ne -o reducedFHIRPath.js
> nearleyc reducedFHIRPath.ne -o ../src/implementationGuides/reducedFHIRPath.js
Run the script:
> node run.js <fhirVersion>
> ts-node run.ts <fhirVersion>
*/

const fs = require('fs');
const compile = require('./compile');
import * as fs from 'fs';
import { SearchImplementationGuides } from '../../src/implementationGuides';

const readSearchParamsFile = path => {
const readSearchParamsFile = (path: string) => {
const data = JSON.parse(fs.readFileSync(path, { encoding: 'utf8' }));
return data.entry
.map(x => x.resource)
.map(({ url, name, type, description, base, expression, target }) => ({
url,
name,
type,
description,
base,
expression,
target,
}));
return data.entry.map((x: any) => x.resource);
};

const run = () => {
const run = async () => {
const args = process.argv.slice(2);
if (!args[0]) {
console.log('Error. Missing fhirVersion parameter');
console.log('Usage: node run.js <fhirVersion>');
console.log('Usage: ts-node run.ts <fhirVersion>');
}
const fhirVersion = args[0];
const searchParams = readSearchParamsFile('search-parameters.json');

const compiledSearchParams = compile(searchParams);
const compiledSearchParams = await SearchImplementationGuides.compile(searchParams);

fs.writeFileSync(
`../../src/schema/compiledSearchParameters.${fhirVersion}.json`,
JSON.stringify(compiledSearchParams),
);
};

run();
run()
.then(console.log)
.catch(console.error);
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ Array [
"ActivityDefinition",
],
"type": "reference",
"url": undefined,
"url": "http://hl7.org/fhir/SearchParameter/Library-predecessor",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
* SPDX-License-Identifier: Apache-2.0
*/

const compile = require('./compile');
import { SearchImplementationGuides } from './index';

const { compile } = SearchImplementationGuides;

describe('compile', () => {
test(`simple path - Patient.communication.language`, () => {
test(`simple path - Patient.communication.language`, async () => {
const compiled = compile([
{
resourceType: 'SearchParameter',
url: 'http://hl7.org/fhir/SearchParameter/Patient-language',
name: 'language',
type: 'token',
Expand All @@ -18,12 +21,14 @@ describe('compile', () => {
},
]);

expect(compiled).toMatchSnapshot();
await expect(compiled).resolves.toMatchSnapshot();
});

test(`simple where - Library.relatedArtifact.where(type='predecessor').resource`, () => {
test(`simple where - Library.relatedArtifact.where(type='predecessor').resource`, async () => {
const compiled = compile([
{
resourceType: 'SearchParameter',
url: 'http://hl7.org/fhir/SearchParameter/Library-predecessor',
name: 'predecessor',
description: 'What resource is being referenced',
base: ['Library'],
Expand All @@ -32,12 +37,13 @@ describe('compile', () => {
target: ['Account', 'ActivityDefinition'],
},
]);
expect(compiled).toMatchSnapshot();
await expect(compiled).resolves.toMatchSnapshot();
});

test(`where with resolve() is - Person.link.target.where(resolve() is RelatedPerson)`, () => {
test(`where with resolve() is - Person.link.target.where(resolve() is RelatedPerson)`, async () => {
const compiled = compile([
{
resourceType: 'SearchParameter',
url: 'http://hl7.org/fhir/SearchParameter/Person-relatedperson',
name: 'relatedperson',
type: 'reference',
Expand All @@ -47,11 +53,12 @@ describe('compile', () => {
target: ['RelatedPerson'],
},
]);
expect(compiled).toMatchSnapshot();
await expect(compiled).resolves.toMatchSnapshot();
});
test(`as - (ConceptMap.source as uri)`, () => {
test(`as - (ConceptMap.source as uri)`, async () => {
const compiled = compile([
{
resourceType: 'SearchParameter',
url: 'http://hl7.org/fhir/SearchParameter/ConceptMap-source-uri',
name: 'source-uri',
type: 'reference',
Expand All @@ -61,11 +68,12 @@ describe('compile', () => {
target: ['ValueSet'],
},
]);
expect(compiled).toMatchSnapshot();
await expect(compiled).resolves.toMatchSnapshot();
});
test(`OR operator - CapabilityStatement.title | CodeSystem.title | ConceptMap.title | ImplementationGuide.title | MessageDefinition.title | OperationDefinition.title | StructureDefinition.title | StructureMap.title | TerminologyCapabilities.title | ValueSet.title`, () => {
test(`OR operator - CapabilityStatement.title | CodeSystem.title | ConceptMap.title | ImplementationGuide.title | MessageDefinition.title | OperationDefinition.title | StructureDefinition.title | StructureMap.title | TerminologyCapabilities.title | ValueSet.title`, async () => {
const compiled = compile([
{
resourceType: 'SearchParameter',
url: 'http://hl7.org/fhir/SearchParameter/conformance-title',
name: 'title',
type: 'string',
Expand All @@ -86,11 +94,12 @@ describe('compile', () => {
'CapabilityStatement.title | CodeSystem.title | ConceptMap.title | ImplementationGuide.title | MessageDefinition.title | OperationDefinition.title | StructureDefinition.title | StructureMap.title | TerminologyCapabilities.title | ValueSet.title',
},
]);
expect(compiled).toMatchSnapshot();
await expect(compiled).resolves.toMatchSnapshot();
});
test(`OR operator with same base resource - (ExampleScenario.useContext.value as Quantity) | (ExampleScenario.useContext.value as Range)`, () => {
test(`OR operator with same base resource - (ExampleScenario.useContext.value as Quantity) | (ExampleScenario.useContext.value as Range)`, async () => {
const compiled = compile([
{
resourceType: 'SearchParameter',
url: 'http://hl7.org/fhir/SearchParameter/ExampleScenario-context-quantity',
name: 'context-quantity',
type: 'quantity',
Expand All @@ -100,6 +109,30 @@ describe('compile', () => {
'(ExampleScenario.useContext.value as Quantity) | (ExampleScenario.useContext.value as Range)',
},
]);
expect(compiled).toMatchSnapshot();
await expect(compiled).resolves.toMatchSnapshot();
});

test(`Invalid input`, async () => {
const compiled = compile([
{
foo: 'bar',
},
]);
await expect(compiled).rejects.toThrowError();
});

test(`unparsable FHIRPath expression`, async () => {
const compiled = compile([
{
resourceType: 'SearchParameter',
url: 'http://hl7.org/fhir/SearchParameter/test',
name: 'test',
type: 'token',
description: 'test',
base: ['Patient'],
expression: 'some random FHIRPath expression that cannot be parsed',
},
]);
await expect(compiled).rejects.toThrowError();
});
});
Loading

0 comments on commit e0024a4

Please sign in to comment.