This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-01-23
Channels
- # announcements (1)
- # babashka (13)
- # cherry (12)
- # cider (6)
- # clj-kondo (3)
- # cljs-dev (28)
- # clojure (77)
- # clojure-europe (25)
- # clojure-nl (1)
- # clojure-norway (35)
- # clojure-uk (5)
- # clojurescript (31)
- # conjure (7)
- # cursive (12)
- # data-science (9)
- # datalevin (5)
- # datomic (8)
- # hyperfiddle (21)
- # jobs (7)
- # kaocha (19)
- # malli (26)
- # matrix (3)
- # releases (1)
- # shadow-cljs (42)
- # squint (95)
- # testing (2)
- # vim (14)
- # wasm (1)
Hey all, I'm integrating Lambda functions (AWS) and I want to convert the result (buffer) to a cljs object
(defn process-payload [res]
(->> res :Payload (.from js/Buffer) .toString))
The result may be of any type (string, map, vector...)
How can I read the string and convert it to an object?
I have tried using read-string
, but when the result is a string it just picks the first element.
(read-string "a b") ; a => should be "a b"
(read-string "[a b]") ; [a b]
(read-string "{:a :b}") ; {:a :b}
you have a wrong serialisation format. strings should be encoded as "\"a b\""
read-string
is reading serialised clojure code and "a b"
means two symbols not string
as a workaround you can try
(require '[clojure.edn :as edn])
(defn read-object [x]
(let [x' (edn/read-string x)]
(if (= x (pr-str x'))
x'
x)))
(read-object "a b")
;; => "a b"
but this will fail when payload is a big map (more than 32 keys), contains non standard formatting (`"{:a :b}"`) etc.The ion is a lambda function (clojure) and a client may be both cljs or another language (like python). How can I return a namespaced keyword map?
what do you mean?
so you want to serialise clojure data structure into a string?
(pr-str obj)
works most of the time except for lazy sequences and native java objects (with some exceptions).
you also can alter how some certain objects being serialised by adding https://clojuredocs.org/clojure.core/print-method
about namespaced keywords, pr-str
can serialise them with no problem. maybe I misunderstand what is "keep" means in that context ) Could you give an example?
I need to send it to the client.
I have ended up using json/write-str
:
(json/write-str res :key-fn str) => "{\":anchor\\/name\":\"btn\",\":anchor\\/index\":6, ...
(pr-str res) => "#:anchor{:name \"btn\", :index 6, ...
A python
client can't really use the second optiongot you, this called namespace syntax for maps. To turn it off:
(binding [*print-namespace-maps* false]
(pr-str {:foo/bar 2}))
;; => "{:foo/bar 2}"
btw, there are python libs providing edn capabilities: https://pypi.org/project/edn-format/
What is the clojuric way to dynamically get a JS field?
I know that (aget x my-field)
works, but it is supposed to be used with arrays, not object.
I used to use (gobj/get x my-field)
, but it uses GCL and i'd like to avoid it.
👀
Do you think it's worth to raise this question to #C07UQ678E and ask for a public-exposed aget
equivalent for objects? (maybe oget
)
unchecked-get
already exists and just could use an updated docstring. no on the oget
oget
could be a simple function. but OK, I will raise the docstring update
19 | (apply unchecked-get #js{:a 42} ["a"]))
-------------------------------^------------------------------------------------
Can't take value of macro cljs.core/unchecked-get
--------------------------------------------------------------------------------
unchecked-get
could use that function treatment. there is no need to add a new name to cljs.core
just for this. unchecked-get
is a perfectly fine and fitting name
this is also unchecked for a reason, as is can be quite easy to use incorrectly with :advanced
in mind
Yes. this should be used to "access fields in JSON objects", not to "access fields in object"
IDK good words to explain. But i understand that it should be used with "data", not with "code".
https://clojurians.slack.com/archives/C03S1KBA2/p1706017756172989 -- Would love feedback from CLJS devs!
React-based: • Reagent (without re-frame) • re-frame (that works on top of Reagent) • UIx • Helix Non-React: • Nothing • HTMX
Not sure if many people would be using it yet but you might want to put plain old "React" in there as Borkdude's Squint and Cherry support a https://github.com/squint-cljs/squint/tree/main/examples/vite-react.
• Electric