-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A function to move to top i.e. position [1,1] or a custom position in the zipper #149
Comments
For example in this config.edn file: https://github.com/gojek/ziggurat/blob/master/resources/config.test.edn, I want to change bootstrap-servers for all the maps inside so as a first step, I search for But, after this when I search for :default (the next key in the set of keys), I need a way to go to top or move to stream-routes. With current set of APIs, I'll have to write my own function to move to top or to stream-routes so that my next search works again. I hope I'm clear. Pasting the above config.edn file here:
|
Sorry for being slow to reply @mjayprateek, I've been out with a small injury. I haven't yet reviewed your issue carefully, but I think adding a I'll think about nuances and follow up when I am back to the keyboard in full capacity, which shouldn't be too long. |
Sure @lread. There's no hurry here. Please take Care. |
Hi @mjayprateek I don't know if you are still feeling a pain here. Have you had a look at docs on sub editing? I wonder if they might scratch your itch. Sub editing allows you to restrict your changes to an isolated sub-tree. Here's one way to update all (require '[rewrite-clj.zip :as z])
(-> (slurp "https://raw.githubusercontent.com/gojek/ziggurat/c4c6d813dc164c3e7b284fb63f43f9a602c0fd55/resources/config.test.edn")
z/of-string
(z/find-value z/next :stream-router)
z/right
(z/postwalk (fn [zloc]
(when (= (-> zloc z/left z/sexpr) :bootstrap-servers)
(z/replace zloc "my-new-value"))))
z/print-root) |
Hi @lread For example, we're using rewrite-clj to automatically upgrade user codebases which use Ziggurat (the library mentioned above) For this the rewriter code goes inside certain parts of a file and when it encouters a var, it tries look up its definition. At this point my zipper is deep inside some function call and I want to start looking for a var from the top. At present, the way we do is we navigate to the top and just search for all I hope that helps. Below is a sample of code we're using (might not be entirely correct but works for us.)
|
Thanks for the clarification @mjayprateek. Because you aren't navigating to the ultimate top of the zipper (the If we add such a function, what to name it? If we do add a |
Hi @lread If |
Maybe |
Maybe |
I agree, I don't think I would have chosen the The
Because of this, I'd probably not reuse |
Is doing |
Hi @mrkam2! Let's see in a REPL: (require '[rewrite-clj.zip :as z])
(def s " [1 2 3]")
(def zloc (z/of-string s))
;; we are auto-navigated to the first non-whitespace node
(z/tag zloc)
;; => :vector
(z/string zloc)
;; => "[1 2 3]"
;; but the the absolute top node is the :forms node
(z/tag (z/up zloc))
;; => :forms
(z/string (z/up zloc))
;; => " [1 2 3]"
;; so if you want to return to the first non-whitespace node, your technique should work fine:
(-> zloc
z/down z/rightmost ;; some nav
z/root
z/of-node
z/tag) ;; where are we now?
;; => :vector
;; or if you want to navigate to the topmost node you might try:
(-> zloc
z/down z/rightmost ;; some nav
z/root
z/of-node*
z/tag) ;; where are we now?
;; => :forms
|
Problem/Opportunity
At times we need to automate changes which requires us to repeatedly (inside code) search for values and make changes. Now while doing that operation on config.edn (or for any other type), after first search, once the zipper location is inside a deeply nested map, there's a problem in searching for brothers of current-location's grandfather or great-grandfather.
This is tough when you don't even know at what level (grandfather or great-grandfather or above that) is the next search item located.
Proposed Solution
There should be a method in zip API to either:
Alternative Solutions
I've written my own function to go up continuously till a certain key is found. I begin my successive searches from there on.
Additional context
Let me know if you need some concrete examples.
Action
I'm happy to contribute a PR with whatever solution the maintainers agree on.
The text was updated successfully, but these errors were encountered: