Skip to content

Commit

Permalink
fix(typescript): valuesets imports
Browse files Browse the repository at this point in the history
  • Loading branch information
krvital committed Oct 21, 2024
1 parent 6c64d2e commit 653745a
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 96 deletions.
40 changes: 25 additions & 15 deletions src/aidbox_sdk/converter.clj
Original file line number Diff line number Diff line change
Expand Up @@ -280,27 +280,37 @@
(defn resolve-choices [schemas]
(map resolve-schema-choices schemas))

(defn collect-dependencies [schema]
(let [primitive-element? (partial fhir/primitive-element? (:fhir-version schema))]
(defn collect-dependencies
"Returns the set of dependencies for the provided schema."
[schema]
(let [primitive-element? (partial fhir/primitive-element? (:fhir-version schema))
backbones-elements (->> (:backbone-elements schema)
(map :elements)
flatten)
all-elements (into (:elements schema) backbones-elements)
types (->> all-elements
(remove primitive-element?)
(map :type))]
(set/union
(cond-> #{}
(:base-resource-name schema) (conj (:base-resource-name schema))
(fhir/constraint? schema) (conj "Meta"))
(->> (:elements schema)
(remove primitive-element?)
(map :type)
(remove nil?)
set)
(->> (:backbone-elements schema)
(map :elements)
flatten
(remove primitive-element?)
(map :type)
(remove nil?)
set))))
(set (remove nil? types)))))

(defn collect-valueset-dependencies
[schema]
(let [backbones-elements (->> (:backbone-elements schema)
(map :elements)
flatten)
all-elements (into (:elements schema) backbones-elements)
valusets (map :valueset all-elements)]
(set (remove nil? valusets))))

(defn resolve-dependencies [schemas]
(map #(assoc % :deps (collect-dependencies %)) schemas))
(map #(-> %
(assoc :deps (collect-dependencies %))
(assoc :valueset-deps (collect-valueset-dependencies %)))
schemas))

(defn resolve-valuesets [schema available-valuesets]
(let [available-valuesets (get available-valuesets (:fhir-version schema))]
Expand Down
1 change: 1 addition & 0 deletions src/aidbox_sdk/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
(doseq [{:keys [path content]} files]
(save-to-file! (io/file output-dir path) content)))


;;
;;

Expand Down
25 changes: 18 additions & 7 deletions src/aidbox_sdk/generator/typescript.clj
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@
(->backbone-type element)

(:valueset element)
(str "vs." (class-name (:valueset element)))
(class-name (:valueset element))

:else
(->lang-type (:type element)))
primitive-type? (fhir/primitive-element? fhir-version element)]
(str (str (->camel-case name) optional ": " type' (when array "[]") ";")
(when primitive-type?
(str "\n" u/indent "_" (->camel-case name) ": Element;")))))))
(str "\n" u/indent "_" (->camel-case name) "?: Element;")))))))

(defn generate-class
"Generates TypeScript type from IR (intermediate representation) schema."
Expand All @@ -131,6 +131,7 @@
(remove nil?)
(map u/add-indent)
(str/join "\n"))]

(str
(when (seq inner-classes)
(str (str/join "\n\n" inner-classes) "\n\n"))
Expand Down Expand Up @@ -171,18 +172,28 @@
(let [relative-path (if (fhir/base-package? ir-schema)
"./"
(str "../" (package->directory (:fhir-version ir-schema)) "/"))
valueset-dep (when (fhir/base-package? ir-schema)
"import * as vs from \"./valuesets\"")]
valueset-deps (when (and (fhir/base-package? ir-schema)
(seq (:valueset-deps ir-schema)))
(format "import { %s } from \"./valuesets\""
(->> (:valueset-deps ir-schema)
(map class-name)
(str/join ", "))))]
(str (->> (: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))))
(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"))
(when valueset-dep
(when valueset-deps
(str
"\n"
valueset-dep)))))
valueset-deps)))))

(defn generate-module
[& {:keys [deps classes]
Expand Down
26 changes: 23 additions & 3 deletions test/aidbox_sdk/converter_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,16 @@

(deftest test-convert
(testing "convert resource"
(match (sut/convert [fixtures/patient-fhir-schema] #{})
(match (sut/convert [fixtures/patient-fhir-schema]
{"hl7.fhir.r4.core"
#{"http://hl7.org/fhir/ValueSet/administrative-gender"}})

[fixtures/patient-ir-schema]))

(testing "convert constraint"
(is (= [(fixt/get-data :organization-preferred-contact-ir-schema)]
(sut/convert [(fixt/get-data :organization-preferred-contact-fhir-schema)] #{})))))
(match
(sut/convert [(fixt/get-data :organization-preferred-contact-fhir-schema)] #{})
[(fixt/get-data :organization-preferred-contact-ir-schema)])))

(deftest test-apply-constraints
(testing "constraints"
Expand Down Expand Up @@ -133,9 +137,25 @@
{:url "http://hl7.org/fhir/StructureDefinition/SampledData",
:base "http://hl7.org/fhir/StructureDefinition/Element"}]))

(deftest test-collect-dependencies
(match
(sut/collect-dependencies (fixt/get-data :patient-ir-schema))
#{"Address"
"Attachment"
"Period"
"CodeableConcept"
"ContactPoint"
"HumanName"
"DomainResource"
"Reference"
"Identifier"
"BackboneElement"}))

(comment
(fixt/load-data!)

@fixt/data

(keys @fixt/data)

::close)
2 changes: 2 additions & 0 deletions test/aidbox_sdk/fixtures/patient_ir_schema.edn
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
:array false,
:required false,
:value "string",
:valueset "administrative-gender"
:type "code",
:choice-option false}
{:name "maritalStatus",
Expand Down Expand Up @@ -259,6 +260,7 @@
:base "BackboneElement"
:name "Patient_Contact"}],
:base "http://hl7.org/fhir/StructureDefinition/DomainResource",
:valueset-deps #{"administrative-gender"}
:deps
#{"Address"
"Attachment"
Expand Down
Loading

0 comments on commit 653745a

Please sign in to comment.