Fork me on GitHub
#specter
<
2022-06-15
>
JaimeV16:06:13

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.

rolt16:06:37

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

rolt16:06:02

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

JaimeV17:06:17

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

rolt16:06:41

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