This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-01-25
Channels
- # aatree (3)
- # admin-announcements (14)
- # aws (2)
- # beginners (23)
- # boot (58)
- # braid-chat (15)
- # cider (8)
- # clara (5)
- # cljs-dev (1)
- # cljsrn (38)
- # clojure (82)
- # clojure-china (2)
- # clojure-czech (16)
- # clojure-dev (11)
- # clojure-finland (1)
- # clojure-ireland (6)
- # clojure-japan (3)
- # clojure-portugal (1)
- # clojure-russia (152)
- # clojure-sg (7)
- # clojurebridge (3)
- # clojurescript (86)
- # code-reviews (3)
- # core-matrix (4)
- # cursive (25)
- # data-science (1)
- # datascript (2)
- # datavis (98)
- # datomic (21)
- # dirac (97)
- # emacs (1)
- # hoplon (1)
- # jobs (2)
- # ldnclj (21)
- # off-topic (3)
- # om (204)
- # pedestal (3)
- # proton (12)
- # reagent (125)
- # ring-swagger (9)
- # slackpocalypse (1)
- # spacemacs (1)
- # vim (1)
- # yada (19)
I'm trying to make a GET route with a query parameter that can be either a Long or a Date. I have
(GET* "/history" []
:summary "Get the transaction history diffs of the database"
:query-params [{begin :- TimePoint nil} {end :- TimePoint nil} {limit :- Long 50} {offset :- Long 0}]
:return s/Any
…
and
(s/defschema TimePoint
(s/maybe (s/either java.util.Date Long)))
However, when I do curl -s -G -d begin=1598495 -d limit=2 http://…
, my number is not converted from String to Long, and I get this: {"errors":{"begin":"(not (some-matching-either-clause? \"1598495\"))"}}
Is there a way to get it to convert (as it does if I specify just Long
instead of TimePoint
—but then I lose the ability to pass either a number or a date)?Query parameters will be strings so they would have to be coerced to Long or Date. Either Schema doesn't support coercion.
@juhoteperi ideas how that could be implemented? With cond-pre maybe?
If a TimePoint would just be a Date and one would add a Long->Date matcher to string-coercer.. might work. Not near a computer, can't test thou.
I don't know how conditional
works with coercion and I don't know what kind of predicate it would need
I'm trying stuff like this, but haven't gotten it to quite work in context yet:
(s/defschema TimePoint
(s/maybe (s/either java.util.Date Long)))
(defn timepoint-matcher [schema]
(when (= schema TimePoint)
(fn [x]
(if (= x "")
nil
(or (try
(Long/parseLong x)
(catch NumberFormatException e nil))
(clojure.instant/read-instant-date x))))))
(def timepoint-coercer (coerce/coercer TimePoint timepoint-matcher))