feat: recipe for adding a key value pair to a json
What's changed?
Added recipe to add a new key and value to a json.
What's your motivation?
Currently, we can rename keys and change values, but can't add new entries to a json
- Fixes https://github.com/openrewrite/rewrite/issues/4246
Thanks for the work here @dpozinen ! I'll review after I come back home Sunday. Also, it seems like the automated suggestions broke compilation after being applied
> Task :rewrite-json:compileJava
/home/runner/work/rewrite/rewrite/rewrite-json/src/main/java/org/openrewrite/json/AddKeyValue.java:74: error: cannot find symbol
JsonPathMatcher pathMatcher = new JsonPathMatcher(keyPath);
^
symbol: class JsonPathMatcher
location: class AddKeyValue
/home/runner/work/rewrite/rewrite/rewrite-json/src/main/java/org/openrewrite/json/AddKeyValue.java:74: error: cannot find symbol
JsonPathMatcher pathMatcher = new JsonPathMatcher(keyPath);
^
symbol: class JsonPathMatcher
location: class AddKeyValue
/home/runner/work/rewrite/rewrite/rewrite-json/src/main/java/org/openrewrite/json/AddKeyValue.java:76: error: cannot find symbol
return new JsonIsoVisitor<ExecutionContext>() {
^
symbol: class JsonIsoVisitor
location: class AddKeyValue
/home/runner/work/rewrite/rewrite/rewrite-json/src/main/java/org/openrewrite/json/AddKeyValue.java:78: error: method does not override or implement a method from a supertype
@Override
^
/home/runner/work/rewrite/rewrite/rewrite-json/src/main/java/org/openrewrite/json/AddKeyValue.java:80: error: cannot find symbol
obj = super.visitObject(obj, ctx);
^
symbol: method visitObject(JsonObject,ExecutionContext)
/home/runner/work/rewrite/rewrite/rewrite-json/src/main/java/org/openrewrite/json/AddKeyValue.java:82: error: cannot find symbol
if (pathMatcher.matches(getCursor()) && objectDoesNotContainKey(obj, key)) {
^
symbol: method getCursor()
I only just now see you had logged an issue along those exact lines; thanks again! It's not always easy to keep up. :)
- https://github.com/openrewrite/rewrite/issues/4246
I only just now see you had logged an issue along those exact lines; thanks again! It's not always easy to keep up. :)
Hi, yeah, I'm not entirely sure where to start with this issue, do I need to create a new style? I'll see when I have the time to get more in depth into how rewrite writes the LST, because the comma isn't part of spacing/padding of the element and is added somewhere internally
I also see you made some changes to the logic namely here. Not sure why when appending (prepend == false) to a non empty json (jsonIsEmpty == false) we are using SINGLE_SPACE instead of the space of the first element of that json?
Thanks for the contribution @dpozinen. I have taken your code, added the formatting detection logic and packaged that into
- #4916.