Skip to content

Commit

Permalink
feat: add Element properties in typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
krvital committed Oct 10, 2024
1 parent 966cc2b commit 8306767
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/aidbox_sdk/generator/typescript.clj
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
(defn ->backbone-type [element]
(str/replace (str (:base element) (uppercase-first-letter (:name element))) #"[_-]" ""))

(defn generate-property [{:keys [name array required type choices profile] :as element}]
(defn generate-property [{:keys [name array required type choices profile fhir-version] :as element}]
(let [optional (if required "" "?")]
(cond choices
(generate-polymorphic-property element)
Expand All @@ -92,10 +92,13 @@
(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 "[]") ";")))))
(let [primitive-type? (fhir/primitive-element? fhir-version element)
type' (if (= "BackboneElement" type)
(->backbone-type element)
(->lang-type (:type element)))]
(str (str (->camel-case name) optional ": " type' (when array "[]") ";")
(when primitive-type?
(str u/indent "\n" "_" (->camel-case name) ": Element;")))))))

(defn generate-class
"Generates TypeScript type from IR (intermediate representation) schema."
Expand All @@ -110,7 +113,9 @@
""))
properties (->> (:elements ir-schema)
(remove #(:choice-option %))
(map #(assoc % :fhir-version (:fhir-version ir-schema)))
(map generate-property)
(flatten)
(remove nil?)
(map u/add-indent)
(str/join "\n"))]
Expand Down
8 changes: 7 additions & 1 deletion test/aidbox_sdk/generator/typescript_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,19 @@
" managingOrganization?: Reference;"
" name?: HumanName[];"
" birthDate?: string;"
" _birthDate: Element;"
" multipleBirth?: boolean | number;"
" deceased?: string | boolean;"
" photo?: Attachment[];"
" link?: PatientLink[];"
" active?: boolean;"
" _active: Element;"
" communication?: PatientCommunication[];"
" identifier?: Identifier[];"
" telecom?: ContactPoint[];"
" generalPractitioner?: Reference[];"
" gender?: string;"
" _gender: Element;"
" maritalStatus?: CodeableConcept;"
" contact?: PatientContact[];"
"};"])
Expand Down Expand Up @@ -165,16 +168,19 @@
" managingOrganization?: Reference;"
" name?: HumanName[];"
" birthDate?: string;"
" _birthDate: Element;"
" multipleBirth?: boolean | number;"
" deceased?: string | boolean;"
" photo?: Attachment[];"
" link?: PatientLink[];"
" active?: boolean;"
" _active: Element;"
" communication?: PatientCommunication[];"
" identifier?: Identifier[];"
" telecom?: ContactPoint[];"
" generalPractitioner?: Reference[];"
" gender?: string;"
" _gender: Element;"
" maritalStatus?: CodeableConcept;"
" contact?: PatientContact[];"
"};"])
Expand All @@ -192,7 +198,7 @@
(is
(= {:path (io/file "hl7-fhir-r4-core/Patient.ts"),
:content
"import { Address } from \"./Address\";\nimport { Attachment } from \"./Attachment\";\nimport { Period } from \"./Period\";\nimport { CodeableConcept } from \"./CodeableConcept\";\nimport { ContactPoint } from \"./ContactPoint\";\nimport { HumanName } from \"./HumanName\";\nimport { DomainResource } from \"./DomainResource\";\nimport { Reference } from \"./Reference\";\nimport { Identifier } from \"./Identifier\";\nimport { BackboneElement } from \"./BackboneElement\";\n\nexport type PatientLink = BackboneElement & {\n type: string;\n other: Reference;\n};\n\nexport type PatientCommunication = BackboneElement & {\n language: CodeableConcept;\n preferred?: boolean;\n};\n\nexport type PatientContact = BackboneElement & {\n name?: HumanName;\n gender?: string;\n period?: Period;\n address?: Address;\n telecom?: ContactPoint[];\n organization?: Reference;\n relationship?: CodeableConcept[];\n};\n\nexport type Patient = DomainResource & {\n address?: Address[];\n managingOrganization?: Reference;\n name?: HumanName[];\n birthDate?: string;\n multipleBirth?: boolean | number;\n deceased?: string | boolean;\n photo?: Attachment[];\n link?: PatientLink[];\n active?: boolean;\n communication?: PatientCommunication[];\n identifier?: Identifier[];\n telecom?: ContactPoint[];\n generalPractitioner?: Reference[];\n gender?: string;\n maritalStatus?: CodeableConcept;\n contact?: PatientContact[];\n};"}
"import { Address } from \"./Address\";\nimport { Attachment } from \"./Attachment\";\nimport { Period } from \"./Period\";\nimport { CodeableConcept } from \"./CodeableConcept\";\nimport { ContactPoint } from \"./ContactPoint\";\nimport { HumanName } from \"./HumanName\";\nimport { DomainResource } from \"./DomainResource\";\nimport { Reference } from \"./Reference\";\nimport { Identifier } from \"./Identifier\";\nimport { BackboneElement } from \"./BackboneElement\";\n\nexport type PatientLink = BackboneElement & {\n type: string;\n other: Reference;\n};\n\nexport type PatientCommunication = BackboneElement & {\n language: CodeableConcept;\n preferred?: boolean;\n};\n\nexport type PatientContact = BackboneElement & {\n name?: HumanName;\n gender?: string;\n period?: Period;\n address?: Address;\n telecom?: ContactPoint[];\n organization?: Reference;\n relationship?: CodeableConcept[];\n};\n\nexport type Patient = DomainResource & {\n address?: Address[];\n managingOrganization?: Reference;\n name?: HumanName[];\n birthDate?: string;\n _birthDate: Element;\n multipleBirth?: boolean | number;\n deceased?: string | boolean;\n photo?: Attachment[];\n link?: PatientLink[];\n active?: boolean;\n _active: Element;\n communication?: PatientCommunication[];\n identifier?: Identifier[];\n telecom?: ContactPoint[];\n generalPractitioner?: Reference[];\n gender?: string;\n _gender: Element;\n maritalStatus?: CodeableConcept;\n contact?: PatientContact[];\n};"}
(sut/generate-resource-module generator (fixt/get-data :patient-ir-schema)))))

#_(deftest test-generate-search-params
Expand Down

0 comments on commit 8306767

Please sign in to comment.