Fork me on GitHub
#yada
<
2016-01-16
>
dominicm12:01:27

@malcolmsparks: Perhaps there should be a record which designates that encoding has been taken care of? That would clearly separate the two use cases.

malcolmsparks12:01:51

@dominicm But what if the user formatted the json outside of yada?

dominicm12:01:38

@malcolmsparks: Then they'd take that string, put it in (TakenCareOf my-formatted-json) and that would be the return value.

dominicm12:01:08

And then use a multimethod to pick up the TakenCareOf record, and skip over formatting.

dominicm12:01:34

Or am I misunderstanding?

malcolmsparks13:01:10

Yes, they could wrap in a JsonString record. There's a design choice that will work. But it's a hassle for those who have to remember to wrap in JsonString. What Would Rich Do?

dominicm13:01:46

Jump in a hammock, but I don't have one.

dominicm13:01:58

I mean, the way you want to handle it is going to vary from case-to-case: Currently: Want to JSON encode yourself: (json-encode my-map) Want to JSON encode a map: my-map Want to .. a string: (json-encode string) JsonString: encode yourself (yada/json-string (json-encode my-map)) a map: my-map a string: string

dominicm13:01:58

I feel like always encoding primitives makes more sense in a consistency pov.

dominicm13:01:39

But the overhead would need addressing somehow. Whether that is encouraging that custom json-encode functions are written, or something else, I don't know.

;; Example encode
(def json-encode (comp yada/json-string some-lib/json-encode)
This would be a convenience wrapper around personal json-encoding, that would wrap it up in the JsonString record.

dominicm13:01:47

Simple, and Easy.

malcolmsparks16:01:21

Upsides: auto conversion of a string to a JSON string. Downsides: Unpleasant surprise for a diligent developer already 'doing the right thing' of using a json library to encode the string and declaring the mime-type correctly.

malcolmsparks16:01:42

It doesn't sit easy with me

malcolmsparks16:01:30

If the developer returns a clojure sequence or map then it's obvious that yada should encode that to json. But the string is much more problematic for me. I feel yada should get out of the way in such cases.

malcolmsparks16:01:15

Even if there is a workaround to 'tell' yada to get out the way, the developer might not be aware of it at the time of the unpleasant surprise. So it's not intuitive - the developer must guess that this feature must exist and resort to stack overflow to find out

malcolmsparks16:01:40

I'm not saying you're wrong of course :)

malcolmsparks16:01:22

I just have concerns that we need to think through in the hammock

malcolmsparks16:01:14

I do acknowledge that the status quo isn't ideal either. A user-agent will try to parse a json string which turns out to be textual error message. JSON parser blows up. It's a problem, yes.

malcolmsparks16:01:43

I used to have a hammock tied across two trees. I have since moved to a new place where tree distances do not allow for a hammock. More's the pity.

malcolmsparks17:01:14

I shall have to buy a ugly hammock stand from ikea :(

dominicm17:01:35

No no, they're good points. We're at the point of guessing human nature and usage. I need to review the docs for the current version to see what "feels" natural. But I'm already cursed by knowledge.

nha18:01:25

Could a solution be : put in :body things that are to be coerced, otherwise put them under another key, like :raw ?

malcolmsparks18:01:27

@nha that idea has legs

malcolmsparks18:01:17

@nha that's an interesting idea

nha18:01:06

I have no idea how it affects yada protocols, and it would also have to be clear that one cannot fill both though.

malcolmsparks18:01:09

:raw doesn't feel exactly the right kw but close

nha18:01:42

Yes I still have some work to learn to name things ^^

malcolmsparks18:01:57

Naming is hard :)

nha18:01:38

One of the two hard things in comp. sci simple_smile

malcolmsparks18:01:01

There's a similar feature in yada today where you can return the (:response ctx) perhaps modified and yada should then step aside

malcolmsparks18:01:10

It's a way to say to the method proxy "this is the actual response I was to return, leave it alone!"

nha18:01:25

Well I was not aware of any other ? I use it like :

(-> (:response ctx)
            (assoc :status 202)
            (assoc :body {:a-key "a-val"}))

dominicm18:01:03

It does incur overhead for all responses though. Given the coercive nature of yada, I see the dilemma a little better now.

malcolmsparks18:01:09

Yeah, you could just return

{:a-key "a-val"}

malcolmsparks18:01:31

Yada's method 'proxy' would put in the 202

malcolmsparks18:01:57

Each method has a proxy to codify the semantics

malcolmsparks18:01:20

It's a design I'm quite pleased about

dominicm18:01:22

Looking through the docs, if I had to support html, I think I'd go with my own resource type, which understood json/edn conversion of itself. It's similar to my own internal version of coercion I've come up with.

dominicm18:01:28

(defrecord Cloak
  [template data]
  JSON
  (json-encode []
     (default-action data))
  EDN
  (edn-encode []
    (default-action data))
  HTML
  (html-encode []
    (html/template template data)))

dominicm18:01:01

Then my handlers would just return a Cloak record.

nha18:01:18

That's very neat ! However if I return

{:a-key "a-val"}
I get
java.lang.IllegalArgumentException: No implementation of method: :interpret-post-result of protocol: #'yada.methods/PostResult found for class: clojure.lang.PersistentArrayMap

nha18:01:58

using [yada "1.1.0-20160114.191215-6"]

nha19:01:46

if I do (yada {:a-key "a-val"}) I get

java.lang.IllegalArgumentException: No implementation of method: :interpret-post-result of protocol: #'yada.methods/PostResult found for class: yada.handler.Handler
	at clojure.core$_cache_protocol_fn.invoke(core_deftype.clj:554)
	at yada.methods$eval51720$fn__51721$G__51711__51728.invoke(methods.clj:194) 
It rings a bell though, I think I saw something about that.

malcolmsparks19:01:25

@nha - yeah, it's only on GETs right now.

malcolmsparks19:01:39

I will add the same thing to other methods though

malcolmsparks19:01:50

sorry, forgot to mention that