-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Spike: Add a zipper to mutate Ast (WIP) #1148
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
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6e5055b
zipper spike
andimarek d483b39
with children index based snippet
andimarek f38ce60
Implement zipper support methods on Node concrete classes
felipe-gdr 5603191
Add test for AstZipper
felipe-gdr 93a6604
Merge pull request #1334 from felipe-gdr/zipper
andimarek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package graphql.language; | ||
|
|
||
|
|
||
| import graphql.PublicApi; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.function.Function; | ||
|
|
||
| @PublicApi | ||
| public class AstZipper { | ||
|
|
||
| private final Node curNode; | ||
| // reverse: the breadCrumbs start from curNode upwards | ||
| private final List<AstBreadcrumb> breadcrumbs; | ||
|
|
||
|
|
||
| public AstZipper(Node curNode, List<AstBreadcrumb> breadcrumbs) { | ||
| this.curNode = curNode; | ||
| this.breadcrumbs = breadcrumbs; | ||
| } | ||
|
|
||
| public static AstZipper rootZipper(Node rootNode) { | ||
| return new AstZipper(rootNode, new ArrayList<>()); | ||
| } | ||
|
|
||
| public static class AstBreadcrumb { | ||
| private final Node node; | ||
| private final Location location; | ||
|
|
||
| public AstBreadcrumb(Node node, Location location) { | ||
| this.node = node; | ||
| this.location = location; | ||
| } | ||
| } | ||
|
|
||
| public static class Location { | ||
| private final String childName; | ||
| private final int position; | ||
|
|
||
| public Location(String childName, int position) { | ||
| this.childName = childName; | ||
| this.position = position; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public AstZipper modifyNode(Function<Node, Node> transform) { | ||
| return new AstZipper(transform.apply(curNode), breadcrumbs); | ||
| } | ||
|
|
||
| public AstZipper changeLocation(Location newLocationInParent) { | ||
| // validate position | ||
| List<AstBreadcrumb> newBreadcrumbs = new ArrayList<>(breadcrumbs); | ||
| AstBreadcrumb lastBreadcrumb = newBreadcrumbs.get(newBreadcrumbs.size() - 1); | ||
| newBreadcrumbs.set(newBreadcrumbs.size() - 1, new AstBreadcrumb(lastBreadcrumb.node, newLocationInParent)); | ||
| return new AstZipper(curNode, newBreadcrumbs); | ||
| } | ||
|
|
||
| public AstZipper changeLocation(Location newLocationInParent, List<AstBreadcrumb> newBreadcrumbs) { | ||
| // validate position | ||
| return new AstZipper(curNode, newBreadcrumbs); | ||
| } | ||
|
|
||
| public AstZipper moveUp() { | ||
| return null; | ||
| } | ||
|
|
||
| public Node toRoot() { | ||
| Node curNode = this.curNode; | ||
| for (AstBreadcrumb breadcrumb : breadcrumbs) { | ||
| // just handle replace | ||
| ChildrenContainer newChildren = breadcrumb.node.getNamedChildren(); | ||
| final Node newChild = curNode; | ||
| newChildren = newChildren.transform(builder -> { | ||
| Location location = breadcrumb.location; | ||
| builder.replaceChild(location.childName, location.position, newChild); | ||
| }); | ||
| curNode = breadcrumb.node.withNewChildren(newChildren); | ||
| } | ||
| return curNode; | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package graphql.language; | ||
|
|
||
| import graphql.Assert; | ||
| import graphql.PublicApi; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Consumer; | ||
|
|
||
| @PublicApi | ||
| public class ChildrenContainer { | ||
|
|
||
| private final Map<String, List<Node>> children = new LinkedHashMap<>(); | ||
|
|
||
| private ChildrenContainer(Map<String, List<Node>> children) { | ||
| this.children.putAll(Assert.assertNotNull(children)); | ||
| } | ||
|
|
||
| public <T extends Node> List<T> getList(String key) { | ||
| return (List<T>) children.getOrDefault(key, new ArrayList<>()); | ||
| } | ||
|
|
||
| public <T extends Node> T getSingleValueOrNull(String key) { | ||
| List<? extends Node> result = children.getOrDefault(key, new ArrayList<>()); | ||
| if (result.size() > 1) { | ||
| throw new IllegalStateException("children " + key + " is not a single value"); | ||
| } | ||
| return result.size() > 0 ? (T) result.get(0) : null; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getChildOrNull might ber a better name
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, will change |
||
|
|
||
|
|
||
| public static Builder newChildrenContainer() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public ChildrenContainer transform(Consumer<Builder> builderConsumer) { | ||
| Builder builder = new Builder(this); | ||
| builderConsumer.accept(builder); | ||
| return builder.build(); | ||
| } | ||
|
|
||
| public boolean isEmpty() { | ||
| return this.children.isEmpty(); | ||
| } | ||
|
|
||
| public static class Builder { | ||
| private final Map<String, List<Node>> children = new LinkedHashMap<>(); | ||
|
|
||
| private Builder() { | ||
|
|
||
| } | ||
|
|
||
| private Builder(ChildrenContainer other) { | ||
| this.children.putAll(other.children); | ||
| } | ||
|
|
||
| public Builder child(String key, Node child) { | ||
| children.computeIfAbsent(key, (k) -> new ArrayList<>()); | ||
| children.get(key).add(child); | ||
| return this; | ||
| } | ||
|
|
||
| public Builder children(String key, List<? extends Node> children) { | ||
| this.children.computeIfAbsent(key, (k) -> new ArrayList<>()); | ||
| this.children.get(key).addAll(children); | ||
| return this; | ||
| } | ||
|
|
||
| public Builder replaceChild(String key, int index, Node newChild) { | ||
| this.children.get(key).set(index, newChild); | ||
| return this; | ||
| } | ||
|
|
||
| public ChildrenContainer build() { | ||
| return new ChildrenContainer(this.children); | ||
|
|
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getChildrenWithNameor justgetChildrenis a better namer I think. You know its a List from the return typeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still stand by this comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will change