From 5d20243373b9a1cb17ac10ed5f663e1937645dac Mon Sep 17 00:00:00 2001 From: Vitaly Kravtsov Date: Fri, 4 Oct 2024 22:33:56 +0600 Subject: [PATCH] fix: add Bundle, Parameters and Binary to resources --- dev/user.clj | 113 +++++++++++++++++++++++++--------------- src/aidbox_sdk/core.clj | 12 +++-- src/aidbox_sdk/fhir.clj | 4 ++ 3 files changed, 82 insertions(+), 47 deletions(-) diff --git a/dev/user.clj b/dev/user.clj index d3f994e..8b121e2 100644 --- a/dev/user.clj +++ b/dev/user.clj @@ -1,31 +1,31 @@ (ns user (:require + [aidbox-sdk.converter :as converter] [aidbox-sdk.core :as sdk] [aidbox-sdk.fhir :as fhir] - [aidbox-sdk.converter :as converter] - [aidbox-sdk.models :as models] - [aidbox-sdk.fixtures.schemas :as fixtures] - [aidbox-sdk.generator :as gen] - [aidbox-sdk.generator.python :as gen.python] [aidbox-sdk.schema :as import] - [aidbox-sdk.cli :as cli] - [malli.core :as m] - [clojure.spec.alpha :as s] - [clojure.data] - [clojure.java.io :as io] - [aidbox-sdk.generator.typescript :as gen.typescript] - [clojure.data.json :as json])) + [clojure.walk :as walk] + [meander.epsilon :as m] + [aidbox-sdk.generator.helpers :as helpers] + [aidbox-sdk.models :as models] + [clojure.set :as set] + [clojure.string :as str] + [clojure.data])) (defonce aidbox-schemas (atom nil)) -(defn load-aidbox-schemas [] - (reset! aidbox-schemas - (import/retrieve - (import/resource "http://localhost:3333/r4/fhir-packages") {:auth "YmFzaWM6c2VjcmV0"})) +(defn load-aidbox-schemas [from] + (let [from (case from + :r4 "http://localhost:3333/r4/fhir-packages" + :r5 "http://localhost:3333/r5/fhir-packages" + :aidbox "http://localhost:8765/api/sdk/fhir-packages")] + (reset! aidbox-schemas + (import/retrieve + (import/resource from) {:auth "YmFzaWM6c2VjcmV0"}))) nil) (comment - (load-aidbox-schemas) + (load-aidbox-schemas :r4) (defonce r4-schemas (import/retrieve (import/resource "resources/r4") {})) (defonce r4b-schemas (import/retrieve (import/resource "resources/r4b") {})) @@ -37,9 +37,6 @@ (defn exclude-keys [m keys] (apply dissoc m keys)) -(defn filter-by-url [url schemas] - (filter #(= url (:url %)) schemas)) - (comment (kinds r4-schemas) @@ -61,47 +58,78 @@ ;; "ValueSet" ;; "StructureDefinition") - (->> @aidbox-schemas - (filter #(= "Patient" (:id %))) - (converter/convert)) - - (converter/convert-search-params - (->> @aidbox-schemas - (filter fhir/search-parameter?) - (remove fhir/search-parameter-from-extension?)) - (->> @aidbox-schemas - (filter fhir/fhir-schema?))) - (let [all-schemas @aidbox-schemas base-type? (every-pred fhir/fhir-schema? fhir/base-type?) datatype? (every-pred fhir/fhir-schema? fhir/datatype? (complement fhir/primitive-type?)) - domain-resource? (every-pred fhir/fhir-schema? fhir/domain-resource?) + resource? (every-pred fhir/fhir-schema? fhir/resource?) constraint? (every-pred fhir/fhir-schema? fhir/constraint? (complement fhir/extension?)) search-param? (every-pred fhir/search-parameter? (complement fhir/search-parameter-from-extension?)) fhir-schemas (filter fhir/fhir-schema? all-schemas) base-schemas (filter base-type? all-schemas) datatype-schemas (filter datatype? all-schemas) - resource-schemas (filter #(or (domain-resource? %) - (fhir/backbone-element? %)) all-schemas) + resource-schemas (filter #(and + (fhir/fhir-schema? %) + (not (fhir/base-type? %)) + (not (fhir/datatype? %)) + (or (fhir/resource-type? %) + (fhir/domain-resource? %) + (fhir/backbone-element? %))) + all-schemas) constraint-schemas (filter constraint? all-schemas) - search-param-schemas [filter search-param? all-schemas] + search-param-schemas (filter search-param? all-schemas) ir-schemas (converter/convert fhir-schemas) base-ir-schemas (converter/convert base-schemas) datatype-ir-schemas (converter/convert datatype-schemas) resource-ir-schemas (converter/convert resource-schemas) search-param-ir-schemas (converter/convert-search-params search-param-schemas - fhir-schemas) - constraint-ir-schemas (converter/convert-constraints constraint-schemas - (remove fhir/constraint? ir-schemas))] + fhir-schemas)] + +;; (def css constraint-schemas) + + ;; (def cssi constraint-ir-schemas) - (map models/validate-fhir-schema fhir-schemas) + ;; (->> (remove fhir/constraint? ir-schemas) + ;; (filter #(contains? (set (map :base css)) %)) + ;; helpers/vector->map) - #_(filter-by-url "http://hl7.org/fhir/StructureDefinition/Resource" fhir-schemas)) + ;; (reduce (fn [acc v] (assoc acc (:url v) v)) css) -; + ;; (let [name->map (reduce (fn [acc v] (assoc acc (:url v) v)) css)] + ;; (reduce (fn [graph {:keys [url base]}] + ;; (if (and base (contains? name->map base)) + ;; (update graph url conj base) + ;; graph)) + ;; (zipmap (map :url css) (repeat #{})) + ;; css)) + + ;; (fhir/filter-by-url "http://hl7.org/fhir/StructureDefinition/bmi" ir-schemas) + + ;; (let [base-schemas (->> (remove fhir/constraint? ir-schemas) + ;; (filter #(contains? (set (map :base constraint-schemas)) (:url %))) + ;; helpers/vector->map)] + ;; (reduce (fn [acc constraint] + ;; (let [base-schema (or (get base-schemas (:base constraint)) + ;; (get acc (:url constraint)))] + + ;; (assoc acc + ;; (:url constraint) + ;; (converter/apply-constraint base-schema constraint)))) + + ;; {} (converter/sort-by-base constraint-schemas))) + + ;; (converter/apply-constraints (remove fhir/constraint? ir-schemas) constraint-schemas) + + (->> all-schemas + (filter #(or (fhir/domain-resource? %) + (fhir/backbone-element? %))) + (filter fhir/backbone-element?)) + ;let + ) + +;comment ) (comment @@ -117,7 +145,6 @@ ; ) - (comment (sdk/generate! :dotnet "http://localhost:3333/r4/fhir-packages" @@ -137,5 +164,5 @@ :auth-token "YmFzaWM6c2VjcmV0" :exit (fn [_] nil)}) -; + ; ) diff --git a/src/aidbox_sdk/core.clj b/src/aidbox_sdk/core.clj index 54c01ee..fdc9131 100644 --- a/src/aidbox_sdk/core.clj +++ b/src/aidbox_sdk/core.clj @@ -62,18 +62,22 @@ base-type? (every-pred fhir/fhir-schema? fhir/base-type?) datatype? (every-pred fhir/fhir-schema? fhir/datatype? (complement fhir/primitive-type?)) - domain-resource? (every-pred fhir/fhir-schema? fhir/domain-resource?) constraint? (every-pred fhir/fhir-schema? fhir/constraint? (complement fhir/extension?)) search-param? (every-pred fhir/search-parameter? (complement fhir/search-parameter-from-extension?)) fhir-schemas (filter fhir/fhir-schema? all-schemas) base-schemas (filter base-type? all-schemas) datatype-schemas (filter datatype? all-schemas) - resource-schemas (filter #(or (domain-resource? %) - (fhir/backbone-element? %)) - all-schemas) constraint-schemas (filter constraint? all-schemas) search-param-schemas (filter search-param? all-schemas) + resource-schemas (filter #(and + (fhir/fhir-schema? %) + (not (fhir/base-type? %)) + (not (fhir/datatype? %)) + (or (fhir/resource-type? %) + (fhir/domain-resource? %) + (fhir/backbone-element? %))) + all-schemas) ir-schemas (converter/convert fhir-schemas) base-ir-schemas (converter/convert base-schemas) diff --git a/src/aidbox_sdk/fhir.clj b/src/aidbox_sdk/fhir.clj index 44cdc9d..5722519 100644 --- a/src/aidbox_sdk/fhir.clj +++ b/src/aidbox_sdk/fhir.clj @@ -142,6 +142,10 @@ [schema] (= (:base schema) "http://hl7.org/fhir/StructureDefinition/DomainResource")) +(defn resource-type? + [schema] + (= (:base schema) "http://hl7.org/fhir/StructureDefinition/Resource")) + (defn search-parameter-from-extension? [search-parameter] (str/includes? (:id search-parameter) "-extensions-"))