specter

JaimeV 2022-06-15T16:11:13.926799Z

I have a OpenAPI JSON file that I am trying to process. The resulting structure is not very clear to me, it looks like a nested collections and maps with no uniformity. Fortunately every node that I am interested has unique identifier. Is there a way to do a deep-search and extract the object at its place. I have tried [MAP-VALS ALL MAP-VALS key-pred] but it fails because the structure is not uniform. I trying to avoid the need to write specific navigators because I am sure I will miss a particular path.

rolt 2022-06-16T16:26:37.112109Z

if there are different levels of nesting, you can have a look at https://github.com/redplanetlabs/specter/wiki/Using-Specter-Recursively

rolt 2022-06-16T16:31:02.078119Z

disclaimer: i barely ever used specter with recursion, I'd probably use clojure.walk or clojure.zip for this kind of things

Lucy Wang 2022-07-14T02:20:56.695539Z

you need the walker navigator https://github.com/redplanetlabs/specter/wiki/List-of-Navigators#walker

JaimeV 2022-06-15T17:16:17.284629Z

Is there a better way to write this:

(defn get-element [id model]
  (let [id-match? #(= (:id %) id)]
    (or (select-one [:model :people ALL id-match?] model)
        (select-one [:model :softwareSystems ALL id-match?] model)
        (select-one [:model :softwareSystems ALL :containers ALL id-match?] model)
        (select-one [:model :softwareSystems ALL :containers ALL :components ALL id-match?] model))))

rolt 2022-06-16T16:27:41.983029Z

a single select-one with https://github.com/redplanetlabs/specter/wiki/List-of-Navigators#multi-path maybe ?

rolt 2022-06-16T16:42:41.814599Z

(select-first [:model (multi-path [:people ALL] [:softwareSystems ALL (multi-path [] [:containers ALL (multi-path [] [:components ALL])])]) #(= (:id %) id)])