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

Introduce MongoDB Refaster rules #1214

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion error-prone-contrib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
<scope>test</scope>
<scope>provided</scope>
Copy link
Member Author

Choose a reason for hiding this comment

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

(Unrelated to this line)

Do we want MongoDB-related Refaster rules in the first place?

Copy link
Member

Choose a reason for hiding this comment

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

</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tech.picnic.errorprone.refasterrules;

import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static com.mongodb.client.model.Filters.eq;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import com.mongodb.client.model.Filters;
import org.bson.conversions.Bson;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;

/** Refaster rules related to MongoDB client expressions and statements. */
@OnlineDocumentation
final class MongoDBRules {
private MongoDBRules() {}

/** Avoid {@link Enum#toString()} invocations when invoking {@link Filters#eq(String, Object)}. */
static final class Eq {
@BeforeTemplate
Bson before(String field, Enum<?> value) {
return eq(field, value.toString());
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Bson after(String field, Enum<?> value) {
return eq(field, value);
}
}
Comment on lines +19 to +30
Copy link
Member Author

Choose a reason for hiding this comment

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

This rule works if Enum#toString and the encoded value are equivalent.

This may not always be the case. Is there a way in Error Prone to identify e.g. whether Enum#toString is overwritten?

If we cannot guarantee it, is a BugChecker the better choice?

Copy link
Member

Choose a reason for hiding this comment

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

We might be able to use a Matcher, we have a few examples here: https://github.com/PicnicSupermarket/error-prone-support/tree/master/refaster-support/src/main/java/tech/picnic/errorprone/refaster/matchers.

I think we can infer something like you are describing in a Matcher 💪🏻 !

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ final class RefasterRulesTest {
MapEntryRules.class,
MapRules.class,
MockitoRules.class,
MongoDBRules.class,
MultimapRules.class,
NullRules.class,
OptionalRules.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tech.picnic.errorprone.refasterrules;

import static com.mongodb.client.model.Filters.eq;

import org.bson.conversions.Bson;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;
import tech.picnic.errorprone.utils.ThirdPartyLibrary;

final class MongoDBRulesTest implements RefasterRuleCollectionTestCase {
Bson testEq() {
return eq("foo", ThirdPartyLibrary.GUAVA.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tech.picnic.errorprone.refasterrules;

import static com.mongodb.client.model.Filters.eq;

import org.bson.conversions.Bson;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;
import tech.picnic.errorprone.utils.ThirdPartyLibrary;

final class MongoDBRulesTest implements RefasterRuleCollectionTestCase {
Bson testEq() {
return eq("foo", ThirdPartyLibrary.GUAVA);
}
}
Loading