Thanks for looking into this. Apparently I did not yet express my question properly.
In short:
Given a deeply nested data structure (e.g. Json with nested objects) I want to set a leaf value without considering which parent and or sibling nodes already exist.
More concrete, given the following Json fragment (out of a configuration file):
"disks": {
"system": "/dev/xvda",
"ephemeral": "/dev/sdb",
"persistent": {
"<disk identifier>" : { "path" : "/container/mounted/volume" }
}
},
I want to add a persistent disk `my-disk` with a given path `/yet/another/volume` and I don't want to handle
- there is no node 'my-disk'
- there is no node 'persistent'
- there is no node 'disks'
Given a function
defaultsToEmpty :: Maybe (Value) -> Maybe (Value)
defaultsToEmpty Nothing = Just $ Object HashMap.empty
defaultsToEmpty m = m
I can do
json & key "disks" %~ defaultsToEmpty
& key "disks".key "persistent" %~ defaultsToEmpty
&
key "disks".key "persistent".key diskId %~ defaultsToEmptybut this doesn't look nice and was wondering if lenses can do better.