meander

All things about https://github.com/noprompt/meander Need help and no one responded? Feel free to ping @U5K8NTHEZ
2020-05-11T11:46:39.232100Z

I’m playing with Meander. It looks like a useful tool to transform arbitrary data. So now I’m trying to transform this JSON data with a deeply nested structure. There are many optional keys in the map. I wonder what’s the best way to describe this The following transforms the data recursively, but I don’t how to solve the optional keys. With & I can catch the rest, but I probably need to use this to capture optional keys

(require '[meander.strategy.epsilon :as m*])

(def rewrite-child
  (m*/rewrite 
    {"title".      !node-title
     "create-time" !create-time
     "children" !node-children

     & !others 
     }
    {:create/email !create-email
     :create/time !create-time
     :node/title !node-title
     :node/children !node-children
     
     & 
     })

(def rewrite-children
      (m*/bottom-up
       (m*/attempt rewrite-child)))    
Ideally I would like to have the following transformation:
{"create-time" 1582030610649,
 "title" "<https://en.wikipedia.org/wiki/Consciousness>",
 "children" [{"create-time" 1582030610649,
              "string" "<https://en.wikipedia.org/wiki/Consciousness>"}]}
=&gt; 
{:create/time 1582030610649,
 :node/title "<https://en.wikipedia.org/wiki/Consciousness>",
 :node/children [{:create/time 1582030610649,
                  :node/string "<https://en.wikipedia.org/wiki/Consciousness>"}]}
Now it becomes
{:create/time 1582030610649,
 :node/title "<https://en.wikipedia.org/wiki/Consciousness>",
 :node/children [{:create/time 1582030610649,
                  :node/title nil
                  :node/childeren nil}]} 
I wonder if I can get rid of the nil’s somehow. There are also more complex cases where I basically need wildcards for keys. Any suggestion how I should approach this? Is there a particular syntax for maps? Thank you :)

2020-05-12T07:10:08.236Z

Thank you. If all goes well, I'll post a link to the result later :)

jimmy 2020-05-11T17:08:30.232200Z

I'll have to do some thinking about how I'd do that. Are there certain keys that are optional and you don't want them if they aren't there? Or all the keys?

noprompt 2020-05-11T18:48:21.233300Z

Yah more detail would be helpful. I can also jump on later today and lend a hand. 👍

2020-05-11T20:07:16.233500Z

Thanks! I’m trying to import a json file. The data structure in the file has an a recursive structure of variable depth. The first level has slightly different keys than the child levels. And there are some special childs with others fields. I guess I could write multiple rewrite rules. I’m wondering what the best way would be to compose that. I’ll add a snippet of the import file to give you an idea

2020-05-11T20:09:14.233700Z

This is the file. Eventually I’m trying to put it in a Datomic database by flattening the structure, but my first concern is mapping the right fields

2020-05-11T20:10:53.234100Z

So if I could combine at least 3 rewrite rules with attempt and bottom-up it could work I guess. Maybe attempt is composable? I’ll give it a try

2020-05-11T21:09:31.234400Z

I’m try rewrite with multiple rules now, one for each map type. But it seems the first one is always chosen. I had the understanding that ? would require the match to be non-nil. I’m probably missing something. Will try more

jimmy 2020-05-11T21:11:11.234600Z

(m/some ?x) is what you want to use for requiring something to be non-nil. Will take a look at this later tonight. Sorry can't get to it earlier.

2020-05-11T21:12:42.234800Z

np, thank you!

2020-05-11T21:32:03.235Z

Ok maybe multiple rewrite rules did work

2020-05-11T21:33:58.235200Z

Now I’m wondering if I can do transformation on values. E.g. I would like to change timestamps to an instant. I could do a postwalk over the data, but maybe this is something Meander can do as well, saving another data iteration