From edc7995e51e18d45332c8c9891adceaa47924dc6 Mon Sep 17 00:00:00 2001 From: Dmitry Egorov Date: Wed, 22 Dec 2021 18:30:03 +0300 Subject: [PATCH] Refactor preprocess-slice-by-discriminator Co-authored-by: @yurii_makhotkin --- src/zen/fhir/loader.clj | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/src/zen/fhir/loader.clj b/src/zen/fhir/loader.clj index 29eb639..b0de171 100644 --- a/src/zen/fhir/loader.clj +++ b/src/zen/fhir/loader.clj @@ -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))) @@ -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))] @@ -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 {}