From bb535df655c3cd940017644fdef893114a58efbd Mon Sep 17 00:00:00 2001 From: niquola Date: Tue, 16 Jan 2024 00:21:47 +0200 Subject: [PATCH] migrate to standard fhirpath --- sof-js/src/path.js | 40 ++++++++++++++++++++------------- sof-js/tests/2_fhirpath.test.js | 12 +++++----- sof-js/tests/test_helpers.js | 2 +- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/sof-js/src/path.js b/sof-js/src/path.js index 5355c01..1d04aee 100644 --- a/sof-js/src/path.js +++ b/sof-js/src/path.js @@ -1,36 +1,36 @@ import { default as fhirpath } from 'fhirpath' -const identity = (v) => [v] +const identity = (ctx, v) => [v] function getResourceKey(nodes, resource) { - return nodes.flatMap(({data: node }) => { + return nodes.flatMap((node) => { const type = node.resourceType const key = `${node.resourceType}/${node.id}` return !resource || resource === tmpe ? [key] : [] }) } -function getReferenceKey(nodes, { name: resource } = { name: undefined }) { - return nodes.flatMap(({ data: node }) => { +function getReferenceKey(nodes, opts) { + let resource = opts?.name; + return nodes.flatMap((node) => { const parts = node.reference.replaceAll('//', '').split('/_history')[0].split('/') - const type = parts.slice(-2)[0] - const key = parts.slice(-2).join('/') - return !resource || resource === type ? [key] : [] + const type = parts[parts.length - 2]; + const key = parts[parts.length - 1]; + if(!resource) { + return [key]; + } else if(resource && resource == type) { + return [key]; + } else { + return [null] + } }) } -function ofType(nodes, a1, a2, a3) { +function ofType(ctx, nodes, a1, a2, a3) { console.log('of type nodes', nodes, a1, a2, a3) return 'ups' } -let fhirpath_options = { - userInvocationTable: { - getResourceKey: { fn: getResourceKey, arity: { 0: [], 1: ['TypeSpecifier'] } }, - getReferenceKey: { fn: getReferenceKey, arity: { 0: [], 1: ['TypeSpecifier'] } }, - identity: { fn: (nodes) => nodes, arity: { 0: [] } }, - } -} function rewrite_path(path) { const ofTypeRegex = /\.ofType\(([^)]+)\)/g @@ -46,6 +46,14 @@ function rewrite_path(path) { } +let fhirpath_options = { + userInvocationTable: { + getResourceKey: { fn: getResourceKey, arity: { 0: [], 1: ['TypeSpecifier'] } }, + getReferenceKey: { fn: getReferenceKey, arity: { 0: [], 1: ['TypeSpecifier'] } }, + identity: { fn: (nodes) => nodes, arity: { 0: [] } }, + } +} + export function fhirpath_evaluate(data, path) { - return fhirpath.evaluate(data, rewrite_path(path), fhirpath_options); + return fhirpath.evaluate(data, rewrite_path(path), {}, null, fhirpath_options); } diff --git a/sof-js/tests/2_fhirpath.test.js b/sof-js/tests/2_fhirpath.test.js index bbc57af..c6ee47a 100644 --- a/sof-js/tests/2_fhirpath.test.js +++ b/sof-js/tests/2_fhirpath.test.js @@ -160,8 +160,8 @@ describe("fhirpath", () => { [{name: 'id', path: "id"}, {name: 'ref', path: "managingOrganization.getReferenceKey()"}]}]}, expected: - [{id: 'pt1', ref: 'Organization/o1'}, - {id: 'pt2', ref: 'Organization/o2'}, + [{id: 'pt1', ref: 'o1'}, + {id: 'pt2', ref: 'o2'}, {id: 'pt3', ref: null}]}) add_test({ @@ -170,10 +170,10 @@ describe("fhirpath", () => { {select: [ {column: [{name: 'id', path: "id"}, - {name: 'ref', path: "managingOrganization.getReferenceKey('Organization')"}]}]}, + {name: 'ref', path: "managingOrganization.getReferenceKey(Organization)"}]}]}, expected: - [{id: 'pt1', ref: 'Organization/o1'}, - {id: 'pt2', ref: 'Organization/o2'}, + [{id: 'pt1', ref: 'o1'}, + {id: 'pt2', ref: 'o2'}, {id: 'pt3', ref: null}]}) add_test({ @@ -182,7 +182,7 @@ describe("fhirpath", () => { {select: [ {column: [{name: 'id', path: "id"}, - {name: 'ref', path: "managingOrganization.getReferenceKey('Encounter')"}]}]}, + {name: 'ref', path: "managingOrganization.getReferenceKey(Encounter)"}]}]}, expected: [{id: 'pt1', ref: null}, {id: 'pt2', ref: null}, diff --git a/sof-js/tests/test_helpers.js b/sof-js/tests/test_helpers.js index 279ceaa..03e6d4e 100644 --- a/sof-js/tests/test_helpers.js +++ b/sof-js/tests/test_helpers.js @@ -36,7 +36,7 @@ export function end_case(name, desc, resources) { test('finalize', ()=>{ // console.log(JSON.stringify(test_case, null, " ")); let file_name = '../sof-tests/' + test_case.title + '.json' - fs.writeFileSync(file_name, JSON.stringify(test_case, null, " ")) + // fs.writeFileSync(file_name, JSON.stringify(test_case, null, " ")) console.log('write: ', file_name) }) }