Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: recipe for adding a key value pair to a json #4341

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

dpozinen
Copy link
Contributor

@dpozinen dpozinen commented Jul 18, 2024

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

@timtebeek timtebeek added the recipe Requested Recipe label Jul 19, 2024
@timtebeek
Copy link
Contributor

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()

timtebeek

This comment was marked as off-topic.

@timtebeek timtebeek marked this pull request as draft July 30, 2024 08:28
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • rewrite-gradle/src/main/groovy/RewriteSettings.groovy
    • lines 31-36

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • rewrite-gradle/src/main/groovy/RewriteSettings.groovy
    • lines 31-36

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • rewrite-gradle/src/main/groovy/RewriteSettings.groovy
    • lines 31-36

Copy link
Contributor

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took me a little while longer to get to this one; looking quite good already. One thing I would still like to change is the formatting used when we append to existing elements. Right now that retains the newline before the comma introduced, where I think this will look better if that comma is on the same line as the line before it. Do you see an option to work that in still @dpozinen ?

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • rewrite-gradle/src/main/groovy/RewriteSettings.groovy
    • lines 31-36

@timtebeek
Copy link
Contributor

I only just now see you had logged an issue along those exact lines; thanks again! It's not always easy to keep up. :)

@dpozinen
Copy link
Contributor Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe Requested Recipe
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

Json formatting/style doesn't seem to be respected when adding values/members
2 participants