Skip to content

Commit

Permalink
migrate to standard fhirpath
Browse files Browse the repository at this point in the history
  • Loading branch information
niquola committed Jan 15, 2024
1 parent 84a7d14 commit bb535df
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
40 changes: 24 additions & 16 deletions sof-js/src/path.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
12 changes: 6 additions & 6 deletions sof-js/tests/2_fhirpath.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -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},
Expand Down
2 changes: 1 addition & 1 deletion sof-js/tests/test_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down

0 comments on commit bb535df

Please sign in to comment.