jrudolph/json-lenses

Support field renames and field mappers?

agemooij opened this issue · 2 comments

I'm just getting started using this library and I'm running into a requirement to support both field renames and field mappers.

One step further would be to be able to completely map a field in both its name and its value (including its type), i.e. being able to write a simple conversion from {foo: 42) to {bar: {baz: 42}} (so using the old value but wrapping it in some new structure with a new key)

The way I currently implement these cases is by using a combination of an extract and a set. Of course this does mean that the old field will still be present.

Do you think it is possible to implement this with the current codebase or would it require a major redesign? I haven't really dived into the implementation yet but if you would give some hints I might be able to come up with a pull request.

I think that's a good idea.

Currently, the field lens is able to focus on a field's value. IIUC what you propose is to focus on the name/value pair and extract / update both together. The basic obstacle in the way of just implementing it is that the library currently only supports focusing on a JsValue which the name/value pair wouldn't be.

So, in the current scheme you probably fallback on wrapping the single key/value pair inside of an JsObject and operate on that. I would welcome a PR that would work like that for the time being. You can take the code of field as a starting point and generalize from there. One question to answer is how to deal with a new value which can be any JsValue (in the current scheme) and not just a JsObject with one field.

That said, some time ago I already realized that specializing on JsValue as the focus type of the lenses here isn't useful. I just pushed the branch where I tried to break the types up and make it more general as a preview. When I find more time I'll work on a new version which expands the scope a bit to simplify things like this one.

Interesting. I'll definitively have a look.