Skip to content

Commit

Permalink
Add Hack to allow FHIRPath As Type Specifier on Collections
Browse files Browse the repository at this point in the history
Closes: #1568
  • Loading branch information
alexanderkiel committed Aug 2, 2024
1 parent 91c29e8 commit e783464
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
32 changes: 32 additions & 0 deletions modules/db/test/blaze/db/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2239,6 +2239,38 @@
count := 1
[0 :id] := "0"))))

(testing "Medication"
(testing "with one ingredient reference"
(with-system-data [{:blaze.db/keys [node]} config]
[[[:put {:fhir/type :fhir/Medication :id "0"
:ingredient
[{:fhir/type :fhir.Medication/ingredient
:item #fhir/Reference{:reference "Substance/0"}}]}]
[:put {:fhir/type :fhir/Substance :id "0"}]
[:put {:fhir/type :fhir/Medication :id "1"}]]]

(testing "ingredient"
(given (pull-type-query node "Medication" [["ingredient" "Substance/0"]])
count := 1
[0 :id] := "0"))))

(testing "with two ingredient references"
(with-system-data [{:blaze.db/keys [node]} config]
[[[:put {:fhir/type :fhir/Medication :id "0"
:ingredient
[{:fhir/type :fhir.Medication/ingredient
:item #fhir/Reference{:reference "Substance/0"}}
{:fhir/type :fhir.Medication/ingredient
:item #fhir/Reference{:reference "Substance/1"}}]}]
[:put {:fhir/type :fhir/Substance :id "0"}]
[:put {:fhir/type :fhir/Substance :id "1"}]
[:put {:fhir/type :fhir/Medication :id "1"}]]]

(testing "ingredient"
(given (pull-type-query node "Medication" [["ingredient" "Substance/0"]])
count := 1
[0 :id] := "0")))))

(testing "Observation"
(with-system-data [{:blaze.db/keys [node]} config]
[[[:put {:fhir/type :fhir/Observation :id "id-0"
Expand Down
16 changes: 9 additions & 7 deletions modules/fhir-path/src/blaze/fhir_path.clj
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,21 @@

(throw-anom (ba/incorrect (is-type-specifier-msg coll)))))))

(defn- as-type-specifier-msg [coll]
(format "as type specifier with more than one item at the left side `%s`"
(pr-str coll)))

(deftype AsTypeExpression [expression type-specifier]
Expression
(-eval [_ context coll]
(let [coll (-eval expression context coll)]
(case (coll/count coll)
0 coll
0 []

1 (if (identical? type-specifier (fhir-spec/fhir-type (coll/nth coll 0)))
coll
[])

(throw-anom (ba/incorrect (as-type-specifier-msg coll)))))))
;; HACK: normally multiple items should throw an error. However in R4 many
;; FHIRPath expressions of search parameters use the as type specifier wrongly.
;; Please remove that hack for R5.
(filterv #(identical? type-specifier (fhir-spec/fhir-type %)) coll)))))

(deftype UnionExpression [e1 e2]
Expression
Expand Down Expand Up @@ -260,7 +259,10 @@
coll
[])

(throw-anom (ba/incorrect (as-type-specifier-msg coll))))))
;; HACK: normally multiple items should throw an error. However in R4 many
;; FHIRPath expressions of search parameters use the as type specifier wrongly.
;; Please remove that hack for R5.
(filterv #(identical? type-specifier (fhir-spec/fhir-type %)) coll))))

(deftype OfTypeFunctionExpression [type-specifier]
Expression
Expand Down
26 changes: 17 additions & 9 deletions modules/fhir-path/test/blaze/fhir_path_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
[taoensso.timbre :as log]))

(st/instrument)
(log/set-level! :trace)
(log/set-min-level! :trace)

(test/use-fixtures :each tu/fixture)

Expand Down Expand Up @@ -506,15 +506,19 @@
{:fhir/type :fhir/Patient :id "foo"})
count := 0))

(testing "multiple item returns an error"
;; HACK: normally multiple items should throw an error. However in R4 many
;; FHIRPath expressions of search parameters use the as type specifier wrongly.
;; Please remove that hack for R5.
(testing "multiple items are filtered"
(given (eval
"Patient.identifier as string"
"Patient.identifier as Identifier"
{:fhir/type :fhir/Patient :id "id-162953"
:identifier
[#fhir/Identifier{:value "value-163922"}
#fhir/Identifier{:value "value-163928"}]})
::anom/category := ::anom/incorrect
::anom/message := "as type specifier with more than one item at the left side `[#fhir/Identifier{:value \"value-163922\"} #fhir/Identifier{:value \"value-163928\"}]`")))
count := 2
[0] := #fhir/Identifier{:value "value-163922"}
[1] := #fhir/Identifier{:value "value-163928"})))

;; 6.3.4 as(type : type specifier)
(deftest as-function-test
Expand All @@ -538,15 +542,19 @@
{:fhir/type :fhir/Patient :id "foo"})
count := 0))

(testing "multiple item returns an error"
;; HACK: normally multiple items should throw an error. However in R4 many
;; FHIRPath expressions of search parameters use the as type specifier wrongly.
;; Please remove that hack for R5.
(testing "multiple items are filtered"
(given (eval
"Patient.identifier.as(string)"
"Patient.identifier.as(Identifier)"
{:fhir/type :fhir/Patient :id "id-162953"
:identifier
[#fhir/Identifier{:value "value-163922"}
#fhir/Identifier{:value "value-163928"}]})
::anom/category := ::anom/incorrect
::anom/message := "as type specifier with more than one item at the left side `[#fhir/Identifier{:value \"value-163922\"} #fhir/Identifier{:value \"value-163928\"}]`")))
count := 2
[0] := #fhir/Identifier{:value "value-163922"}
[1] := #fhir/Identifier{:value "value-163928"})))

;; 6.5. Boolean logic

Expand Down

0 comments on commit e783464

Please sign in to comment.