This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-03-09
Channels
- # aws (51)
- # beginners (57)
- # calva (10)
- # chlorine-clover (7)
- # cider (20)
- # clj-kondo (55)
- # clojure (43)
- # clojure-europe (9)
- # clojure-italy (1)
- # clojure-nl (5)
- # clojure-spec (8)
- # clojure-uk (71)
- # clojurescript (33)
- # core-async (22)
- # cursive (20)
- # datomic (3)
- # emacs (8)
- # figwheel-main (8)
- # fulcro (13)
- # garden (2)
- # graalvm (60)
- # graphql (26)
- # jobs (6)
- # joker (6)
- # kaocha (2)
- # lambdaisland (5)
- # malli (36)
- # off-topic (9)
- # portkey (15)
- # re-frame (3)
- # reagent (25)
- # remote-jobs (4)
- # spacemacs (3)
- # sql (111)
- # tree-sitter (29)
- # uncomplicate (3)
- # xtdb (2)
I’m using ring-jetty to build an API and I can’t respond with an integer in the body
java.lang.IllegalArgumentException: No implementation of method: :write-body-to-stream of protocol: #'ring.core.protocols/StreamableResponseBody found for class: java.lang.Integer
my ns
1 (ns myapi.core
2 (:gen-class)
3 (:require
4 ¦ [myapi.graphql :as graphql]
5 ¦ [myapi.db :as db]
6 ¦ [reitit.ring :as ring]
7 ¦ [ring.adapter.jetty :as jetty]
8 ¦ [ring.middleware.reload :refer [wrap-reload]]
9 ¦ [jsonista.core :as json]
10 ¦ [ring.middleware.params :refer [wrap-params]]
11 ¦ [ring.middleware.keyword-params :refer [wrap-keyword-params]]
-- 12 ¦ [raven-clj.core :refer [capture]]
-- 13 ¦ [raven-clj.ring :refer [wrap-sentry]]
14 ¦ [reitit.ring.coercion :as rrc]
-- 15 ¦ [reitit.ring.middleware.exception :as exception]
16 ¦ [schema.core :as s]
17 ¦ [reitit.coercion.schema]))
and the handler
(defn handler
[request]
(let [params (:params request)
customer-uuid (:customer_uuid params)
debtor-uuid (:debtor_uuid params)
limit (trust-limit customer-uuid
(n-complete-invoices! customer-uuid debtor-uuid))]
{:status 200
:body (int limit)}))
am I missing some middleware? responding with a string worksjava.lang.IllegalArgumentException: No implementation of method: :write-body-to-stream of protocol: #'ring.core.protocols/StreamableResponseBody found for class: clojure.lang.PersistentArrayMap
maybe it needs to be encoded first into json somehow? it should be possible to respond to a GET request with just a number though, right?you could add muuntaja.middleware/wrap-format
for example to get content negotiation and auto-encoding of response body
if you use reitit, there are some examples which have basic set of middleware pre-configured, like: https://github.com/metosin/reitit/tree/master/examples/ring-swagger
Hi everyone
I have a (probably very simple) question
How can I use async/mult
asynchronize?
Would the clojuredocs example answer that? https://clojuredocs.org/clojure.core.async/mult
Do you mean async input or output? You can make the input channel a buffer (https://clojuredocs.org/clojure.core.async/buffer) which would buffer to everyone.
hello all, how to convert this {"service" {"lj" {}}, "setup" {"interval" "5"}}
to hash-map with keyword?
(defn keywordize [m]
(if (map? m)
(reduce-kv
(fn [acc k v]
(assoc acc (keyword k) (keywordize v)))
{}
m)
m))
something like this mb?
or clojure.walk/keywordize-keys
then
(defn keywordize [m]
(cond
(map? m)
(reduce-kv
(fn [acc k v]
(assoc acc (keyword k) (keywordize v)))
{}
m)
(instance? java.util.Hashtable m)
(reduce
(fn [acc k]
(assoc acc (keyword k) (keywordize (.get m k))))
{}
(.keySet m))
:else
m))
this should work for Hashtable tooI tried with keywordize-keys
but the result is only with one level, like service
and setup
, not with interval
or lj
if you have a function like keywordize-keys:
(clojure.walk/postwalk #(if (map? %) (keywordize-keys %) %) m)
clojure.walk/keywordize-keys
already does recursive walk
@delaguardo even if it is hashtable instead of hashmap?
what is hashtable?
no, my versions of keywordize will not work for Hashtable
it's going to be basically impossible to support all arbitrary java data structures. I would convert anything you get into a clojure data structure at the edge if possible
yes, converting the java Hashtable into a clojure map first would make keywordize-keys work
this is one way, or you could implement clojure.lang.IPersistentMap interface for Hastable class
there is no way to do that
I would be wary of making a change that changes how clojure/java interact for the whole system in order to make something work in one case
there is no way to do that
I just figured that out (
this is what I did
(defn key-string->keyword [config]
(into {}
(for [[k v] config]
(do
(if (= (type v) java.util.Hashtable)
[(keyword k) (key-string->keyword v)]
[(keyword k) v])))))
{"service" {"lj" {}}, "setup" {"interval" "5"}}
-> {:service {:lj {}}, :setup {:interval 5}}
@ghadi no, it´s other system that resturns from configuration file a java.util.Hashtable
that'll work. fwiw I would've done:
(walk/keywordize-keys (walk/postwalk #(if (and (not (map? %)) (instance? java.util.Map %)) (into {} %) %) m))
Try this
weird. does for me
user=> (def m {"service" (java.util.Hashtable. {"lj" {}}), "setup" (java.util.Hashtable. {"interval" "5"})})
#'user/m
user=> (walk/keywordize-keys (walk/postwalk #(if (and (not (map? %)) (instance? java.util.Map %)) (into {} %) %) m))
{:service {:lj {}}, :setup {:interval "5"}}
I have an api that expects a clojure.spec.alpha/coll-of string? however when there's a single value being sent it treats it as only a string and says it does not does not satisfy coll? What's the best way to ensure that a single value still gets treated as a single value collection?
hi! I’m just starting with clojure and looking for some resources/sites with some exercises to practice clojure and preferebly also goes into some testing. any tips?
http://www.4clojure.com/ is an easy starting point as it's web-based but personally I tend to recommend https://github.com/functional-koans/clojure-koans more as it encourages one to get familiar with running Clojure and using and editor/IDE.
Also, if you've not already found it, http://clojuredocs.org/ is a good community-driven function reference - it's particularly useful as it provides practical examples of usage for most of the functions.
Exercism and Codewars are both test oriented challenges. Exercism has lots of nice challenges, not too difficult, so you can focus on the language. Codewars seems to be more about mathematics the further you progress. I have published videos of solving some of the challenges from those sites and about 60 of the 4clojure challenges https://practicalli.github.io/