Fork me on GitHub
#ring
<
2021-07-27
>
Jacob Rosenzweig01:07:48

Can anyone tell me why this decoder function works but not the encoder function?

(def instance
  (m/create
    (-> m/default-options
        (update-in
          [:formats "application/transit+json" :decoder-opts]
          (partial merge time/time-deserialization-handlers))
        (update-in
          [:formats "application/transit+json" :encoder-opts]
          (partial merge time/time-serialization-handlers))
        (update-in
          [:formats "application/json" :encoder-opts]
          (partial merge {:encode-key-fn #(name (csk/->camelCase %))}))
        (update-in
          [:formats "application/json" :decoder-opts]
          (partial merge {:decode-key-fn #(keyword (csk/->kebab-case %))})))))

Jacob Rosenzweig01:07:04

I still get camel case in my web responses.

seancorfield01:07:31

@rosenjcb It would help if you explained what m and time were and how this instance is used.

Jacob Rosenzweig01:07:52

So for context, this is what I get out of the luminus template. I honestly am not sure where this belongs, so I put it in the #ring channel as a muuntaja channel doesn't exist. As far as I can tell, it's used by the Muuntaja JSON middleware. One suggestion someone had was this: https://github.com/duct-framework/duct/issues/104 Which basically boils down to adding custom decoder and encoder functions to the format config. My encoder works, it's able to call the (csk/->kebab-case) function and convert any camelCase payloads into kebab-case maps. But for whatever reason, it doesn't want to work the other way around.

Jacob Rosenzweig01:07:01

Maybe my function is wrong, maybe my approach is wrong. Not really sure.

seancorfield02:07:42

This is why I don't recommend Luminus for beginners -- so many moving parts and so many libraries to understand... and so many opportunities for things to go wrong 😐

seancorfield02:07:20

Auto-converting JSON to keywords is generally considered a bad idea (although it is less dangerous than it used to be: since keywords were interned "forever" you could DDoS Clojure servers by just sending them lots of random JSON and filling up their keyword storage -- but now I believe it uses weak references so they can be GC'd?). Anyways, just something to be aware of.

👀 3
seancorfield02:07:31

I've never used Muuntaja. That's a Metosin library? Are you also using Reitit? There's a #reitit channel so maybe you could ask in there. I thought there was a #metosin channel but apparently not. There is a #duct channel which might be worth a try (I've never used Duct either).

seancorfield02:07:16

Part of the "problem" with the Luminus template is that there are so many layers and you kind of have to understand them all in order to start customizing stuff...

seancorfield02:07:33

There is a #luminus channel too...

Jacob Rosenzweig02:07:47

Yeah let me try reposting in #reitit. Seems like there's some activity there.

Jacob Rosenzweig02:07:43

@seancorfield what do you recommend to beginners then? Everyone I saw recommended luminus.

seancorfield02:07:47

I usually recommend beginners start with Ring + ( Compojure | Reitit ) and understand that stuff first.

3
☝️ 2