Fork me on GitHub
#specter
<
2017-12-04
>
nathanmarz00:12:32

you're misunderstanding cond-path and keypath

nathanmarz00:12:39

a condition is true if it selects anything

nathanmarz00:12:55

keypath always selects something, so it's pointless to be a condition on its own

nathanmarz00:12:59

you're also trying to navigate the maps and find the field in the submap at the same time, which isn't the right approach

nathanmarz00:12:45

instead do something like: (def NODES (recursive-path [] p (continue-then-stay :subgroups ALL)))

nathanmarz00:12:28

then from there you can do (transform [NODES :fields ...

eoliphant01:12:59

hmm ok, will give that a shot, yeah i’d just tossed in keypath in trying to figure out why it wasn’t working. originally it was just

(sp/cond-path
  :group/subgroups [ALL p]
but ok i think I see understand it a little better will break it out

eoliphant03:12:31

ok. so after banging my head for a bit because i’d mistyped the key in NODES function lol, I’ve got everything working, needs some cleanup, but basically this:

(sp/setval [:designer/cur-form-def
            :form/groups
            sp/ALL
            NODES
            :group/fields
            #(some? %)
            (before-index-dynamic #(= (:field/id %) #uuid "b9a2c9ea-2647-48d3-ab26-4c2a115d7b6f"))

            ]
           [{:new :field}]
           appdb)
Will add {:new field} before the field with the matching id. so good there. However, had to do a bit of hacking because not all :group/* maps have group/fields, This was initially blowing up the before-index.. function as specter would dutifully provide a nil. So I added the some? check. That works, in terms of the field getting inserted, though I’m sure it’s not the best way, but the other thing is that the newly returned data structure adds a {:group/fields nil} to groups that didn’t have one, which surprised me as I thought the some? check would prevent that. So I’m thinking the recursive path function should weed out groups that don’t have :group/fields?

nathanmarz12:12:12

@eoliphant ah you just need before-index-dynamic to navigate nowhere if there's no match

nathanmarz12:12:17

this does the trick:

(defn ^:direct-nav before-index-dynamic [p]
  (path
    (srange-dynamic
      (fn [aseq]
        (if-let [i (first (keep-indexed (fn [i e] (if (p e) i)) aseq))]
          i
          (count aseq)
          ))
      (end-fn [aseq s]
        (if (= s (count aseq)) s (inc s))))
    (complement empty?)
    BEFORE-ELEM
    ))

nathanmarz12:12:04

if you only want to navigate to a value in a map if the key exists, then do (must :somekey)

eoliphant14:12:40

ah yeah. ok so even in my example just (must :group/fields), obviated the need for the (some? ..) and gave the proper result, will mess around with the path function as well to see what makes more sense