Skip to content

Commit

Permalink
fix: generation typescript deps in modules for profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
krvital committed Oct 10, 2024
1 parent d6f53b9 commit 024b8e8
Show file tree
Hide file tree
Showing 11 changed files with 444 additions and 42 deletions.
3 changes: 2 additions & 1 deletion src/aidbox_sdk/converter.clj
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
:resource-name (url->resource-name (get schema :url))
:base-resource-name (when (get schema :base)
(url->resource-name (get schema :base)))
:fhir-version (get schema :fhir-version)
:package (get schema :package)
:url (get schema :url)
:type (get schema :type)
Expand Down Expand Up @@ -262,6 +263,7 @@
;; Convert main function
;;


(defn convert [schemas]
(->> schemas
(map resolve-element-references)
Expand Down Expand Up @@ -362,7 +364,6 @@
((fn [schema] (update schema :deps set/union #{"Meta"})))
((fn [schema] (assoc schema :resource-name (url->resource-name (:url constraint)))))))


(defn apply-constraint [base-schema constraint]
(-> base-schema
;; apply required
Expand Down
3 changes: 3 additions & 0 deletions src/aidbox_sdk/fhir.clj
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,6 @@
(filter #(= url (:url %)) schemas))

(def find-by-url (comp first filter-by-url))

(defn base-package? [schema]
(contains? #{"hl7.fhir.r4.core" "hl7.fhir.r4b.core" "hl7.fhir.r5.core"} (:package schema)))
39 changes: 19 additions & 20 deletions src/aidbox_sdk/generator/typescript.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
->camel-case]]
[aidbox-sdk.generator.utils :as u]
[clojure.java.io :as io]
[clojure.string :as str])
[clojure.string :as str]
[aidbox-sdk.fhir :as fhir])
(:import
[aidbox_sdk.generator CodeGenerator]))

(defn package->directory
"Generates directory name from package name.
(defn package->directory "Generates directory name from package name.
Example:
hl7.fhir.r4.core -> hl7-fhir-r4-core"
Expand Down Expand Up @@ -149,24 +149,23 @@
(str/replace path #"(\.ts)|[\.\/]" ""))

(defn generate-deps
"Takes a list of resource names and generates import declarations."
[deps]
(->> deps
(map (fn [{:keys [module members]}]
(if (seq members)
(format "import { %s } from \"%s\";" (str/join ", " members) module)
(format "import * as %s from \"%s\";" (path->name module) module))))
(str/join "\n")))
"Takes an IR schema generates import declarations."
[{:keys [deps package fhir-version] :as ir-schema}]
(let [relative-path (if (fhir/base-package? ir-schema)
"./"
(str "../" (package->directory (:fhir-version ir-schema)) "/"))]
(->> (:deps ir-schema)
(map class-name)
(map (fn [d] {:module (str relative-path d) :members [d]}))
(map (fn [{:keys [module members]}]
(if (seq members) (format "import { %s } from \"%s\";" (str/join ", " members) module) (format "import * as %s from \"%s\";" (path->name module) module))))
(str/join "\n"))))

(defn generate-module
[& {:keys [deps classes]
:or {classes []}}]
(->> (conj []
(->> deps
(map class-name)
(map (fn [d] {:module (str "./" d) :members [d]}))
generate-deps)

deps
classes)
(flatten)
(str/join "\n\n")))
Expand All @@ -178,22 +177,22 @@
(map (fn [ir-schema]
{:path (resource-file-path ir-schema)
:content (generate-module
:deps (:deps ir-schema)
:deps (generate-deps ir-schema)
:classes [(generate-class ir-schema (map generate-class (:backbone-elements ir-schema)))])})
ir-schemas)))

(generate-resource-module [_ ir-schema]
{:path (resource-file-path ir-schema)
:content (generate-module
{:deps (:deps ir-schema)
{:deps (generate-deps ir-schema)
:classes [(generate-class ir-schema
(map generate-class (:backbone-elements ir-schema)))]})})

(generate-search-params [_ ir-schemas]
(map (fn [ir-schema]
{:path (search-param-filepath ir-schema)
:content (generate-module
:deps (map #(format "%sSearchParameters" %) (:deps ir-schema))
:deps (generate-deps {:package "hl7.fhir.r4.core" :deps (map #(format "%sSearchParameters" %) (:deps ir-schema))})
:classes [(generate-class
{:name (format "%sSearchParameters" (:name ir-schema))
:base (when (:base ir-schema)
Expand All @@ -205,7 +204,7 @@
(map (fn [ir-schema]
{:path (resource-file-path ir-schema)
:content (generate-module
{:deps (:deps ir-schema)
{:deps (generate-deps ir-schema)
:classes [(generate-class ir-schema
(map generate-class (:backbone-elements ir-schema)))]})})
ir-schemas))
Expand Down
25 changes: 18 additions & 7 deletions src/aidbox_sdk/schema.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
(filter #(str/ends-with? (.getName %) ".gz")))]
(println "✅ Found packages:" (count packages))
packages))

(defn create-gzip-reader [path]
(-> path
(io/input-stream)
Expand All @@ -40,9 +39,7 @@
(map (fn [schema]
(if-not (:resourceType schema)
(assoc schema :resourceType "FHIRSchema")
schema)

))))
schema)))))

(defn prepare-schemas [schemas]
(map #(->> (get-in % [:package-meta :name])
Expand Down Expand Up @@ -106,6 +103,18 @@
(defn skip-root-package [packages]
(rest packages))

(defn get-fhir-version [package]
(let [allowed-base-packages #{"hl7.fhir.r4.core" "hl7.fhir.r4b.core" "hl7.fhir.r5.core"}]
(->> (keys (:dependencies package))
(map name)
(filter #(contains? allowed-base-packages %))
first)))

(defn enrich-schema-with-fhir-version [schema version]
(if version
(assoc schema :fhir-version version)
(assoc schema :fhir-version (:package schema))))

(defmethod retrieve :url
[{:keys [source]} {:keys [exit] :as opts}]
(let [extract-link (fn [package] (-> package :href))
Expand All @@ -122,7 +131,9 @@
[]))]
(->> fhir-packages
;; TODO using pmap for side effects is questionable
(pmap (fn [package]
(println "Downloading schemas for:" (extract-name package))
(fetch-n-parse (extract-link package) opts)))
(map (fn [package]
(let [base-package-name (get-fhir-version package)
schemas (fetch-n-parse (extract-link package) opts)]
(println "Downloading schemas for:" (extract-name package))
(map #(enrich-schema-with-fhir-version % base-package-name) schemas))))
(flatten))))
33 changes: 31 additions & 2 deletions test/aidbox_sdk/converter_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,37 @@
:summary true,
:elements {:referenceRange {:array true, :type "Reference"}}}}}))

(deftest test-resolve-dependencies
(testing "simple case"
(is (= #{"Address"
"Attachment"
"Period"
"CodeableConcept"
"ContactPoint"
"HumanName"
"DomainResource"
"Reference"
"Identifier"
"BackboneElement"}
(-> (sut/resolve-dependencies [(dissoc (fixt/get-data :patient-ir-schema) :deps)])
first
:deps))))

(testing "another package"
(is (= #{"Address"
"Attachment"
"Period"
"CodeableConcept"
"ContactPoint"
"HumanName"
"DomainResource"
"Reference"
"Identifier"
"BackboneElement"}
(-> (sut/resolve-dependencies [(dissoc (fixt/get-data :patient-ir-schema) :deps)])
first
:deps)))))

(deftest test-url->resource-name
(testing "one word"
(is (= "Immunization"
Expand Down Expand Up @@ -83,8 +114,6 @@
(fixt/get-data :us-core-vital-signs-fhir-schema)
(fixt/get-data :us-core-bmi-fhir-schema)]))))))

(fixt/get-data :vitalsigns-fhir-schema)

(deftest test-sort-by-base
(match
(sut/sort-by-base
Expand Down
67 changes: 67 additions & 0 deletions test/aidbox_sdk/fixtures/ehrsrle_provenance_ir_schema.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{:package "hl7.fhir.r4.core",
:derivation "constraint",
:name "Provenance",
:resource-name "ehrsrle-provenance",
:type "Provenance",
:elements
[{:name "agent",
:base "Provenance",
:array false,
:required false,
:value "string",
:type nil,
:choice-option false}
{:name "policy",
:base "Provenance",
:array false,
:required false,
:value "string",
:type nil,
:choice-option false}
{:name "reason",
:base "Provenance",
:array false,
:required false,
:value "string",
:type nil,
:choice-option false}
{:name "target",
:base "Provenance",
:array false,
:required false,
:value "string",
:type nil,
:choice-option false}
{:name "activity",
:base "Provenance",
:array false,
:required false,
:value "string",
:type nil,
:choice-option false}
{:name "location",
:base "Provenance",
:array false,
:required false,
:value "string",
:type nil,
:choice-option false}
{:name "recorded",
:base "Provenance",
:array false,
:required false,
:value "string",
:type nil,
:choice-option false}
{:name "signature",
:base "Provenance",
:array false,
:required false,
:value "string",
:type nil,
:choice-option false}],
:url "http://hl7.org/fhir/StructureDefinition/ehrsrle-provenance",
:base-resource-name "Provenance",
:backbone-elements [],
:base "http://hl7.org/fhir/StructureDefinition/Provenance",
:deps #{"Meta" "Provenance"}}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{:package "hl7.fhir.r4.core",
:technical-id
"hl7.fhir.r4.core/4.0.1/FHIRSchema/http://hl7.org/fhir/StructureDefinition/organization-preferredContact/4.0.1",
:fhir-version "hl7.fhir.r4.core"
:derivation "constraint",
:fhirVersion nil,
:excluded ["extension"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
:derivation "constraint",
:name "Extension",
:type "Extension",
:fhir-version "hl7.fhir.r4.core"
:resource-name "organization-preferred-Contact"
:elements
[{:name "url",
Expand Down
30 changes: 30 additions & 0 deletions test/aidbox_sdk/fixtures/us_core_bmi_fhir_schema.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{:package "hl7.fhir.us.core",
:technical-id
"hl7.fhir.us.core/6.1.0/FHIRSchema/http://hl7.org/fhir/us/core/StructureDefinition/us-core-bmi/6.1.0",
:derivation "constraint",
:fhirVersion nil,
:name "us-core-bmi",
:type "Observation",
:resourceType "FHIRSchema",
:elements
{:code
{:type "CodeableConcept",
:pattern {:coding [{:code "39156-5", :system "http://loinc.org"}]},
:mustSupport true},
:valueQuantity
{:max 1,
:min 0,
:elements
{:code {:type "code", :fixed "kg/m2", :mustSupport true},
:unit {:type "string", :mustSupport true},
:value {:type "decimal", :mustSupport true},
:system
{:type "uri", :fixed "http://unitsofmeasure.org", :mustSupport true}},
:required ["value" "unit" "system" "code"],
:mustSupport true}},
:id "us-core-bmi",
:kind "resource",
:url "http://hl7.org/fhir/us/core/StructureDefinition/us-core-bmi",
:packageVersion "6.1.0",
:base "http://hl7.org/fhir/us/core/StructureDefinition/us-core-vital-signs",
:version "6.1.0"}
Loading

0 comments on commit 024b8e8

Please sign in to comment.