specter

Latest version: 1.1.3
richiardiandrea 2021-05-03T18:44:37.023700Z

Hi folks again 😄 How would I go if I wanted to set a key in a map that is missing? For instance I only want to add :foo nil if it's not already there

richiardiandrea 2021-05-03T19:03:25.024Z

this is what I have come up so far:

(defn assoc-mode-nils
  "Add nil to empty modes."
  [root]
  ;; Please do not remove this or you will break spec and frontend.
  (sp/transform
   [maps/POST-ORDER-TREE-VALS
    (sp/must :descriptor)
    (sp/pred #(= (:type %) enum/segmod-element-type-heart-lv-subsegment))
    (sp/multi-path [(sp/keypath (keyword cmr.scoring/mode-function)) (sp/nil->val nil)]
                   [(sp/keypath (keyword cmr.scoring/mode-edema)) (sp/nil->val nil)])]
   identity
   root))

2021-05-03T21:03:15.024800Z

(s/setval [(s/cond-path (s/not-selected? (s/map-key :a)) (s/keypath :a))] 2 {:a nil :b 4})
=> {:a nil, :b 4}
(s/setval [(s/cond-path (s/not-selected? (s/map-key :a)) (s/keypath :a))] 2 {:a 10 :b 4})
=> {:a 10, :b 4}
(s/setval [(s/cond-path (s/not-selected? (s/map-key :a)) (s/keypath :a))] 2 {:b 4})
=> {:b 4, :a 2}

2021-05-03T21:03:19.025Z

not sure if there’s a better way