Skip to content

Commit

Permalink
Merge pull request #10 from vadi2/add-r4-support
Browse files Browse the repository at this point in the history
Add R4 support
  • Loading branch information
vadi2 authored Dec 30, 2022
2 parents 397eb8d + d692b8c commit 848b2ca
Show file tree
Hide file tree
Showing 33 changed files with 60,877 additions and 55 deletions.
1 change: 1 addition & 0 deletions .devcontainer/scripts/init-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

set -e

sudo luarocks install argparse
sudo luarocks install lua-cjson
sudo luarocks install rapidjson
sudo luarocks install lunajson
Expand Down
26 changes: 26 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug Lua Interpreter",
"type": "lua-local",
"request": "launch",
"program": {
"lua": "lua",
"file": "${file}"
}
},
{
"name": "Debug Custom Lua Environment",
"type": "lua-local",
"request": "launch",
"program": {
"command": "command"
},
"args": []
}
]
}
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,27 @@ to_json("/path/to/file", {file = true})
to_json("xml content", {pretty = true})
-- convert an XML file given as a location to JSON and prettyprint it
to_json("/path/to/file", {file = true, pretty = true})
-- supported versions are 'R4', 'STU3', and 'auto' (default)
-- 'auto' will try all FHIR versions starting from R4 and going down
to_json("xml content", {fhirversion = "R4"})
to_json("xml content", {fhirversion = "STU3"})
to_json("xml content", {fhirversion = "auto"})

-- convert given JSON content as a string to XML
to_xml("json content")
-- convert an JSON file given as a location to XML
to_xml("/path/to/file", {file = true})

-- get the FHIR definition of an element
-- convert JSON content as a string to XML and prettyprint it
to_xml("json content", {pretty = true})
-- convert an JSON file given as a location to XML and prettyprint it
to_xml("/path/to/file", {file = true, pretty = true})
-- supported versions are 'R4', 'STU3', and 'auto' (default)
-- 'auto' will try all FHIR versions starting from R4 and going down
to_xml("json content", {fhirversion = "R4"})
to_xml("json content", {fhirversion = "STU3"})
to_xml("json content", {fhirversion = "auto"})

-- get the FHIR definition of an element. Defaults to STU3
get_fhir_definition('Patient', 'animal', 'species')
-- returns a table with metadata about the element. Do not modify this table. Keys are:
-- _min (int): minimum cardinality
Expand All @@ -39,6 +53,10 @@ get_fhir_definition('Patient', 'animal', 'species')
-- _type (string): FHIR element type. Possible values are: Address, Age, Annotation, Attachment, BackboneElement, CodeableConcept, Coding, ContactDetail, ContactPoint, Contributor, Count, DataRequirement, Distance, DomainResource, Duration, Element, ElementDefinition, Extension, HumanName, Identifier, Meta, Money, Narrative, ParameterDefinition, Period, Quantity, Range, Ratio, Reference, RelatedResource, Resource, SampledData, Signature, Timing, TriggerDefinition, UsageContext, base64Binary, boolean, code, date, dateTime, decimal, id, instant, integer, markdown, oid, positiveInt, string, time, unsignedInt, uri, xhtml
-- [1]: if present, this is a link to the base type of the resource
-- _derivations: if present, this is a map of resources that extend on this resource

-- last argument can be either 'R4' or 'STU3' to specify version. Defaults to STU3
get_fhir_definition('Patient', 'contact', 'relationship', 'R4')
get_fhir_definition('Patient', 'animal', 'species', 'STU3')
```

### Examples
Expand Down
65 changes: 65 additions & 0 deletions spec/R4/auto_versioning_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
--[[
FHIR Formats
Copyright (C) 2016 Vadim Peretokin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]

package.path = "src/?.lua;"..package.path
local in_fhir_json = require("fhirformats").to_json
local in_fhir_xml = require("fhirformats").to_xml
local cjson = require("cjson")
local xml = require("xml")
local inspect = require("inspect")

describe("auto-version xml to json", function()
local positive_example, negative_example, immunization_example,
positive_example_data, immunization_example_data

setup(function()
io.input("spec/R4/stu3-immunization.json")
positive_example_data = io.read("*a")
immunization_example_data = in_fhir_json("spec/R4/stu3-immunization.xml", {file = true, fhirversion = "auto"})

-- for same div data test
assert:set_parameter("TableFormatLevel", -1)
end)

before_each(function()
positive_example = cjson.decode(positive_example_data)
immunization_example = cjson.decode(immunization_example_data)
end)

it("should have the same non-div data", function()
assert.same(positive_example, immunization_example)
end)
end)

describe("auto-version json to xml", function()
local positive_example, negative_example, immunization_example,
positive_example_data, immunization_example_data

setup(function()
io.input("spec/R4/stu3-immunization.xml")
positive_example_data = io.read("*a")
immunization_example_data = in_fhir_xml("spec/R4/stu3-immunization.json", {file = true, fhirversion = "auto"})

-- for same div data test
assert:set_parameter("TableFormatLevel", -1)
end)

it("should have the same data, order included", function()
assert.same(positive_example_data, immunization_example_data)
end)
end)
27 changes: 27 additions & 0 deletions spec/R4/deviceusestatement-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"resourceType": "DeviceUseStatement",
"id": "example",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><p><b>Generated Narrative with Details</b></p><p><b>id</b>: example</p><p><b>identifier</b>: 51ebb7a9-4e3a-4360-9a05-0cc2d869086f</p><p><b>status</b>: active</p><p><b>subject</b>: <a>Patient/example</a></p><p><b>device</b>: <a>Device/example</a></p><p><b>reasonReference</b>: <a>Appendectomy (surgery)</a></p></div>"
},
"identifier": [
{
"system": "http:goodhealth.org/identifiers",
"value": "51ebb7a9-4e3a-4360-9a05-0cc2d869086f"
}
],
"status": "active",
"subject": {
"reference": "Patient/example"
},
"device": {
"reference": "Device/example"
},
"reasonReference": [
{
"reference": "Procedure/example",
"display": "Appendectomy (surgery)"
}
]
}
18 changes: 18 additions & 0 deletions spec/R4/deviceusestatement-example.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?><DeviceUseStatement xmlns="http://hl7.org/fhir">
<id value="example"/>
<text><status value="generated"/><div xmlns="http://www.w3.org/1999/xhtml"><p><b>Generated Narrative with Details</b></p><p><b>id</b>: example</p><p><b>identifier</b>: 51ebb7a9-4e3a-4360-9a05-0cc2d869086f</p><p><b>status</b>: active</p><p><b>subject</b>: <a>Patient/example</a></p><p><b>device</b>: <a>Device/example</a></p><p><b>reasonReference</b>: <a>Appendectomy (surgery)</a></p></div></text><identifier>
<system value="http:goodhealth.org/identifiers"/>
<value value="51ebb7a9-4e3a-4360-9a05-0cc2d869086f"/>
</identifier>
<status value="active"/>
<subject>
<reference value="Patient/example"/>
</subject>
<device>
<reference value="Device/example"/>
</device>
<reasonReference>
<reference value="Procedure/example"/>
<display value="Appendectomy (surgery)"/>
</reasonReference>
</DeviceUseStatement>
81 changes: 81 additions & 0 deletions spec/R4/deviceusestatement_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
--[[
FHIR Formats
Copyright (C) 2022 Vadim Peretokin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]

package.path = "src/?.lua;"..package.path
local in_fhir_json = require("fhirformats").to_json
local in_fhir_xml = require("fhirformats").to_xml
local cjson = require("cjson")
local xml = require("xml")
local inspect = require("inspect")

describe("xml to json", function()
local positive_example, negative_example, deviceusestatement_example,
positive_example_data, deviceusestatement_example_data

setup(function()
io.input("spec/R4/deviceusestatement-example.json")
positive_example_data = io.read("*a")
deviceusestatement_example_data = in_fhir_json("spec/R4/deviceusestatement-example.xml",
{file = true, fhirversion = "R4"})

-- for same div data test
assert:set_parameter("TableFormatLevel", -1)
end)

before_each(function()
positive_example = cjson.decode(positive_example_data)
deviceusestatement_example = cjson.decode(deviceusestatement_example_data)
end)

it("should have the same non-div data", function()
-- cut out the div's, since the whitespace doesn't matter as much in xml
positive_example.text.div = nil
deviceusestatement_example.text.div = nil
assert.same(positive_example, deviceusestatement_example)
end)

it("should have xml-comparable div data", function()
local positive_example_div = xml.load(positive_example.text.div)
local deviceusestatement_example_div = xml.load(deviceusestatement_example.text.div)
--print(inspect(positive_example_div))
--print(inspect(deviceusestatement_example_div))
assert.same(positive_example_div, deviceusestatement_example_div)
end)
end)

describe("json to xml", function()
local positive_example, negative_example, deviceusestatement_example,
positive_example_data, deviceusestatement_example_data

setup(function()
io.input("spec/R4/deviceusestatement-example.xml")
positive_example_data = io.read("*a")
deviceusestatement_example_data = in_fhir_xml("spec/R4/deviceusestatement-example.json",
{file = true, fhirversion = "R4"})

-- for same div data test
assert:set_parameter("TableFormatLevel", -1)
end)

it("should have the same data", function()
-- convert it down to JSON since order of elements doesn't matter in JSON, while it does in XML
assert.same(
cjson.decode(in_fhir_json(positive_example_data, {fhirversion = "R4"})),
cjson.decode(in_fhir_json(deviceusestatement_example_data, {fhirversion = "R4"})))
end)
end)
Loading

0 comments on commit 848b2ca

Please sign in to comment.