specter

stephenmhopper 2024-06-01T20:19:59.880529Z

I use camel-snake-kebab quite extensively in one of my applications. Some recent profiling found it to be a bit of a slow point. I'd like to rewrite it in Specter, but I'm struggling a bit. (cske/transform-keys csk/->snake_case data) What's the equivalent in Specter to walk the data structure and convert the keys to snake case? This is close to what I want, but doesn't walk the data structure:

(specter/transform
  [specter/MAP-KEYS]
  csk/->snake_case
  {:AThing {:AAThing 12
            :A2Thing {:AAAThing "x"}}}) 
Am I supposed to use specter/walker or specter/ALL somehow?

nathanmarz 2024-06-01T22:13:24.196339Z

the problem with walker is it's a blind walk that will traverse into all data structures, including records

nathanmarz 2024-06-01T22:13:48.044379Z

I find it's usually much better to have a path that's tailored to your representation

nathanmarz 2024-06-01T22:15:47.421029Z

so if the structure is always either values or maps in the value position, then you can do something like this:

(def data
  {:AThing {:AAThing 12
            :A2Thing {:AAAThing "x"}}})

(def ALL-MAPS
  (recursive-path [] p
    (continue-then-stay
      MAP-VALS map? p)))

(transform [ALL-MAPS MAP-KEYS] str data)
;; => {":AThing" {":AAThing" 12, ":A2Thing" {":AAAThing" "x"}}}