Skip to content

Commit

Permalink
fix pipeline formatting
Browse files Browse the repository at this point in the history
add some internal recipes for upgrade testing
  • Loading branch information
evanchooly committed Jan 5, 2025
1 parent 84a5e9e commit c4f97cc
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 119 deletions.
117 changes: 58 additions & 59 deletions rewrite/src/main/java/dev/morphia/rewrite/recipes/PipelineRewrite.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,62 +32,62 @@ public class PipelineRewrite extends Recipe {
public static final MethodMatcher PIPELINE = new MethodMatcher(AGGREGATION + " pipeline(..)");

static final List<MethodMatcher> matchers = List.of(
new MethodMatcher(AGGREGATION + " addFields(dev.morphia.aggregation.stages.AddFields)"),
new MethodMatcher(AGGREGATION + " autoBucket(dev.morphia.aggregation.stages.AutoBucket)"),
new MethodMatcher(AGGREGATION + " bucket(dev.morphia.aggregation.stages.Bucket)"),
new MethodMatcher(AGGREGATION + " changeStream()"),
new MethodMatcher(AGGREGATION + " changeStream(dev.morphia.aggregation.stages.ChangeStream)"),
new MethodMatcher(AGGREGATION + " collStats(dev.morphia.aggregation.stages.CollectionStats)"),
new MethodMatcher(AGGREGATION + " count(dev.morphia.aggregation.stages.Count)"),
new MethodMatcher(AGGREGATION + " currentOp(dev.morphia.aggregation.stages.CountOp)"),
new MethodMatcher(AGGREGATION + " densify(dev.morphia.aggregation.stages.Densify)"),
new MethodMatcher(AGGREGATION + " documents(dev.morphia.aggregation.expressions.impls.DocumentExpression)"),
new MethodMatcher(AGGREGATION + " facet(dev.morphia.aggregation.stages.Facet)"),
new MethodMatcher(AGGREGATION + " fill(dev.morphia.aggregation.stages.Fill)"),
new MethodMatcher(AGGREGATION + " geoNear(dev.morphia.aggregation.stages.GeoNear)"),
new MethodMatcher(AGGREGATION + " graphLookup(dev.morphia.aggregation.stages.GraphLookup)"),
new MethodMatcher(AGGREGATION + " group(dev.morphia.aggregation.stages.Group)"),
new MethodMatcher(AGGREGATION + " indexStats(dev.morphia.aggregation.stages.IndexStats)"),
new MethodMatcher(AGGREGATION + " limit(long)"),
new MethodMatcher(AGGREGATION + " lookup(dev.morphia.aggregation.stages.Lookup)"),
new MethodMatcher(AGGREGATION + " match(dev.morphia.query.filters.Filter...)"),
new MethodMatcher(AGGREGATION + " planCacheStats()"),
new MethodMatcher(AGGREGATION + " project(dev.morphia.aggregation.stages.Projection)"),
new MethodMatcher(AGGREGATION + " redact(dev.morphia.aggregation.stages.Redact)"),
new MethodMatcher(AGGREGATION + " replaceRoot(dev.morphia.aggregation.stages.ReplaceRoot)"),
new MethodMatcher(AGGREGATION + " replaceWith(dev.morphia.aggregation.stages.ReplaceWith)"),
new MethodMatcher(AGGREGATION + " sample(dev.morphia.aggregation.stages.Sample)"),
new MethodMatcher(AGGREGATION + " set(dev.morphia.aggregation.stages.Set)"),
new MethodMatcher(AGGREGATION + " skip(long)"),
new MethodMatcher(AGGREGATION + " sort(dev.morphia.aggregation.stages.Sort)"),
new MethodMatcher(AGGREGATION + " sortByCount(dev.morphia.aggregation.stages.SortByCount)"),
new MethodMatcher(AGGREGATION + " unionWith(Class,Stage...)"),
new MethodMatcher(AGGREGATION + " unionWith(String,Stage...)"),
new MethodMatcher(AGGREGATION + " unset(dev.morphia.aggregation.stages.Unset)"),
new MethodMatcher(AGGREGATION + " unwind(dev.morphia.aggregation.stages.Unwind)"));
new MethodMatcher(AGGREGATION + " addFields(dev.morphia.aggregation.stages.AddFields)"),
new MethodMatcher(AGGREGATION + " autoBucket(dev.morphia.aggregation.stages.AutoBucket)"),
new MethodMatcher(AGGREGATION + " bucket(dev.morphia.aggregation.stages.Bucket)"),
new MethodMatcher(AGGREGATION + " changeStream()"),
new MethodMatcher(AGGREGATION + " changeStream(dev.morphia.aggregation.stages.ChangeStream)"),
new MethodMatcher(AGGREGATION + " collStats(dev.morphia.aggregation.stages.CollectionStats)"),
new MethodMatcher(AGGREGATION + " count(dev.morphia.aggregation.stages.Count)"),
new MethodMatcher(AGGREGATION + " currentOp(dev.morphia.aggregation.stages.CountOp)"),
new MethodMatcher(AGGREGATION + " densify(dev.morphia.aggregation.stages.Densify)"),
new MethodMatcher(AGGREGATION + " documents(dev.morphia.aggregation.expressions.impls.DocumentExpression)"),
new MethodMatcher(AGGREGATION + " facet(dev.morphia.aggregation.stages.Facet)"),
new MethodMatcher(AGGREGATION + " fill(dev.morphia.aggregation.stages.Fill)"),
new MethodMatcher(AGGREGATION + " geoNear(dev.morphia.aggregation.stages.GeoNear)"),
new MethodMatcher(AGGREGATION + " graphLookup(dev.morphia.aggregation.stages.GraphLookup)"),
new MethodMatcher(AGGREGATION + " group(dev.morphia.aggregation.stages.Group)"),
new MethodMatcher(AGGREGATION + " indexStats(dev.morphia.aggregation.stages.IndexStats)"),
new MethodMatcher(AGGREGATION + " limit(long)"),
new MethodMatcher(AGGREGATION + " lookup(dev.morphia.aggregation.stages.Lookup)"),
new MethodMatcher(AGGREGATION + " match(dev.morphia.query.filters.Filter...)"),
new MethodMatcher(AGGREGATION + " planCacheStats()"),
new MethodMatcher(AGGREGATION + " project(dev.morphia.aggregation.stages.Projection)"),
new MethodMatcher(AGGREGATION + " redact(dev.morphia.aggregation.stages.Redact)"),
new MethodMatcher(AGGREGATION + " replaceRoot(dev.morphia.aggregation.stages.ReplaceRoot)"),
new MethodMatcher(AGGREGATION + " replaceWith(dev.morphia.aggregation.stages.ReplaceWith)"),
new MethodMatcher(AGGREGATION + " sample(dev.morphia.aggregation.stages.Sample)"),
new MethodMatcher(AGGREGATION + " set(dev.morphia.aggregation.stages.Set)"),
new MethodMatcher(AGGREGATION + " skip(long)"),
new MethodMatcher(AGGREGATION + " sort(dev.morphia.aggregation.stages.Sort)"),
new MethodMatcher(AGGREGATION + " sortByCount(dev.morphia.aggregation.stages.SortByCount)"),
new MethodMatcher(AGGREGATION + " unionWith(Class,Stage...)"),
new MethodMatcher(AGGREGATION + " unionWith(String,Stage...)"),
new MethodMatcher(AGGREGATION + " unset(dev.morphia.aggregation.stages.Unset)"),
new MethodMatcher(AGGREGATION + " unwind(dev.morphia.aggregation.stages.Unwind)"));

private static final MethodMatcher MEGA_MATCHER = new MethodMatcher(
AGGREGATION + " addFields(dev.morphia.aggregation.stages.AddFields)") {
AGGREGATION + " addFields(dev.morphia.aggregation.stages.AddFields)") {
@Override
public boolean matches(@Nullable MethodCall methodCall) {
return matchers.stream().anyMatch(matcher -> matcher.matches(methodCall)) || PIPELINE.matches(methodCall);
return matchers.stream().anyMatch(matcher -> matcher.matches(methodCall));
}
};

private static final JavaTemplate PIPELINE_TEMPLATE = (JavaTemplate.builder("Aggregation.pipeline(#{any(List<Stage>)})"))
.javaParser(JavaParser.fromJavaVersion()
.classpath("morphia-core"))
.imports(Aggregation.class.getName())
.imports(Stage.class.getName())
.doBeforeParseTemplate(System.out::println)
.build();

private static final JavaTemplate MATCH = (JavaTemplate.builder("Match.match(#{any()})"))
.javaParser(JavaParser.fromJavaVersion()
.classpath("morphia-core"))
.imports(Filter.class.getName())
.imports(Match.class.getName())
.build();
.javaParser(JavaParser.fromJavaVersion()
.classpath("morphia-core"))
.imports(Aggregation.class.getName())
.imports(Stage.class.getName())
.doBeforeParseTemplate(System.out::println)
.build();

private static final JavaTemplate MATCH = (JavaTemplate.builder("Match.match()"))
.javaParser(JavaParser.fromJavaVersion()
.classpath("morphia-core"))
.imports(Filter.class.getName())
.imports(Match.class.getName())
.build();

@Override
public String getDisplayName() {
Expand All @@ -99,7 +99,6 @@ public String getDescription() {
return "Rewrites an aggregation from using stage-named methods to using pipeline(Stage...).";
}


@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {

Expand All @@ -112,25 +111,25 @@ public MethodInvocation visitMethodInvocation(@NotNull MethodInvocation methodIn
final List<Expression> args = new ArrayList<>();
MethodInvocation invocation = (MethodInvocation) updated.getSelect();
if (invocation.getSimpleName().equals("match")) {

maybeAddImport(Match.class.getName(), "match", false);
MethodInvocation applied = MATCH.apply(new Cursor(getCursor(), invocation),
invocation.getCoordinates().replaceMethod(),
invocation.getArguments().toArray());
args.add(applied.withSelect(null));
invocation.getCoordinates().replaceMethod());
args.add(applied.withArguments(invocation.getArguments()).withSelect(null));
} else {
args.addAll(invocation.getArguments());
}
args.addAll(updated.getArguments());
Space after = methodInvocation.getPadding().getSelect().getAfter();
Space space = Space.build(getIndent(after), emptyList());
var list = args.stream()
.map(a -> (Expression) a.withPrefix(space))
.toList();
.map(a -> (Expression) a.withPrefix(space))
.toList();

updated = invocation.withArguments(list);
}
// return updated.withName(methodInvocation.getName().withSimpleName("pipeline"));
return updated.withName(methodInvocation.getName().withSimpleName("pipeline"));
return maybeAutoFormat(methodInvocation, updated.withName(methodInvocation.getName().withSimpleName("pipeline"))
.withPrefix(Space.build("\n", emptyList())), context);
} else {
return super.visitMethodInvocation(methodInvocation, context);
}
Expand Down Expand Up @@ -160,8 +159,8 @@ public MethodInvocation visitMethodInvocation(@NotNull MethodInvocation methodIn
if (invocation.getSimpleName().equals("match")) {

MethodInvocation applied = MATCH.apply(new Cursor(getCursor(), invocation),
invocation.getCoordinates().replaceMethod(),
invocation.getArguments().toArray());
invocation.getCoordinates().replaceMethod(),
invocation.getArguments().toArray());
args.add(applied.withSelect(null));
} else {
args.addAll(invocation.getArguments());
Expand All @@ -172,7 +171,7 @@ public MethodInvocation visitMethodInvocation(@NotNull MethodInvocation methodIn

Collections.reverse(args);
return updated.withName(methodInvocation.getName().withSimpleName("pipeline"))
.withArguments(args);
.withArguments(args);
// MethodInvocation applied = PIPELINE_TEMPLATE.apply(new Cursor(getCursor(), updated),
// updated.getCoordinates().replaceMethod(), args.toArray() );
// return autoFormat(applied, context);
Expand Down
22 changes: 22 additions & 0 deletions rewrite/src/main/resources/META-INF/rewrite/rewrite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,29 @@ recipeList:
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: dev.morphia.DatastoreImpl
newFullyQualifiedTypeName: dev.morphia.MorphiaDatastore
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: dev.morphia.utils.IndexDirection
newFullyQualifiedTypeName: dev.morphia.mapping.IndexDirection
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: dev.morphia.utils.IndexType
newFullyQualifiedTypeName: dev.morphia.mapping.IndexType
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: dev.morphia.mapping.MapperOptions.PropertyDiscovery
newFullyQualifiedTypeName: dev.morphia.mapping.PropertyDiscovery
- org.openrewrite.maven.UpgradeDependencyVersion:
groupId: dev.morphia.morphia
artifactId: morphia-core
newVersion: 3.0.0-SNAPSHOT
---
type: specs.openrewrite.org/v1beta/recipe
name: dev.morphia.InternalOnly
displayName: Recipe definitions specifically for testing in morphia-upgrade-testing. not for external use.
recipeList:
- org.openrewrite.DeleteSourceFiles:
filePattern: src/test/java/dev/morphia/test/TestLegacyUpdate.java
- org.openrewrite.DeleteSourceFiles:
filePattern: src/test/java/dev/morphia/test/query/legacy/TestLegacyQuery.java
- org.openrewrite.DeleteSourceFiles:
filePattern: src/test/java/dev/morphia/test/BuildConfigTest.java
- org.openrewrite.DeleteSourceFiles:
filePattern: src/test/java/dev/morphia/test/models/Keys.java
Loading

0 comments on commit c4f97cc

Please sign in to comment.