Fork me on GitHub
#meander
<
2024-02-13
>
Tommi Martin10:02:03

Could someone point me in the right direction with Meander as I try to learn it. Is there a way of matching both map and sub map data in a manner that returns a combined result? Essentially I have a case of

{:important-key1 "test"
 :important-key2 "test2"
 :useless-key1 ""
 :useless-map {}
 :subservices {:dynamic-name1 {:important-key ""
                               :useless-key2 ""
               :dynamic-name2 {:important-key ""
                               :useless-key2 ""}}}}
Actual data is a lot more complex than that. But the structure is the same. I would like to cherry pick the important keys from the data using meander, in the example given it would just drop the useless keys and retain other things. Am I going the wrong way for trying to figure out a meander query that I can use that returns everything with a single match that I wouldn't have to merge the results of meander outside of it? The real use-case would take similar data to the above one, but create several new keys to the root based on the dynamic-name1 and 2. But one step at a time. Am I going wrong with Meander here?

Jimmy Miller23:02:45

Not solving your exact case here. But hopefully something useful in this post. https://jimmyhmiller.github.io/meander-practical If you are wanting one result m/rewrite is probably best to focus on. On mobile so sadly can’t write an example.

👀 1
👌 1
Tommi Martin08:02:06

Thank you for the examples there, I wasn't able to get the system quite where I wanted it to be yet but I am getting close with rewrite. It's slow going and needs more input adjustment. This might take me a hot minute to learn. Currently the challenge holding me back is the questions: how to iterate over a map of objects indexed by name with rewrites. Or how to manipulate rewrite's [{:important-key ?key} ...] syntax to return a value indexed map instead of a vector. Definately bit more than my skills could handle when I decided to try meander. I'll get back to you if I figure out a way of doing the above or make a workaround. Edit: My current out is a vector of subservices without key indexes. This isn't enough because I need to merge values from another datasource into the subservices by their name but it's closer.

Tommi Martin10:02:27

Cracked it, here is the final result that allows me to work with the data in a way I wanted to.:

(meander/rewrite
   (mu/decode "application/json" (slurp "test/app/fixtures/simple.json"))
   {:payload {:name ?name
              :composite ?composite
              :type ?type
              :datasources [{:name (meander/and !name !index) :replicator {:version !version}} ...]}}
   {(meander/keyword ?name) {:name ?name
                             :composite ?composite
                             :type ?type
                             :datasources {& ([(meander/keyword !index) {:name !name :replicator {:version !version}}] ...)}}})
With an input of payload object with a datasources being a vector it spits out a name indexed map of the sub services. It's not the ideal solution since the input needs to be a map with a vector instead of a map with a submap. But at this point I'll take my victory and call it for this round. I can make it work. The solution to my problems is in how the datasources lines are being manipulated and rewritten.