This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-10-31
Channels
- # aleph (8)
- # announcements (11)
- # aws (1)
- # babashka (7)
- # beginners (104)
- # calva (52)
- # clara (1)
- # clj-kondo (28)
- # cljdoc (8)
- # cljsrn (2)
- # clojure (20)
- # clojure-europe (8)
- # clojure-uk (1)
- # clojurescript (26)
- # core-typed (3)
- # datomic (6)
- # holy-lambda (1)
- # jobs (1)
- # jobs-discuss (14)
- # malli (7)
- # pathom (31)
- # polylith (19)
- # re-frame (8)
- # reitit (1)
- # releases (1)
- # shadow-cljs (5)
- # tools-build (92)
has anyone solved the general problem of translating a JSON API to namespaced keywords?
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?
Also might be relevant: https://purelyfunctional.tv/mini-guide/json-serialization-api-clojure/
I'm looking to use an API like https://pokeapi.co/docs/v2#pokemon
the namespaces should be based on where it is in the response - i.e. "is_hidden"
would become :pokemon.abilities/hidden?
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
@U4YGF4NGM There is a cleverer way, i’m sure, but
(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"}]}})
i'm looking for something that would make it easier to pick the namespace based on the context the string key is in
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)
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
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
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.