Skip to content

Commit e599c33

Browse files
committed
fix: constraints generating algorithm
1 parent 1e7d48e commit e599c33

File tree

3 files changed

+31
-52
lines changed

3 files changed

+31
-52
lines changed

src/aidbox_sdk/fhir.clj

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
(def structure-definition? (resource-type-pred "StructureDefinition"))
1515
(def search-parameter? (resource-type-pred "SearchParameter"))
1616
(def value-set? (resource-type-pred "ValueSet"))
17+
(def fhir-schema? (resource-type-pred "FHIRSchema"))
1718

1819
;; Derivations
1920
(defn constraint? [schema] (= (:derivation schema) "constraint"))
20-
(defn specialization? [schema] (= (:derivation schema) "constraint"))
21+
(defn specialization? [schema] (= (:derivation schema) "specialization"))
2122

2223
;; Misc
2324
(defn extension? [schema] (= (:type schema) "Extension"))

src/aidbox_sdk/generator.clj

+28-50
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
safe-conj
77
uppercase-first-letter
88
vector-to-map]]
9+
[aidbox-sdk.fhir :as fhir]
910
[aidbox-sdk.schema :as schema]
1011
[clojure.java.io :as io]
1112
[clojure.set :as set]
@@ -558,35 +559,33 @@
558559
(apply-patterns (:url constraint) (filter #(contains? (last %) :pattern) (:elements constraint)))))
559560

560561
(defn apply-constraints [constraint-schemas base-schemas]
561-
(loop [result {}
562-
i 0]
563-
(if (or (= (count constraint-schemas) (count result))
564-
(> i (count constraint-schemas)))
562+
(loop [result {}]
563+
(if (= (count constraint-schemas) (count result))
565564
result
566-
(recur (reduce (fn [acc constraint-schema]
567-
(cond
568-
(contains? result (:url constraint-schema))
569-
acc
570-
571-
(contains? result (:base constraint-schema))
572-
(assoc acc
573-
(:url constraint-schema)
574-
(assoc (apply-single-constraint constraint-schema
575-
(get result (:base constraint-schema)))
576-
:package (:package constraint-schema)))
577-
578-
(contains? base-schemas (:base constraint-schema))
579-
(assoc acc
580-
(:url constraint-schema)
581-
(assoc (apply-single-constraint constraint-schema
582-
(get base-schemas (:base constraint-schema)))
583-
:package (:package constraint-schema)))
584-
585-
:else acc))
586-
587-
result
588-
constraint-schemas)
589-
(inc i)))))
565+
(recur
566+
(reduce (fn [acc constraint-schema]
567+
(cond
568+
(contains? result (:url constraint-schema))
569+
acc
570+
571+
(contains? result (:base constraint-schema))
572+
(assoc acc
573+
(:url constraint-schema)
574+
(assoc (apply-single-constraint constraint-schema
575+
(get result (:base constraint-schema)))
576+
:package (:package constraint-schema)))
577+
578+
(contains? base-schemas (:base constraint-schema))
579+
(assoc acc
580+
(:url constraint-schema)
581+
(assoc (apply-single-constraint constraint-schema
582+
(get base-schemas (:base constraint-schema)))
583+
:package (:package constraint-schema)))
584+
585+
:else acc))
586+
587+
result
588+
constraint-schemas)))))
590589

591590
;;
592591
;; Search Parameters
@@ -688,26 +687,6 @@
688687
(conj schema {:backbone-elements
689688
(flat-backbones (:backbone-elements schema) [])})))))
690689

691-
(defn generate-constraints [schemas]
692-
(let [base-schemas (->> schemas
693-
(prepared-schemas)
694-
(map (fn [schema]
695-
(conj schema {:backbone-elements
696-
(flat-backbones (:backbone-elements schema) [])})))
697-
(vector-to-map))
698-
constraints (filter #(and
699-
(constraint? %)
700-
(not (from-extension? %)))
701-
schemas)]
702-
(->> (apply-constraints
703-
constraints
704-
base-schemas)
705-
(mapv (fn [[name' schema]]
706-
{:name name'
707-
:schema schema
708-
:file-content (generate-constraint-namespace
709-
(assoc schema
710-
:url name'))})))))
711690

712691
(defn build-all! [& {:keys [auth input output]}]
713692
(let [output (io/file output)
@@ -721,7 +700,6 @@
721700
(constraint? %)
722701
(not (from-extension? %)))))]
723702

724-
725703
(prepare-target-directory! output)
726704

727705
;; create base namespace (all FHIR datatypes) file
@@ -779,7 +757,7 @@
779757
(println "Generating constraints classes")
780758
(doseq [{:keys [name schema file-content]}
781759
(->> (apply-constraints
782-
constraints
760+
(remove fhir/structure-definition? constraints)
783761
(->> all-schemas
784762
(prepared-schemas)
785763
(map (fn [schema]

test/aidbox_sdk/generator_test.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[matcho.core :as matcho]
44
[aidbox-sdk.generator :as sut]))
55

6-
(deftest apply-constraints-test
6+
(deftest test-apply-constraints
77
(testing "base schema is a specialization schema"
88

99
(def constraints [{:package "hl7.fhir.r4.core",

0 commit comments

Comments
 (0)