Skip to content

Commit

Permalink
Refactor preprocess-slice-by-discriminator
Browse files Browse the repository at this point in the history
Co-authored-by: @yurii_makhotkin <[email protected]>
  • Loading branch information
Dmitry Egorov and KGOH committed Dec 22, 2021
1 parent 11495c5 commit edc7995
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions src/zen/fhir/loader.clj
Original file line number Diff line number Diff line change
Expand Up @@ -387,28 +387,7 @@
pattern))


(defmethod preprocess-slices-by-discriminator :pattern [slices discriminator]
(let [rich-path (->> (rich-parse-path-full (:path discriminator))
(remove (fn [path-el]
(assert (= :key (:type path-el)))
(= "$this" (:key path-el)))))
inter-path (build-path rich-path)
path (mapv (comp keyword :key) rich-path)]
(sp/transform [sp/MAP-VALS]
(fn [v]
(if-let [[pattern-k pattern] (some-> (get-in v inter-path) (utils/poly-find :pattern))]
(let [match (cond->> (pattern->zen-match pattern)
(seq path)
(assoc-in {} path))]
(-> (if (seq path)
(update-in v path dissoc pattern-k)
(dissoc v pattern-k))
(assoc :match match)))
v))
slices)))


(defmethod preprocess-slices-by-discriminator :value [slices discriminator]
(defn slice-discriminator->match [slices discriminator d-type-key]
(let [rich-path (->> (rich-parse-path-full (:path discriminator))
(remove (fn [path-el]
(assert (= :key (:type path-el)))
Expand All @@ -417,7 +396,7 @@
path (mapv (comp keyword :key) rich-path)]
(sp/transform [sp/MAP-VALS]
(fn [v]
(if-let [[pattern-k pattern] (some-> (get-in v inter-path) (utils/poly-find :fixed))]
(if-let [[pattern-k pattern] (some-> (get-in v inter-path) (utils/poly-find d-type-key))]
(let [match (cond->> (pattern->zen-match pattern)
(seq path)
(assoc-in (:match v) path))]
Expand All @@ -428,6 +407,13 @@
v))
slices)))

(defmethod preprocess-slices-by-discriminator :pattern [slices discriminator]
(slice-discriminator->match slices discriminator :pattern))


(defmethod preprocess-slices-by-discriminator :value [slices discriminator]
(slice-discriminator->match slices discriminator :fixed))


(defn fix-slices-names [slices]
(into {}
Expand Down

0 comments on commit edc7995

Please sign in to comment.