-
Notifications
You must be signed in to change notification settings - Fork 39
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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
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. This rule works if This may not always be the case. Is there a way in Error Prone to identify e.g. whether If we cannot guarantee it, is a 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. 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 |
---|---|---|
@@ -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); | ||
} | ||
} |
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.
(Unrelated to this line)
Do we want MongoDB-related Refaster rules in the first place?
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.
Yeah, sounds like a nice idea to me. We also have a BugPattern that's MongoDB related: https://github.com/PicnicSupermarket/error-prone-support/blob/master/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoDBTextFilterUsage.java.