Skip to content

Commit 78d0d29

Browse files
committed
refactor: remove constraints conversion from dotnet's module
1 parent 55c89b5 commit 78d0d29

File tree

2 files changed

+0
-248
lines changed

2 files changed

+0
-248
lines changed

src/aidbox_sdk/generator/dotnet.clj

-100
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
[aidbox-sdk.generator.helpers :refer [->pascal-case uppercase-first-letter]]
55
[aidbox-sdk.generator.utils :as u]
66
[clojure.java.io :as io]
7-
[clojure.set :as set]
87
[clojure.string :as str])
98
(:import
109
[aidbox_sdk.generator CodeGenerator]))
@@ -195,105 +194,6 @@
195194
;; Constraints
196195
;;
197196

198-
(defn apply-excluded [excluded schema]
199-
(filter (fn [field-schema]
200-
(not (some #(= % (:name field-schema)) excluded)))
201-
schema))
202-
203-
(defn apply-required [required elements]
204-
(->> elements
205-
(map (fn [element]
206-
(if (contains? (set required) (:name element))
207-
(assoc element :required true)
208-
element)))))
209-
210-
(defn apply-choices [choices schema]
211-
(->> choices
212-
(map (fn [[key item]]
213-
(set/difference
214-
(set (:choices (first (filter #(= (:name %) (name key)) schema))))
215-
(set (:choices item)))))
216-
(reduce set/union #{})
217-
((fn [choices-to-exclude]
218-
(filter #(not (contains? choices-to-exclude (:name %))) schema)))))
219-
220-
(defn pattern-codeable-concept [name schema]
221-
(->> (str "}")
222-
(str "\tpublic new " (str/join ", " (map #(str "Coding" (str/join (str/split (:code %) #"-"))) (get-in schema [:pattern :coding] []))) "[] Coding { get; } = [new()];\n") #_(str/join ", " (map #(str "Coding" (str/join (str/split (:code %) #"-")) "()") (get-in schema [:pattern :coding] [])))
223-
(str "\nclass " (str/join (map uppercase-first-letter (str/split name #"-"))) " : CodeableConcept\n{\n")
224-
(str (when-let [coding (:coding (:pattern schema))]
225-
(str/join (map (fn [code]
226-
(->> (str "}")
227-
(str (when (contains? code :code) (str "\tpublic new string Code { get; } = \"" (:code code) "\";\n")))
228-
(str (when (contains? code :system) (str "\tpublic new string System { get; } = \"" (:system code) "\";\n")))
229-
(str (when (contains? code :display) (str "\tpublic new string Display { get; } = \"" (:display code) "\";\n")))
230-
(str "\n\nclass Coding" (str/join (str/split (:code code) #"-")) " : Coding\n{\n"))) coding))) "\n")))
231-
232-
(defn create-single-pattern [constraint-name [key schema] elements]
233-
(case (url->resource-name (some #(when (= (name key) (:name %)) (:value %)) elements))
234-
"CodeableConcept" (pattern-codeable-concept (str (uppercase-first-letter (url->resource-name constraint-name)) (uppercase-first-letter (subs (str key) 1))) schema) ""))
235-
236-
(defn apply-patterns [constraint-name patterns schema]
237-
(->> (map (fn [item]
238-
(if-let [pattern (some #(when (= (name (first %)) (:name item)) (last %)) patterns)]
239-
(case (:value item)
240-
"str" (assoc item :value (:pattern pattern) :literal true)
241-
"CodeableConcept" (conj item (hash-map :value (str
242-
(str/join
243-
(map uppercase-first-letter
244-
(str/split (url->resource-name constraint-name) #"-")))
245-
(str/join (map uppercase-first-letter
246-
(str/split (:name item) #"-"))))
247-
:codeable-concept-pattern true))
248-
"Quantity" item item) item)) (:elements schema))
249-
(hash-map :elements)
250-
(conj schema (hash-map :patterns (concat (get schema :patterns []) (map (fn [item] (create-single-pattern constraint-name item (:elements schema))) patterns))))))
251-
252-
(defn add-meta [constraint-name elements]
253-
(->> (filter #(not (= (:name %) "meta")) elements)
254-
(concat [{:name "meta"
255-
:required true :value "Meta"
256-
:meta (str " = new() { Profile = [\"" constraint-name "\"] };")}])))
257-
258-
(defn apply-single-constraint [constraint parent-schema]
259-
(->> (:elements parent-schema)
260-
(apply-required (:required constraint))
261-
(apply-excluded (:excluded constraint))
262-
(apply-choices (filter #(contains? (last %) :choices) (:elements constraint)))
263-
(add-meta (:url constraint))
264-
(hash-map :elements)
265-
(conj parent-schema)
266-
(apply-patterns (:url constraint) (filter #(contains? (last %) :pattern) (:elements constraint)))))
267-
268-
(defn apply-constraints [constraint-schemas base-schemas]
269-
(loop [result {}]
270-
(if (= (count constraint-schemas) (count result))
271-
result
272-
(recur
273-
(reduce (fn [acc constraint-schema]
274-
(cond
275-
(contains? result (:url constraint-schema))
276-
acc
277-
278-
(contains? result (:base constraint-schema))
279-
(assoc acc
280-
(:url constraint-schema)
281-
(assoc (apply-single-constraint constraint-schema
282-
(get result (:base constraint-schema)))
283-
:package (:package constraint-schema)))
284-
285-
(contains? base-schemas (:base constraint-schema))
286-
(assoc acc
287-
(:url constraint-schema)
288-
(assoc (apply-single-constraint constraint-schema
289-
(get base-schemas (:base constraint-schema)))
290-
:package (:package constraint-schema)))
291-
292-
:else acc))
293-
294-
result
295-
constraint-schemas)))))
296-
297197
(defn generate-constraint-module [schema]
298198
(generate-module
299199
:name (package->module-name (:package schema))

test/aidbox_sdk/generator/dotnet_test.clj

-148
Original file line numberDiff line numberDiff line change
@@ -3,157 +3,9 @@
33
[aidbox-sdk.fixtures.schemas :as fixtures]
44
[aidbox-sdk.generator :as sut]
55
[aidbox-sdk.generator.dotnet :refer [generator] :as gen.dotnet]
6-
[matcho.core :as matcho]
76
[clojure.java.io :as io]
87
[clojure.test :refer [deftest is testing]]))
98

10-
(deftest test-apply-constraints
11-
(testing "base schema is a specialization schema"
12-
13-
(def constraints
14-
[{:package "hl7.fhir.r4.core",
15-
:derivation "constraint",
16-
:excluded ["value"],
17-
:type "Extension",
18-
:elements
19-
{:extension
20-
{:slicing
21-
{:slices
22-
{:code
23-
{:schema
24-
{:scalar true,
25-
:excluded ["extension"],
26-
:required ["value"],
27-
:type "Extension",
28-
:elements
29-
{:url {:fixed "code", :type "uri"},
30-
:valueCodeableConcept
31-
{:required-element true, :type "CodeableConcept", :choiceOf "value"},
32-
:value {:choices ["valueCodeableConcept"]}}},
33-
:min 0,
34-
:max 1,
35-
:match {:type "pattern", :value {:url "code"}}},
36-
:period
37-
{:schema
38-
{:scalar true,
39-
:excluded ["extension"],
40-
:required ["value"],
41-
:type "Extension",
42-
:elements
43-
{:url {:fixed "period", :type "uri"},
44-
:valuePeriod
45-
{:required-element true, :type "Period", :choiceOf "value"},
46-
:value {:choices ["valuePeriod"]}}},
47-
:min 0,
48-
:max 1,
49-
:match {:type "pattern", :value {:url "period"}}}},
50-
:discriminator [{:type "value", :path "url"}],
51-
:rules "open"}},
52-
:url
53-
{:fixed "http://hl7.org/fhir/StructureDefinition/patient-nationality"}},
54-
:id "patient-nationality",
55-
:kind "complex-type",
56-
:url "http://hl7.org/fhir/StructureDefinition/patient-nationality",
57-
:base "http://hl7.org/fhir/StructureDefinition/Extension"}])
58-
59-
(def base {"http://hl7.org/fhir/StructureDefinition/Extension"
60-
{:package "hl7.fhir.r4.core",
61-
:derivation "specialization",
62-
:name "Extension",
63-
:type "Extension",
64-
:elements
65-
[{:name "valueBase64Binary",
66-
:base "Extension",
67-
:array false,
68-
:required false,
69-
:value "string"}],
70-
:url "http://hl7.org/fhir/StructureDefinition/Extension",
71-
:backbone-elements [],
72-
:base "http://hl7.org/fhir/StructureDefinition/Element"}})
73-
74-
(matcho/match
75-
(gen.dotnet/apply-constraints constraints base)
76-
{"http://hl7.org/fhir/StructureDefinition/patient-nationality"
77-
{:package "hl7.fhir.r4.core",
78-
:derivation "specialization",
79-
:name "Extension",
80-
:type "Extension",
81-
:elements
82-
[{:name "meta",
83-
:required true,
84-
:value "Meta",
85-
:meta
86-
" = new() { Profile = [\"http://hl7.org/fhir/StructureDefinition/patient-nationality\"] };"}
87-
{:name "valueBase64Binary",
88-
:base "Extension",
89-
:array false,
90-
:required false,
91-
:value "string"}]
92-
:url "http://hl7.org/fhir/StructureDefinition/Extension",
93-
:base "http://hl7.org/fhir/StructureDefinition/Element"}}))
94-
95-
(testing "base schema is a constraint schema"
96-
(def constraints [{:package "hl7.fhir.us.mcode",
97-
:derivation "constraint",
98-
:fhirVersion "4.0.1",
99-
:name "SecondaryCancerCondition",
100-
:type "Condition",
101-
:resourceType "StructureDefinition",
102-
:title "Secondary Cancer Condition Profile",
103-
:status "active",
104-
:id "mcode-secondary-cancer-condition",
105-
:kind "resource",
106-
:url
107-
"http://hl7.org/fhir/us/mcode/StructureDefinition/mcode-secondary-cancer-condition",
108-
:base "http://hl7.org/fhir/us/core/StructureDefinition/us-core-condition",
109-
:version "2.1.0",
110-
:fqn "hl7.fhir.us.mcode#2.1.0/mcode-secondary-cancer-condition"}])
111-
112-
(def base {"http://hl7.org/fhir/us/core/StructureDefinition/us-core-condition"
113-
{:package "hl7.fhir.us.core",
114-
:derivation "constraint",
115-
:fhirVersion "4.0.1",
116-
:jurisdiction [{:coding [{:system "urn:iso:std:iso:3166", :code "US"}]}],
117-
:name "USCoreCondition",
118-
:type "Condition",
119-
:experimental false,
120-
:resourceType "StructureDefinition",
121-
:elements {:recordedDate {:mustSupport true}}
122-
:title "US Core Condition Profile",
123-
:status "active",
124-
:id "us-core-condition",
125-
:kind "resource",
126-
:url "http://hl7.org/fhir/us/core/StructureDefinition/us-core-condition",
127-
:base "http://hl7.org/fhir/StructureDefinition/Condition",
128-
:version "4.1.0",
129-
:fqn "hl7.fhir.us.core#4.1.0/us-core-condition",
130-
:required ["category" "code"]}})
131-
132-
(matcho/match
133-
(gen.dotnet/apply-constraints constraints base)
134-
{"http://hl7.org/fhir/us/mcode/StructureDefinition/mcode-secondary-cancer-condition"
135-
{:package "hl7.fhir.us.mcode",
136-
:derivation "constraint",
137-
:name "USCoreCondition",
138-
:type "Condition",
139-
:experimental false,
140-
:resourceType "StructureDefinition",
141-
:elements
142-
[{:name "meta",
143-
:required true,
144-
:value "Meta",
145-
:meta
146-
" = new() { Profile = [\"http://hl7.org/fhir/us/mcode/StructureDefinition/mcode-secondary-cancer-condition\"] };"}
147-
[:recordedDate {:mustSupport true}]]
148-
:title "US Core Condition Profile",
149-
:status "active",
150-
:id "us-core-condition",
151-
:kind "resource",
152-
:url "http://hl7.org/fhir/us/core/StructureDefinition/us-core-condition",
153-
:base "http://hl7.org/fhir/StructureDefinition/Condition",
154-
:version "4.1.0",
155-
:fqn "hl7.fhir.us.core#4.1.0/us-core-condition",
156-
:required ["category" "code"]}})))
1579

15810
(deftest test-generate-property
15911
(testing "simple case"

0 commit comments

Comments
 (0)