This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-08-14
Channels
- # aleph (1)
- # announcements (1)
- # beginners (59)
- # boot (2)
- # calva (5)
- # cider (8)
- # clj-kondo (6)
- # cljdoc (5)
- # cljsrn (11)
- # clojure (123)
- # clojure-dusseldorf (1)
- # clojure-europe (4)
- # clojure-italy (22)
- # clojure-losangeles (4)
- # clojure-nl (10)
- # clojure-spec (18)
- # clojure-uk (22)
- # clojurescript (103)
- # cursive (32)
- # data-science (1)
- # datomic (21)
- # events (2)
- # figwheel (1)
- # fulcro (12)
- # graalvm (3)
- # graphql (8)
- # jobs (2)
- # kaocha (4)
- # klipse (2)
- # lein-figwheel (4)
- # leiningen (23)
- # off-topic (11)
- # planck (11)
- # re-frame (8)
- # reagent (2)
- # reitit (3)
- # rewrite-clj (1)
- # ring (1)
- # ring-swagger (31)
- # schema (2)
- # shadow-cljs (66)
- # spacemacs (3)
- # specter (16)
- # sql (9)
- # tools-deps (16)
- # vim (26)
@joefromct there is a guide in https://github.com/metosin/muuntaja/blob/master/doc/Creating-new-formats.md for adding new formats.
thanks for that, i think i have something working. Hereās what i have so far for adding xml as a new format to a muuntaja instance:
i basically stole most of that from [here](https://github.com/metosin/muuntaja/blob/d69dfbb38f9fd6ab956d5638596433fd727f2f81/modules/muuntaja-yaml/src/muuntaja/format/yaml.clj#L1)
just a string at the moment, but iāll have specs to make sure itās well-formed xml, i donāt have a schema at the moment so maybe i can plug that in later too
i was unsure on line 10 for the (slurp data) bit but i donāt know if there is a better way to do it⦠surprised everyone else is lucky enough to not be using xml š
yeah i was tinkering with that too, as per here https://github.com/joefromct/clj-xmltojson
haha. using that in some legacy, but using a custom endpoint to read that. not as a first class format
oh, itās private. last modified 4years ago. kinda horrible, that clj-xmltojson must be better.
(declare xml->clj)
(defn- attr-name [k] (keyword (str "#" (name k))))
(defn- decorate-attrs [m] (zipmap (map attr-name (keys m)) (vals m)))
(defn- merge-to-vector [m1 m2] (merge-with #(flatten [%1 %2]) m1 m2))
(defn- childs? [v] (map? (first v)))
(defn- lift-text-nodes [m] (if (= (keys m) [:##text]) (val (first m)) m))
(defn- parts [{:keys [attrs content]}]
(merge {} (decorate-attrs attrs)
(if (childs? content)
(reduce merge-to-vector (map xml->clj content))
(hash-map :##text (first content)))))
(defn xml->clj
([xml] (hash-map (:tag xml) (-> xml parts lift-text-nodes)))
([xml schema]
(xmls/->clj (xml->clj xml) schema)))
well⦠no promises there. I think the idea is sound, but i have a nasty postwalk iāve been thinking about trying to remove somehow for performance reasons. There is this clever bit of the python equivelent library that has a concept of āforce listā for single-entry xml elementsā¦. to force to a vector. The only way i could add the āforce listā functionality was with a postwalk
basically, xml like this: <xml> <planes> <plane>jumbo</plane> </planes> </xml> you could pass a set into clj-xmltojson that would yield a vector of length one for the ājumbo planeā
thus, forcing it to a list ⦠when you donāt have an xsd and you need to make sure planes is always a list. This would be āinferredā to be a list: <xml> <planes> <plane>jumbo</plane> <plane>f35</plane> </planes> </xml>
Yes, when they are deliberately passed in via the force-list parameter (set) itās pretty nasty code, i should find time to rewrite it and update the readme. embarrassed that somebody is actually looking at atm.