Fork me on GitHub
#clojurescript
<
2024-01-23
>
itaied10:01:25

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}

delaguardo10:01:00

you have a wrong serialisation format. strings should be encoded as "\"a b\""

delaguardo10:01:53

read-string is reading serialised clojure code and "a b" means two symbols not string

delaguardo10:01:13

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.

🙌 1
itaied14:01:34

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?

delaguardo14:01:15

what do you mean?

itaied14:01:25

how should I serialize the datomic response? I want to keep the namespaced keywords

delaguardo14:01:06

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?

itaied15:01:39

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 option

delaguardo15:01:04

got you, this called namespace syntax for maps. To turn it off:

(binding [*print-namespace-maps* false]
  (pr-str {:foo/bar 2}))
;; => "{:foo/bar 2}"

delaguardo15:01:31

btw, there are python libs providing edn capabilities: https://pypi.org/project/edn-format/

itaied15:01:09

oh cool, thanks!

souenzzo12:01:30

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.

thheller12:01:25

unchecked-get and unchecked-set

souenzzo12:01:07

This one?

(core/defmacro unchecked-get
  "INTERNAL. ...

souenzzo12:01:33

👀 Do you think it's worth to raise this question to #C07UQ678E and ask for a public-exposed aget equivalent for objects? (maybe oget )

thheller12:01:10

unchecked-get already exists and just could use an updated docstring. no on the oget

souenzzo13:01:30

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
--------------------------------------------------------------------------------

thheller13:01:24

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

👌 1
thheller13:01:19

this is also unchecked for a reason, as is can be quite easy to use incorrectly with :advanced in mind

souenzzo13:01:39

Yes. this should be used to "access fields in JSON objects", not to "access fields in object"

souenzzo13:01:11

IDK good words to explain. But i understand that it should be used with "data", not with "code".

👍 1
p-himik13:01:36

React-based: • Reagent (without re-frame) • re-frame (that works on top of Reagent) • UIx • Helix Non-React: • Nothing • HTMX

👍 2
1
Chris McCormick14:01:03

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.

👍 1
zeitstein15:01:38

Don't know about popular, but definitely 'promising' in my opinion: #C03NTBBSF46.

thheller16:01:03

psst thats a secret 😉

rolt16:01:45

• fulcro (full stack though) - also in the react based frameworks

hifumi12319:01:56

also uix and uix2 are quite different, and i know some people intentionally using v1 over v2 for their projects