backend: defscalar int_string

Using `defscalar` fixes introspection errors.

Be aware, that UNION types are not implemented till now:
https://github.com/ajk/specialist-server/issues/3

Also `->Int` was moved from an explicit mapping postprocessing function
into the conformer.
This commit is contained in:
Johannes Lötzsch 2022-03-12 01:23:02 +01:00
parent b851df5476
commit 5abc0e7a8b
1 changed files with 15 additions and 9 deletions

View File

@ -13,19 +13,26 @@
(defn JaNein->bool [JaNein] (defn JaNein->bool [JaNein]
({"Ja" true "Nein" false} JaNein)) ({"Ja" true "Nein" false} JaNein))
(defn ->Int (t/defscalar int_string
"If unable to parse, we return the original string (requires union datatype)" {:name "Int" :description "Integer or String"}
[s] ;; TODO: Here we should be able to use s/with-gen
(try (fn [v]
(Integer/parseInt s) (cond
(catch NumberFormatException _e s))) (number? v)
(int v)
(string? v)
(try
(Integer/parseInt v)
(catch NumberFormatException _e v))
:else
::s/invalid)))
(def mapping_lifeline_wpforms {:id #(or (get % "E-Mail") (get % "Telefonnummer")) ;; TODO: uuid will be generated when record is written to db (def mapping_lifeline_wpforms {:id #(or (get % "E-Mail") (get % "Telefonnummer")) ;; TODO: uuid will be generated when record is written to db
:time_from_str "frühestes Einzugsdatum" :time_from_str "frühestes Einzugsdatum"
:time_duration_str "Möglicher Aufenthalt (Dauer)" ;; TODO: the duration is not parsed till now :time_duration_str "Möglicher Aufenthalt (Dauer)" ;; TODO: the duration is not parsed till now
:beds ["Verfügbare Betten" ->Int] :beds ["Verfügbare Betten" #(s/conform int_string %)]
:languages ["Sprachen (sprechen / verstehen)" #(split % #"\n")] :languages ["Sprachen (sprechen / verstehen)" #(split % #"\n")]
:place_country "Land" :place_country "Land"
@ -62,8 +69,7 @@
(s/def ::t_boolean t/boolean #_ (s/with-gen t/boolean #(s/gen boolean?))) (s/def ::t_boolean t/boolean #_ (s/with-gen t/boolean #(s/gen boolean?)))
(s/def ::t_string t/string #_ (s/with-gen t/string #(s/gen string?))) (s/def ::t_string t/string #_ (s/with-gen t/string #(s/gen string?)))
(s/def ::t_int_string (s/or :int t/int (s/def ::t_int_string int_string #_ (s/with-gen t/int #(s/gen int?)))
:string t/string) #_ (s/with-gen t/int #(s/gen int?)))
(s/def :xtdb.api/id (s/nilable ::t_string)) ;; TODO: in future not nilable (s/def :xtdb.api/id (s/nilable ::t_string)) ;; TODO: in future not nilable
(s/def ::time_from_str (s/nilable ::t_string)) (s/def ::time_from_str (s/nilable ::t_string))