Skip to content

Commit

Permalink
fix: consider required/optional key in property generation
Browse files Browse the repository at this point in the history
  • Loading branch information
krvital committed Oct 7, 2024
1 parent 5d20243 commit 9495b2f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 64 deletions.
5 changes: 3 additions & 2 deletions src/aidbox_sdk/generator/dotnet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@
[{:keys [name array required value type choices] :as element}]
(let [name' (->pascal-case name)
lang-type (str/replace (or value type "") #"[_-]" "")
required' (if required "required " "")
type (str
(when required "required ")
required'
lang-type
(:generic element)
(when array "[]")
Expand All @@ -134,7 +135,7 @@
(= (:type element) "Meta")
(if (:profile element)
(format "public new Meta Meta { get; } = new() { Profile = [\"%s\"] };" (:profile element))
(format "public %s Meta { get; set; }" name'))
(format "public %s%s Meta { get; set; }" required' name'))

:else
(str "public " type " " name' " { get; set; }"
Expand Down
48 changes: 24 additions & 24 deletions src/aidbox_sdk/generator/python.clj
Original file line number Diff line number Diff line change
Expand Up @@ -115,35 +115,35 @@
lang-type (if (= "BackboneElement" (:type element))
(->backbone-type element)
(->lang-type (:type element)))
lang-type
(cond->> lang-type

:always
remove-guard-from-class-name
lang-type (cond->> lang-type

(and forward-reference? (starts-with-capital? lang-type))
(format "'%s'"))
:always
remove-guard-from-class-name

type (cond
;; required and array
(and (:required element)
(:array element))
(format "list[%s]" lang-type)
(and forward-reference? (starts-with-capital? lang-type))
(format "'%s'"))

;; not required and array
(and (not (:required element))
(:array element))
(format "Optional[List[%s]]" lang-type)
type (cond
;; required and array
(and (:required element)
(:array element))
(format "list[%s]" lang-type)

;; required and not array
(and (:required element)
(not (:array element)))
lang-type
;; not required and array
(and (not (:required element))
(:array element))
(format "Optional[List[%s]]" lang-type)

;; not required and not array
(and (not (:required element))
(not (:array element)))
(format "Optional[%s]" lang-type))
;; required and not array
(and (:required element)
(not (:array element)))
lang-type

;; not required and not array
(and (not (:required element))
(not (:array element)))
(format "Optional[%s]" lang-type))

default-value (cond
(:meta element)
Expand Down Expand Up @@ -186,7 +186,7 @@
(map u/add-indent)
(str/join "\n"))
base-class-literal (when-not (str/blank? base-class)
(format "(%s)" base-class))]
(format "(%s)" base-class))]
(str
(when (seq inner-classes)
(str (str/join "\n\n" inner-classes) "\n\n"))
Expand Down
27 changes: 14 additions & 13 deletions src/aidbox_sdk/generator/typescript.clj
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,20 @@
(str/replace (str (:base element) (uppercase-first-letter (:name element))) #"[_-]" ""))

(defn generate-property [{:keys [name array required type choices profile] :as element}]
(cond choices
(generate-polymorphic-property element)

(= type "Meta")
(if profile
(format "%s: Meta & { profile: [\"%s\"] };" name profile)
(format "%s: Meta;" name))

:else
(let [type' (if (= "BackboneElement" type)
(->backbone-type element)
(->lang-type (:type element)))]
(str (->camel-case name) (when-not required "?") ": " type' (when array "[]") ";"))))
(let [optional (if required "" "?")]
(cond choices
(generate-polymorphic-property element)

(= type "Meta")
(if profile
(format "%s%s: Meta & { profile: [\"%s\"] };" name optional profile)
(format "%s%s: Meta;" name optional))

:else
(let [type' (if (= "BackboneElement" type)
(->backbone-type element)
(->lang-type (:type element)))]
(str (->camel-case name) optional ": " type' (when array "[]") ";")))))

(defn generate-class
"Generates TypeScript type from IR (intermediate representation) schema."
Expand Down
14 changes: 13 additions & 1 deletion test/aidbox_sdk/generator/dotnet_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,21 @@
(testing "element with meta"
(is (= "public new Meta Meta { get; } = new() { Profile = [\"http://hl7.org/fhir/StructureDefinition/vitalsigns\"] };"
(gen.dotnet/generate-property {:name "meta",
:required true,
:required false,
:value "Meta",
:profile "http://hl7.org/fhir/StructureDefinition/vitalsigns",
:type "Meta"})))

(is (= "public required Meta Meta { get; set; }"
(gen.dotnet/generate-property {:name "meta",
:required true,
:value "Meta",
:type "Meta"})))

(is (= "public Meta Meta { get; set; }"
(gen.dotnet/generate-property {:name "meta"
:required false
:value "Meta"
:type "Meta"}))))

(testing "element with choices"
Expand Down
Loading

0 comments on commit 9495b2f

Please sign in to comment.