Fork me on GitHub
#clojurescript
<
2021-10-31
>
lilactown05:10:08

has anyone solved the general problem of translating a JSON API to namespaced keywords?

West05:10:26

What JSON library are you using?

West05:10:54

They usually have that built in, but it’s not so easy to find.

lilactown05:10:09

just the browser

West05:10:22

Just so I understand, you have an API that sends you back some keywords and them to be parsed as namespaced keywords rather than just keywords. Is the namespace in the API, or added by the consumer of the API?

lilactown06:10:55

which responds with JSON, using strings for keys etc.

lilactown06:10:47

and I'd like to convert it to CLJS structures with namespaced keywords

lilactown06:10:57

the namespaces should be based on where it is in the response - i.e. "is_hidden" would become :pokemon.abilities/hidden?

phronmophobic06:10:25

https://github.com/stathissideris/spec-provider is in a similar space, but the high level api assumes that is_hidden is always the same namespaced keyword. However, there is a lower level API that collects statistics that might be part of a strategy for the general problem

emccue14:10:13

@U4YGF4NGM There is a cleverer way, i’m sure, but

emccue14:10:14

(require '[clojure.string :as string])
(defn i-get-confused-using-walk-helper [path api-response]
  (cond
    (map? api-response)
    (into {}
          (for [[k v] api-response]
            [(keyword (string/join "." path)
                      k)
             (i-get-confused-using-walk-helper
              (conj path k)
              v)]))

    (sequential? api-response)
    (mapv (partial i-get-confused-using-walk-helper path)
          api-response)

    :else
    api-response))

(defn i-get-confused-using-walk [api-response]
  (i-get-confused-using-walk-helper [] api-response))

(i-get-confused-using-walk {"pokemon" {"abilities" [{"name" "Shed Skin"}
                                                    {"name" "Wonder Guard"}]}})

emccue14:10:36

translate that to pre or post walk and it sounds like what you want

lilactown05:10:55

i'm looking for something that would make it easier to pick the namespace based on the context the string key is in

roelof11:10:57

Is there something wrong with my code

(ns learn-cljs.temp-converter
  (:require [goog.dom :as gdom]
            [goog.events :as gevents]))

(defn f->c [deg-f]
  (/ (- deg-f 32) 1.8))

(defn c->f [deg-c]
  (+ (* deg-c 1.8) 32))

(def input-target (gdom/getElement "selectInputDegreeType"))
(def temp-input (gdom/getElement "inputDegree"))
(def output-target (gdom/getElement "convertedDegree"))
(def output-unit-target (gdom/getElement "selectConversionType"))
(def display-unit-target (gdom/getElement "convertedUnit"))

(defn get-input-temp []
  (js/parseInt (.-value temp-input)))

(defn set-output-temp [temp]
  (gdom/setTextContent output-target
                       (.toFixed temp 2)))

(defn get-input-target []
  (.-value input-target))

(defn get-output-target []
 (.-value output-target))

(defn update-output [_]
    (prn "event fired")
    (cond
      (and (= \C (get-input-target))
           (= \F (get-output-target)))
      (do (set-output-temp (c->f (get-input-temp)))
          (gdom/setTextContent display-unit-target "F"))
      (and (= \F (get-input-target))
           (= \C (get-output-target)))
      (do (set-output-temp (f->c (get-input-temp)))
          (gdom/setTextContent display-unit-target "C"))
      :else
      (do (set-output-temp (get-input-temp))
          (gdom/setTextContent display-unit-target(get-input-target)))))

(gevents/listen output-unit-target "change" update-output)

roelof11:10:21

on VS Code the repl is not turning on when I open the site

p-himik11:10:26

Unless you see some code-related errors somewhere, this sounds like a tooling problem and not a CLJS problem. Assuming you're using Calva, it would make sense to ask in #calva

roelof11:10:31

oke, I do not see any problems

roelof11:10:41

Thanks, asked on the calva channel

roelof15:10:29

what do you experts think of this : https://github.com/RoelofWobben/learn-clojurescript/blob/main/temp-converter/src/learn_cljs/temp_converter.cljs The only thing I have to add is converting to and from Kelvin but that is not a very difficult task

emccue16:10:28

getElement doesn’t mutate afaik so its probably fine to just def

roelof16:10:37

oke, then I have misunderstood this in my challenge

Every time the code is reloaded, it will attach more event handlers. Use the initialization pattern discussed in Lesson 6 to ensure that the event handlers are attached only once.

roelof16:10:19

or do they mean I have to add my vegents to this :

(defonce is-initialized?
  (do                                                      ;; <1>
    (.setItem js/localStorage "init-at" (.now js/Date))
    (js/alert "Welcome!")
    true))                                                 ;; <2>

p-himik08:11:55

Assuming that's what they discussed in Lesson 6, then yes.