metosin / schema-tools

Clojure(Script) tools for Plumatic Schema
http://metosin.github.io/schema-tools/
Eclipse Public License 2.0
107 stars 16 forks source link

Support for joda-time #47

Open ikitommi opened 6 years ago

ikitommi commented 6 years ago

For library ergonomics, would be great if there was bindings for joda-time classeses too. could be conditional, e.g. if the classes are found in classpath. Many libs (like https://github.com/metosin/metosin-common) still use joda...

(defmethod transform-type DateTime [_ _] {:type "string" :format "date-time"})
(defmethod transform-type LocalDate [_ _] {:type "string" :format "date"})
(defmethod transform-type LocalTime [_ _] {:type "string" :format "time"})
ikitommi commented 6 years ago

also, coercion needs this too.

(defn ->DateTime [date] (if (instance? Date date) (tc/from-date date) date))

(defn parse-date-time ^DateTime [date] (tf/parse (tf/formatters :date-time-parser) (->DateTime date)))
(defn parse-date ^DateTime [date] (tf/parse-local-date (tf/formatters :date) (->DateTime date)))
(defn parse-time ^DateTime [date] (tf/parse-local-time (tf/formatters :time-parser) (->DateTime date)))

(defn cond-matcher [preds]
  (fn [x]
    (or
      (some
        (fn [[f1 f2]]
          (if (f1 x) (f2 x)))
        preds)
      x)))

(def string->joda
  {DateTime (cond-matcher {string? parse-date-time})
   LocalDate (cond-matcher {string? parse-date})
   LocalTime (cond-matcher {string? parse-time})})