Skip to content

Commit

Permalink
refactor: remove unsued code and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krvital committed Aug 13, 2024
1 parent 7bae5a9 commit facbdcc
Show file tree
Hide file tree
Showing 4 changed files with 710 additions and 50 deletions.
39 changes: 17 additions & 22 deletions src/aidbox_sdk/converter.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@

(def primitives #{"dateTime" "xhtml" "Distance" "time" "date" "string" "uuid" "oid" "id" "Dosage" "Duration" "instant" "Count" "decimal" "code" "base64Binary" "unsignedInt" "url" "markdown" "uri" "positiveInt" "canonical" "Age" "Timing"})

(defn- url->resource-type [url]
(defn url->resource-name
"There are :id and :name in schemas but they are not reliable source."
[url]
(last (str/split (str url) #"/")))


;; flatten backbones

(defn flat-backbones [backbone-elements accumulator]
(defn flatten-backbones [backbone-elements accumulator]
(reduce (fn [acc item]
(concat (flat-backbones (:backbone-elements item) acc)
(concat (flatten-backbones (:backbone-elements item) acc)
[(dissoc item :backbone-elements)]))
accumulator
backbone-elements))
Expand Down Expand Up @@ -58,7 +57,6 @@
(mix-parents-elements-circular schemas)
(mix-parents-backbones-circular schemas))))


;;; compile elements

(defn- escape-keyword [word]
Expand All @@ -79,13 +77,13 @@
:else (if type (str "Base." type) "string")))

(defn- derive-basic-type [name element]
(get-type name (url->resource-type (:type element))))
(get-type name (url->resource-name (:type element))))

(defn- transform-element [name element required]
(->> (derive-basic-type name element)))

(defn- resolve-backbone-elements [[k, v]]
(if (= (url->resource-type (:type v)) "BackboneElement") (vector k, v) (vector)))
(if (= (url->resource-name (:type v)) "BackboneElement") (vector k, v) (vector)))

(defn- collect-types [parent-name required [k v]]
(if (contains? v :choices)
Expand All @@ -96,7 +94,7 @@
:array (boolean (:array v))
:required (.contains required (name k))
:value (transform-element
(str (url->resource-type parent-name) "_" (uppercase-first-letter (name k))) v (.contains required (name k)))}))
(str (url->resource-name parent-name) "_" (uppercase-first-letter (name k))) v (.contains required (name k)))}))

(defn- get-typings-and-imports [parent-name required data]
(reduce (fn [acc item]
Expand Down Expand Up @@ -134,35 +132,33 @@
(or (:required schema) [])
(seq (:elements schema)))
(clear-backbone-elements
(url->resource-type (:url schema)))
(url->resource-name (:url schema)))
(safe-conj
(hash-map :base (get schema :base)
:package (get schema :package)
:url (get schema :url)
:type (get schema :type)
:derivation (get schema :derivation))))))


;; resolve references

(defn- find-schema-by-url [schemas url]
(->> schemas
(filter (fn [s] (= (:url s) url)))
(filter #(= url (:url %)))
(first)))

(defn- find-element-by-reference [schemas reference]
(let [[url & path] reference
schema (find-schema-by-url schemas url)
(defn- find-element-by-reference [schemas element-reference]
(let [[schema-url & path] element-reference
schema (find-schema-by-url schemas schema-url)
element (get-in schema (map keyword path))]
(or element {})))

(defn resolve-references [schemas]
(walk/postwalk
(fn [x]
(if-let [reference (:elementReference x)]
(do (prn reference)
(merge (dissoc x :elementReference)
(find-element-by-reference schemas reference)))
(merge (dissoc x :elementReference)
(find-element-by-reference schemas reference))
x))
schemas))

Expand All @@ -171,6 +167,5 @@
(resolve-references)
(compile-elements)
(combine-elements)
(map (fn [schema]
(conj schema {:backbone-elements
(flat-backbones (:backbone-elements schema) [])})))))
(map (fn [schema] (update schema
:backbone-elements #(flatten-backbones % []))))))
33 changes: 7 additions & 26 deletions src/aidbox_sdk/generator.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
(ns aidbox-sdk.generator
(:refer-clojure :exclude [namespace])
(:require
[aidbox-sdk.generator.dotnet.templates :as dotnettpl]
[aidbox-sdk.generator.helpers :refer [->pascal-case
safe-conj
uppercase-first-letter
vector-to-map]]
[aidbox-sdk.converter :as converter]
[aidbox-sdk.fhir :as fhir]
[aidbox-sdk.generator.dotnet.templates :as dotnettpl]
[aidbox-sdk.generator.helpers :refer [->pascal-case uppercase-first-letter
vector-to-map]]
[aidbox-sdk.schema :as schema]
[clojure.java.io :as io]
[clojure.set :as set]
Expand All @@ -31,9 +29,6 @@
(= (:url schema) "http://hl7.org/fhir/StructureDefinition/Element")
(= (:derivation schema) "specialization")))

(defn search-parameter? [schema]
(= (:resourceType schema) "SearchParameter"))

(defn search-parameter-from-extension? [search-parameter]
(str/includes? (:id search-parameter) "-extensions-"))

Expand All @@ -42,26 +37,13 @@
[schema]
(= (:base schema) "http://hl7.org/fhir/StructureDefinition/DomainResource"))

(defn constraint? [schema]
(= (:derivation schema) "constraint"))

(defn from-extension? [schema]
(= (:type schema) "Extension"))

;;
;; Generator
;;

(defn url->resource-type [reference]
(last (str/split (str reference) #"/")))

(defn flat-backbones [backbone-elements accumulator]
(reduce (fn [acc item]
(concat (flat-backbones (:backbone-elements item) acc)
[(dissoc item :backbone-elements)]))
accumulator
backbone-elements))

(defn get-class-name [url]
(let [n (apply str (map uppercase-first-letter (str/split (url->resource-type url) #"-")))]
(cond
Expand Down Expand Up @@ -463,7 +445,7 @@

(defn search-parameters-for [schemas resource-name]
(->> schemas
(filter search-parameter?)
(filter fhir/search-parameter?)
(remove search-parameter-from-extension?)
(filter #(contains? (set (:base %)) resource-name))))

Expand Down Expand Up @@ -536,11 +518,11 @@
all-schemas (schema/retrieve
(schema/resource input)
{:auth auth})
search-params-schemas (filter search-parameter? all-schemas)
search-params-schemas (filter fhir/search-parameter? all-schemas)
constraints (->> all-schemas
(filter #(and
(constraint? %)
(not (from-extension? %)))))]
(fhir/constraint? %)
(not (fhir/extension? %)))))]

(prepare-target-directory! output)

Expand Down Expand Up @@ -602,7 +584,6 @@
(filter fhir/fhir-schema? constraints)
(->> (filter fhir/fhir-schema? all-schemas)
(converter/convert)
(map #(assoc % :backbone-elements (flat-backbones (:backbone-elements %) [])))
(vector-to-map)))
(mapv (fn [[name' schema]]
{:name name'
Expand Down
34 changes: 32 additions & 2 deletions test/aidbox_sdk/converter_test.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
(ns aidbox-sdk.converter-test
(:require
[clojure.test :refer [deftest is]]
[clojure.test :refer [deftest is are testing]]
[aidbox-sdk.fixtures.schemas :as fixtures]
[aidbox-sdk.converter :as sut]))

(deftest test-converter-utils
(testing "url->resource-name"
(are [x y] (= x y)
"date"
(sut/url->resource-name "http://hl7.org/fhir/StructureDefinition/date")

(deftest resolve-reference-test
"ContactDetail"
(sut/url->resource-name "http://hl7.org/fhir/StructureDefinition/ContactDetail")

"Immunization"
(sut/url->resource-name "http://hl7.org/fhir/StructureDefinition/Immunization")

"openEHR-exposureDate"
(sut/url->resource-name "http://hl7.org/fhir/StructureDefinition/openEHR-exposureDate")

"iso21090-ADXP-deliveryAddressLine"
(sut/url->resource-name "http://hl7.org/fhir/StructureDefinition/iso21090-ADXP-deliveryAddressLine"))))

(deftest test-resolve-references
(is (= (sut/resolve-references fixtures/schemas-with-element-reference)
fixtures/schemas-with-element-reference-resolved)))

(deftest test-backbones-flattening
(is (= (sut/flatten-backbones fixtures/unflattened-backbone-elements [])
fixtures/flattened-backbone-elements)))

(deftest test-convert
(testing "convert resource"
(is (= (sut/convert [fixtures/patient-fhir-schema])
[fixtures/patient-ir-schema])))

(testing "convert constraint"
(is (= (sut/convert [fixtures/organization-preferred-contact-fhir-schema])
[fixtures/organization-preferred-contact-ir-schema]))))
Loading

0 comments on commit facbdcc

Please sign in to comment.