Skip to content

Commit

Permalink
add fhir_extract_as_metas_token
Browse files Browse the repository at this point in the history
  • Loading branch information
maksym committed Nov 24, 2016
1 parent 7610640 commit 0dcfe8e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/fhir/search_token.litcoffee
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,66 @@ PostgreSQL implementation is based on arrays support - http://www.postgresql.org
returns: 'text[]'
immutable: true
extract_value = (resource, metas)->
for meta in metas
value = xpath.get_in(resource, [meta.path])
if value && (value.length > 0)
return {
value: value
path: meta.path
elementType: meta.elementType
}
null
exports.fhir_extract_as_metas_token = (plv8, resource, metas)->
res = []
data = extract_value(resource, metas)
if data
if data.elementType == 'boolean'
res = for str in data.value
if str.toString() == 'false'
'false'
else
'true'
else if data.elementType == 'dateTime' or data.elementType == 'date'
res = if data.value.length > 0 then ['true', 'false'] else []
else if data.elementType == 'code' || data.elementType == 'string' || data.elementType == 'uri'
res = (str.toString().toLowerCase() for str in data.value)
else if data.elementType == 'Identifier' or data.elementType == 'ContactPoint'
for coding in data.value
res.push(coding.value.toString().toLowerCase()) if coding.value
res.push("#{coding.system}|#{coding.value}".toLowerCase())
else if data.elementType == 'Coding'
for coding in data.value
res.push(coding.code.toString().toLowerCase()) if coding.code
res.push("#{coding.system}|#{coding.code}".toLowerCase())
else if data.elementType == 'Quantity'
for quant in data.value
res.push(quant.code.toString().toLowerCase()) if quant.code
res.push(quant.unit.toString().toLowerCase()) if quant.unit
res.push("#{quant.system}|#{quant.code}".toLowerCase())
res.push("#{quant.system}|#{quant.unit}".toLowerCase())
else if data.elementType == 'CodeableConcept'
for concept in data.value
for coding in (concept.coding || [])
res.push(coding.code.toString().toLowerCase()) if coding.code
res.push("#{coding.system}|#{coding.code}".toLowerCase())
else if data.elementType == 'Reference'
for ref in data.value
res.push(ref.reference)
else
throw new Error("fhir_extract_as_token: Not implemented for #{data.elementType}")
if res.length == 0
['$NULL']
else
res
exports.fhir_extract_as_metas_token.plv8_signature =
arguments: ['json', 'json']
returns: 'text[]'
immutable: true
exports.fhir_sort_as_token = (plv8, resource, path, element_type)->
data = xpath.get_in(resource, [path])[0]
return null unless data
Expand Down
8 changes: 7 additions & 1 deletion test/fhir/search_token_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ resource =
{system: 'ssn', value: '1'}
{system: 'mrn', value: '2'}
]
value:
value:
value: 10
unit: "mg"
system: "http://unitsofmeasure.org"
Expand Down Expand Up @@ -90,6 +90,12 @@ describe "extract_as_token", ->
specs.forEach (spec)->
it JSON.stringify(spec.path), ->
res = search.fhir_extract_as_token({}, resource, spec.path, spec.elementType)
metasRes = search.fhir_extract_as_metas_token({}, resource,
[
{path: ['Resource', 'unknownPath'], elementType: spec.elementType},
{path: spec.path, elementType: spec.elementType}
])
assert.deepEqual(res, spec.result)
assert.deepEqual(metasRes, spec.result)
order = search.fhir_sort_as_token({}, resource, spec.path, spec.elementType)
assert.deepEqual(order, spec.order)

0 comments on commit 0dcfe8e

Please sign in to comment.