borkdude / rewrite-edn

Utility lib on top of rewrite-clj with common operations to update EDN while preserving whitespace and comments.
Eclipse Public License 1.0
84 stars 14 forks source link

Add operations for manipulating list/vec? e.g. conj #31

Closed zerg000000 closed 1 year ago

zerg000000 commented 1 year ago

When we dealing with edn, we will always encounter updating list/vector. It would be good to have some ops for that.

Since conj is not always worked as expected. Don't know what would be the best to add to the api.

borkdude commented 1 year ago

@zerg000000 I think it's usually best to take a few examples into account so we can see how it should work.

zerg000000 commented 1 year ago

For example, I want to append :create-friends-table to the end of vector

{:migrations [:init-db
              :create-user-table
              :create-role-tables]}

In rewrite-edn, can i do something like?

(require '[rewrite-clj.zip :as z])

(-> (r/parse-string edn-string)
    (r/update :migrations (fn [vals] (z/append-child vals :create-friends-table)))

or

(-> (r/parse-string edn-string)
    (r/update :migrations (fn [vals] (r/append-child vals :create-friends-table)))
borkdude commented 1 year ago

@zerg000000 Ah yes, this works:

user=>        (def nodes (r/parse-string "{:a [1 2 3]}"))
#'user/nodes
user=>        (str (r/update nodes :a (fn [vals] (z/root (z/append-child (z/edn vals) 4)))))
"{:a [1 2 3 4]}"
zerg000000 commented 1 year ago

That's nice. I can live with this, any plan make this ops as part of rewrite-edn api?

borkdude commented 1 year ago

I guess we could introduce conj but this would raise the expectation that it would work for list, vector, maps and sets and behave differently for each. The most frequent use case would be vector probably.

borkdude commented 1 year ago

Yeah, let's do conj.

borkdude commented 1 year ago

It might take me a while to add so if someone is willing to do a PR, welcome.