@nathanmarz ironically enough I needed to also write a version of the macro I was struggling with over the weekend that does just stack the lambda bindings at the top of the AST and replaces all instances of corresponding locals. I did this as before by calling setval
from inside transform
, which I get the sense is not an ideal way to use Specter, but don't fully understand how you rewrote it to use transformed
inside the navigator.
@sophiago it'll be easier to understand the navigators if you play with them on toy examples
True. I tell myself I'm testing these on the simplest functions, but they're still macros doing fairly heavy source transformation. It seems clear the issue here is that I'm trying to apply transformed
directly to a symbol, and only the first one that matches as well, rather than in example you gave where it's one level up from the node in a zipper.
I suppose it's just the combination of learning what's essentially a whole DSL and wanting to use it immediately given how well it fits so many of my problems. For example, it would seem calling setval
from inside transform
as in the first example shouldn't cause a significant hit in perf, but I'd have to actually look at your macros or at least reread the wiki page about caching to make that judgment for sure. At least your docs are quite good π
hello guys, I am new to specter library. does anyone of you know which function is used to navigate the entire data structure tree and manipulate specific key
without providing a path to the key
@huthayfa.ainqawi that's a little vague, can you give a minimal example of what you're working with and what you're trying to get to?
{ "key": "hsq.contentjsonsample.sampleKey", "value": { "links":[{"text":"anytext","url":"http://nowhere.com","precedence":"1"},{"text":"anytext","url":"http://nowhere.com","precedence":"2"}], "misc":[{"key":"keyName","value":"valuessssssss"},{"key":"keyName","value":"valuessssssss"}], "assets":[{"name":"facebooklogo","location":"/s3/cmb/facebooklogo.png","type":"image"},{"name":"mypdf","location":"/pdfLocation","type":"pdf"}], }, "language":"EN", "shortName": "my Sample Json key", "description": "my Sample Json key explain why this key" }
wherever there is assets key, I want to manipulate it
I suspect you want https://github.com/nathanmarz/specter/wiki/List-of-Navigators#walker
that said, walker
is generally something I consider a "I have no idea what data's coming in, and I have no control over the format, and I'm ok with accidentally manipulating other keys that just happen to be called that", which is pretty heavy handed & crude
so if you can narrow your path down to more (like, assets
will always be inside a map stored at links
, or something) it can help to prevent random bugs
you donβt need specter for this, you can use update
from core: (update your-map :assets update-fn)
@schmee update
will only do it if you are directly manipulating a map, not if you need to find a map nested in a bigger data structure
@schmee does update do a nested map search
(or multiple maps nested in a bigger structure)
well, you need to specify what this larger data structure looks like in order to get a sensible answer
I don't know where the assets happen in the structure, maybe at second or third level
it depends on the jsonSchema
@huthayfa.ainqawi this code will run on every nested assets key with a walk that descends into all collections and map values (but not map keys):
(def NESTED-MAPS
(recursive-path [] p
(cond-path map? (continue-then-stay MAP-VALS p)
coll? [ALL p]
)))
(transform [NESTED-MAPS (must "assets")] my-transform-fn data)
so the value returned from the NESTED-MAPS will replace the current value, right ?
oh sorry, I didn't see my-transform-fn
@nathanmarz thank you so much