redplanetlabs/specter

Need a `navigable?` operation

WhittlesJr opened this issue · 1 comments

We need a way to determine if a given specter path is "navigable." This isn't quite the same question as whether or not there is a value at the path, like selected?. Here's my example:

(ns rh.specter
  (:require  [clojure.test :as t]
             [com.rpl.specter :as sp]))


(def data
  [{:id  1
    :key "value"}
   {:id  2
    :key "other value"}])

(defn op [id data]
  (let [path [(sp/filterer [:id (sp/pred= id)]) sp/FIRST :new-key]]
    (sp/setval path "new value" data)))

(op 1 data) ;; navigable
(op 3 data) ;; not navigable

I'm trying to add a :new-key to whichever map matches the given :id. There is no value currently at :new-key that would show up with selected?, but with :id as 1, it is navigable in that setval can get there and add the "new value". However, with :id as 3, it is not navigable, and setval does nothing.

We need a way to detect the difference between these cases. I can't find anything suitable in the docs. Right now my only thought is to do the setval, then do a diff and see if anything changed.

Oh, just kidding! I think selected-any? is actually exactly what I need.