Skip to content

Commit fb082ea

Browse files
feat(python-profiles): migrate from fqn
1 parent 50edb74 commit fb082ea

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

src/python-generator/second-try/main.clj

+34-38
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,17 @@
5151
(defn safe-conj [a b] (conj a (or b {})))
5252

5353
(defn compile-elements [schemas]
54-
(->> (vec schemas)
55-
#_(filter #(not (= (:derivation (last %)) "constraint")))
56-
(map (fn [[name definition]]
57-
(->> (help/elements-to-vector definition)
58-
(help/get-typings-and-imports (:type definition) (or (:required definition) []))
59-
(test1 (help/get-resource-name name))
60-
(safe-conj (hash-map :base (get definition :base)))
61-
(hash-map name))))
62-
(into {})))
54+
(map (fn [schema]
55+
(->> (help/elements-to-vector schema)
56+
(help/get-typings-and-imports (:type schema) (or (:required schema) []))
57+
(test1 (help/get-resource-name (:url schema)))
58+
(safe-conj (hash-map :base (get schema :base) :url (get schema :url))))) schemas))
6359

6460
(defn combine-elements [schemas]
65-
(->> (vec schemas)
66-
#_(filter #(= "hl7.fhir.r4.core#4.0.1/Observation" (first %)))
67-
(map (fn [[name definition]]
68-
(->> definition
69-
(mix-parents-elements-circular (dissoc schemas nil))
70-
(mix-parents-backbones-circular (dissoc schemas nil))
71-
(hash-map name))))
72-
(into {})))
61+
(map (fn [schema]
62+
(->> schema
63+
(mix-parents-elements-circular schemas)
64+
(mix-parents-backbones-circular schemas))) schemas))
7365

7466
(defn apply-excluded [excluded schema]
7567
(filter (fn [field-schema]
@@ -114,26 +106,26 @@
114106

115107

116108
(defn apply-single-constraint [constraint parent-schema]
109+
(println parent-schema)
117110
(->> (:elements parent-schema)
118111
(apply-required (:required constraint))
119112
(apply-excluded (:excluded constraint))
120113
(apply-choises (filter #(contains? (last %) :choices) (:elements constraint)))
121114
(hash-map :elements)
122115
(conj parent-schema)
123-
(apply-patterns (:fqn constraint) (filter #(contains? (last %) :pattern) (:elements constraint)))))
116+
(apply-patterns (:url constraint) (filter #(contains? (last %) :pattern) (:elements constraint)))))
124117

125118
(defn apply-constraints [constraint-schemas result base-schemas]
126-
(if (reduce (fn [acc, [schema-name]]
127-
(when (not (get result schema-name)) (reduced true))) false constraint-schemas)
128-
(apply-constraints constraint-schemas
129-
(reduce (fn [acc [schema-name definition]]
130-
(if (contains? result (:base definition))
131-
(conj acc (hash-map schema-name (apply-single-constraint definition (get result (:base definition)))))
132-
(if (contains? base-schemas (:base definition))
133-
(conj acc (hash-map schema-name (apply-single-constraint definition (get base-schemas (:base definition)))))
134-
acc))) result constraint-schemas) base-schemas) result))
135-
136-
(defn transform-structure [data] (into {} (map #(hash-map (:fqn %) %) data)))
119+
(if (reduce (fn [_, constraint-schema]
120+
(when (not (get result (:url constraint-schema))) (reduced true))) false constraint-schemas)
121+
(apply-constraints
122+
constraint-schemas
123+
(reduce (fn [acc constraint-schema]
124+
(if (contains? result (:base constraint-schema))
125+
(conj acc (hash-map (:url constraint-schema) (apply-single-constraint constraint-schema (get result (:base constraint-schema)))))
126+
127+
(if (contains? base-schemas (:base constraint-schema))
128+
(conj acc (hash-map (:url constraint-schema) (apply-single-constraint constraint-schema (get base-schemas (:base constraint-schema))))) acc))) result constraint-schemas) base-schemas) result))
137129

138130
(defn combine-single-class [name elements]
139131
(->> (map (fn [item]
@@ -153,22 +145,26 @@
153145
(str (str/join (map (fn [definition] (combine-single-class (:name definition) (:elements definition))) (:backbone-elements definition))))
154146
(str (str/join (:patterns definition)))
155147
(str "from ..base import *\n")
156-
(str "from typing import Optional\n")
148+
(str "from typing import Optional, Literal\n")
157149
(str "from pydantic import BaseModel\n")
158150
(help/write-to-file "/Users/gena.razmakhnin/Documents/aidbox-sdk-js/test_dir/constraint" (help/get-resource-name name))))
159151

160152
(defn doallmap [elements] (doall (map save-to-file elements)))
161153

162154
(defn main []
163-
(let [schemas (transform-structure (help/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-python-tooklit/fhir-schema-2/1.0.0_hl7.fhir.r4.core#4.0.1_package.ndjson.gz"))
164-
base-schemas (->> schemas (filter #(= (:derivation (last %)) "specialization")))
155+
(let [schemas (help/parse-ndjson-gz "/Users/gena.razmakhnin/Documents/aidbox-python-tooklit/fhir-schema-2/1.0.0_hl7.fhir.r4.core#4.0.1_package.ndjson.gz")
156+
base-schemas (->> schemas (filter #(= (:derivation %) "specialization")))
165157
constraint-schemas (->> schemas
166-
(filter #(= (:derivation (last %)) "constraint"))
167-
(filter #(or (= (first %) "hl7.fhir.r4.core#4.0.1/vitalsigns") (= (first %) "hl7.fhir.r4.core#4.0.1/triglyceride"))))]
158+
(filter #(= (:derivation %) "constraint"))
159+
(filter #(or (= (:url %) "http://hl7.org/fhir/StructureDefinition/vitalsigns") (= (:url %) "http://hl7.org/fhir/StructureDefinition/triglyceride") (= (:url %) "http://hl7.org/fhir/StructureDefinition/bmi"))))]
160+
168161
(->> base-schemas
169-
#_(compile-elements)
170-
#_(combine-elements)
171-
#_(apply-constraints constraint-schemas {})
172-
#_(map save-to-file))))
162+
(compile-elements)
163+
(combine-elements)
164+
(filter #(not (nil? (:url %))))
165+
(map (fn [item] (hash-map (:url item) item)))
166+
(into {})
167+
(apply-constraints constraint-schemas {})
168+
(map save-to-file))))
173169

174170
(main)

0 commit comments

Comments
 (0)