Skip to content

Commit

Permalink
fix base types and datatypes generation
Browse files Browse the repository at this point in the history
  • Loading branch information
krvital committed Sep 5, 2024
1 parent 8f18688 commit d58750c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 15 deletions.
18 changes: 11 additions & 7 deletions src/aidbox_sdk/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,21 @@
{:auth (:auth-token options)
:exit (:exit options)})

datatype? (every-pred fhir/fhir-schema? fhir/base-schema? fhir/datatype?)
domain-resource? (every-pred fhir/fhir-schema? fhir/base-schema? fhir/domain-resource?)
base-type? (every-pred fhir/fhir-schema? fhir/base-type?)
datatype? (every-pred fhir/fhir-schema? fhir/datatype? (complement fhir/primitive-type?))
domain-resource? (every-pred fhir/fhir-schema? fhir/domain-resource?)
constraint? (every-pred fhir/fhir-schema? fhir/constraint? (complement fhir/extension?))
search-param? (every-pred fhir/search-parameter? (complement fhir/search-parameter-from-extension?))

fhir-schemas (filter fhir/fhir-schema? all-schemas)
datatype-schemas (filter datatype? all-schemas)
resource-schemas (filter domain-resource? all-schemas)
constraint-schemas (filter constraint? all-schemas)
search-param-schemas (filter search-param? all-schemas)
base-schemas (filter base-type? all-schemas)
datatype-schemas (filter datatype? all-schemas)
resource-schemas (filter domain-resource? all-schemas)
constraint-schemas (filter constraint? all-schemas)
search-param-schemas (filter search-param? all-schemas)

ir-schemas (converter/convert fhir-schemas)
base-ir-schemas (converter/convert base-schemas)
datatype-ir-schemas (converter/convert datatype-schemas)
resource-ir-schemas (converter/convert resource-schemas)
search-param-ir-schemas (converter/convert-search-params search-param-schemas
Expand All @@ -90,7 +93,8 @@
(println "---")

(println "Generating datatypes")
(save-files! (generator/generate-datatypes generator' datatype-ir-schemas))
(save-files! (generator/generate-datatypes generator' (into base-ir-schemas
datatype-ir-schemas)))

(println "Generating resources")
(save-files! (map generate-resource-module resource-ir-schemas))
Expand Down
87 changes: 82 additions & 5 deletions src/aidbox_sdk/fhir.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,77 @@
(ns aidbox-sdk.fhir
(:require [clojure.string :as str]))
(:require
[clojure.set :as set]
[clojure.string :as str]))

;; Base Types and Datatypes

(def r4-base-types #{"Element" "Resource" "DomainResource"})

(def r4-primitive-types
#{"boolean" "integer" "string" "decimal" "uri" "url" "canonical" "base64Binary"
"instant" "date" "dateTime" "time" "code" "oid" "id" "markdown" "unsignedInt"
"positiveInt" "uuid"})

(def r4-general-purpose-datatypes
#{"Ratio" "Period" "Range" "Attachment" "Identifier" "HumanName" "ContactPoint"
"Address" "Signature" "SampledData" "Quantity" "Age" "Distance" "Duration"
"Count" "MoneyQuantity" "SimpleQuantity" "BackboneElement" "Timing" "Money"
"Coding" "CodeableConcept" "Annotation"})

(def r4-metadata-types
#{"ContactDetail" "Contributor" "DataRequirement" "UsageContext"
"TriggerDefinition" "Expression" "ParameterDefinition" "RelatedArtifact"})

(def r4-special-purpose-datatypes
#{"Reference" "Meta" "Dosage" "BackboneElement" "ElementDefinition" "Extension"
"Narrative" "xhtml"})

(def r4-datatypes
(set/union r4-primitive-types
r4-general-purpose-datatypes
r4-metadata-types
r4-special-purpose-datatypes))

(def r4b-base-types r4-base-types)

(def r4b-primitive-types r4-primitive-types)

(def r4b-general-purpose-datatypes
(into r4-general-purpose-datatypes #{"RationRange" "Identifier"}))

(def r4b-metadata-types r4-metadata-types)

(def r4b-special-purpose-datatypes
(into r4-special-purpose-datatypes #{"CodeableReference" "ProductShelfLife"
"MarketingStatus"}))

(def r4b-datatypes
(set/union r4b-primitive-types
r4b-general-purpose-datatypes
r4b-metadata-types
r4b-special-purpose-datatypes))

(def r5-base-types
#{"Base" "Element" "BackboneElement" "DataType" "PrimitiveType" "BackboneType"
"Resource" "DomainResource" "CanonicalResource" "MetadataResource"})

(def r5-primitive-types (set/union r4b-primitive-types #{"integer64"}))

(def r5-general-purpose-datatypes r4b-general-purpose-datatypes)

(def r5-metadata-types
(set/union r4b-metadata-types #{"Availability" "ExtendedContactDetail"
"MonetaryComponent" "VirtualServiceDetail"}))

(def r5-special-purpose-datatypes
#{"CodeableReference" "Meta" "Reference" "Dosage" "xhtml" "Narrative"
"Extension" "ElementDefinition" "BackboneType"})

(def r5-datatypes
(set/union r5-primitive-types
r5-general-purpose-datatypes
r5-metadata-types
r5-special-purpose-datatypes))

;; Predicates
(defn resource-type-pred [rt] (fn [schema] (= rt (:resourceType schema))))
Expand All @@ -25,10 +97,9 @@
(defn extension? [schema] (= (:type schema) "Extension"))

(defmulti datatype? :package)
(defmethod datatype? "hl7.fhir.r4.core" [schema] (= (:base schema) "http://hl7.org/fhir/StructureDefinition/Element"))
(defmethod datatype? "hl7.fhir.r4b.core" [schema] (= (:base schema) "http://hl7.org/fhir/StructureDefinition/Element"))
(defmethod datatype? "hl7.fhir.r5.core" [schema] (or (= (:base schema) "http://hl7.org/fhir/StructureDefinition/DataType")
(= (:base schema) "http://hl7.org/fhir/StructureDefinition/PrimitiveType")))
(defmethod datatype? "hl7.fhir.r4.core" [schema] (contains? r4-datatypes (:id schema)))
(defmethod datatype? "hl7.fhir.r4b.core" [schema] (contains? r4b-datatypes (:id schema)))
(defmethod datatype? "hl7.fhir.r5.core" [schema] (contains? r5-datatypes (:id schema)))
(defmethod datatype? :default [_] false)

(defmulti backbone-element? :package)
Expand All @@ -54,6 +125,11 @@

;;

(defmulti base-type? :package)
(defmethod base-type? "hl7.fhir.r4.core" [schema] (contains? r4-base-types (:id schema)) )
(defmethod base-type? "hl7.fhir.r4b.core" [schema] (contains? r4b-base-types (:id schema)) )
(defmethod base-type? "hl7.fhir.r5.core" [schema] (contains? r5-base-types (:id schema)) )

(defn base-schema? [schema]
(or (= (:url schema) "http://hl7.org/fhir/StructureDefinition/BackboneElement")
(= (:url schema) "http://hl7.org/fhir/StructureDefinition/Resource")
Expand All @@ -67,3 +143,4 @@

(defn search-parameter-from-extension? [search-parameter]
(str/includes? (:id search-parameter) "-extensions-"))

11 changes: 8 additions & 3 deletions src/aidbox_sdk/generator/typescript.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"code" "string"
"markdown" "string"
"id" "string"
"xhtml" "string"

;; hardcoded just in case
"Meta" "Meta"
Expand Down Expand Up @@ -101,9 +102,13 @@
(when (seq inner-classes)
(str (str/join "\n\n" inner-classes) "\n\n"))

"export type " class-name' " = " (when-not (str/blank? base-class) (str base-class " & ")) "{\n"
properties
"\n};")))
"export type " class-name' " = "
(when-not (str/blank? base-class)
(if (seq properties)
(str base-class " & ")
base-class))
(when (seq properties) (str "{\n" properties "\n}"))
";")))

(defn generate-deps [deps]
(->> deps
Expand Down

0 comments on commit d58750c

Please sign in to comment.