ring-swagger

ring-swagger & compojure-api
slipset 2017-10-17T08:18:53.000041Z

@ikitommi some sort of (st/add-custom-coersion [:body :formats "application/json" :timestamp] json-timestamp->long)

slipset 2017-10-17T08:19:00.000196Z

would be a nice function to have.

slipset 2017-10-17T08:19:40.000392Z

for some permutation of [:body :formats "application/json" :timestamp]

ikitommi 2017-10-17T08:42:55.000087Z

What if we turn this around and add the coercers directly to Spec (Records)?

slipset 2017-10-17T08:43:06.000047Z

Yeah!

ikitommi 2017-10-17T08:43:46.000379Z

(st/spec
   {:pred (partial DateTime)
    :coercion {:string str->date-time}
    :json-schema/examples "..."})

slipset 2017-10-17T08:43:47.000100Z

(st/spec {:coercer json-timestamp->long :type :timestamp})

ikitommi 2017-10-17T08:43:54.000133Z

👍

slipset 2017-10-17T08:43:55.000232Z

🙂

slipset 2017-10-17T08:44:25.000187Z

yeah, you need the map there right?

slipset 2017-10-17T08:45:02.000482Z

So the keys in the coercion-map would be #{:string :json :edn}

slipset 2017-10-17T08:45:05.000109Z

?

slipset 2017-10-17T08:46:27.000049Z

and how would you then control which part of the lifecycle you’d want coercion in (request/response)?

ikitommi 2017-10-17T08:46:46.000388Z

There are three scopes for coercion (`:request`, :response and :string), but only two modes (`:json` and :string).

slipset 2017-10-17T08:46:52.000508Z

I guess if you need finegrained control, you do as you did in your gist.

slipset 2017-10-17T08:47:06.000363Z

make the simple case simple, and the hard case possible 🙂

slipset 2017-10-17T08:47:59.000173Z

As a newcomer to this project, I guess the fact that :string is present both in scope and mode kind of confuses me. But that might just be me.

ikitommi 2017-10-17T08:48:14.000114Z

That is confusing.

ikitommi 2017-10-17T08:48:27.000018Z

the Spec Records should not know anything about the request/response, but they should know what kind of coercion should occur. Schema had string and json.

ikitommi 2017-10-17T08:48:45.000289Z

spec-tools has those too

slipset 2017-10-17T08:49:00.000113Z

And don’t get me wrong. I’m super happy for this work and your help.

slipset 2017-10-17T08:49:30.000160Z

Any criticism here is just so we can make this better.

ikitommi 2017-10-17T08:51:11.000175Z

thanks, want to make things easy too. There was an idea with schemas, how to bundle ceorcion + encoding + decoding into a one concept: adding a type could be done in one place. I think we could do that now, for both Schemas & Specs. Idea is described here: https://github.com/metosin/web-schemas

ikitommi 2017-10-17T08:52:08.000139Z

As jsonista supports explicit mappings (opposed to Cheshire having global extensions), we can do this now.

ikitommi 2017-10-17T08:54:36.000311Z

e.g. introduce MyNewType, one can describe all the things (encode, decode (multiple formats: json, edn, transit), coercion in different types, docs) in single definition. With syntax validation. Muuntaja, Jsonista and Schema/Spec-tools would take parts of those and together they would just work. And there would be a test-suite: “which types can I pass in which contexts?”

ikitommi 2017-10-17T08:55:25.000011Z

Currently, if a EDN-writer is missing for a custom type, it can fail at runtime when first time tried to pass over the wire. Temporal runtime coupling. Which is BAD.

ikitommi 2017-10-17T08:56:01.000331Z

Spec Record could be the host for all the info, just need to figure out the format in which. Ideas welcome.

ikitommi 2017-10-17T08:56:35.000265Z

And this should be a small micro-library, might be good for other web libs too.

ikitommi 2017-10-17T08:57:51.000338Z

can’t remember how this is/was solved in the java/scala-land.

slipset 2017-10-17T08:58:16.000202Z

It’s a bit much for me to wrap my head around right now 🙂

ikitommi 2017-10-17T08:58:26.000051Z

me too, off to lunch 😉

slipset 2017-10-17T08:58:29.000266Z

Ok 😉

slipset 2017-10-17T08:58:45.000095Z

It could be cool if the coercer was a multi-method though?

slipset 2017-10-17T09:00:32.000175Z

(defmethod coerce [:time-tamp :json :request] [{:keys [value]] ...)

slipset 2017-10-17T09:01:19.000008Z

(defmulti coerce type-mode-scope)

slipset 2017-10-17T09:02:05.000191Z

(defn type-mode-scope [{:keys [type mode scope]}] [type mode scope])

slipset 2017-10-17T09:02:21.000034Z

Or something along these lines.

slipset 2017-10-17T09:03:12.000003Z

Problem with this is that you get the temporal coupling. If I don’t implement the multi-method for a given type/mode/scope combination, it might blow up in production.