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

Allow unit return type for suspend functions #293

Closed
wants to merge 4 commits into from

Conversation

oguzcelik
Copy link

Summary

This pr aims to fix the lint rule for retrofit usage. Solves this issue

For suspend functions it's a valid case to return Unit.

Requirements (place an x in each [ ])

Unfortunately the contributing guidelines and Contributor license agreement links are not working.

The following point can be removed after setting up CI (such as Travis) with coverage reports (such as Codecov)

  • I've written tests to cover the new code and functionality included in this PR.

The following point can be removed after setting up a CLA reporting tool such as cla-assistant.io

Copy link

salesforce-cla bot commented Jul 8, 2024

Thanks for the contribution! Before we can merge this, we need @oguzcelik to sign the Salesforce Inc. Contributor License Agreement.

@ZacSweers
Copy link
Collaborator

Let's make this more intentional. Could we either add an opt-in annotation like @AllowUnitResponse with a doc on it explaining contexts where it makes sense? Blanket disabling this feels like the wrong move when the goal is to make developers more intentional.

import slack.lint.BaseSlackLintTest

class RetrofitUsageDetectorTest : BaseSlackLintTest() {

private companion object {
private val allowUnitResponse =
Copy link
Author

Choose a reason for hiding this comment

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

I followed other examples of how to make the test recognise this import. Don't know if there is a better way.

* to have a return type of Unit, which otherwise will be flagged as an issue.
*/
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.BINARY)
Copy link
Author

Choose a reason for hiding this comment

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

Marked as Binary retention as we don't need this at runtime.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can it just be SOURCE?

@oguzcelik
Copy link
Author

Would you want to make the docs more explicit ? Something like:

/**
 * Annotation to allow suspend Retrofit functions to return Unit.
 * 
 * This annotation is used to indicate that a suspend Retrofit function is allowed to have 
 * a return type of [Unit]. This might be necessary for certain HTTP methods where the 
 * response body is not significant. For instance:
 * 
 * - Methods like `@POST`, `@PUT`, or `@DELETE` may logically return `Unit` if 
 *    the client is only concerned with the success/failure of the operation and not the 
 *    actual response body.
 * 
 * Invalid Use Cases:
 * - `@GET`: Typically, `@GET` methods should return a meaningful response object as 
 *    they are intended to retrieve data.
 */

Copy link
Collaborator

@ZacSweers ZacSweers left a comment

Choose a reason for hiding this comment

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

Sorry for the delay, I was on sabbatical over the summer. Left a couple comments, also please run ./gradlew spotlessApply to fix formatting issues (and in the future - please match our project's conventions)

* to have a return type of Unit, which otherwise will be flagged as an issue.
*/
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.BINARY)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can it just be SOURCE?

returnType.canonicalText == "kotlin.Unit"
) {
val isSuspend = context.evaluator.isSuspend(node)
val hasAllowUnitResponseAnnotation = node.findAnnotation("slack.lint.annotations.AllowUnitResponse") != null
Copy link
Collaborator

Choose a reason for hiding this comment

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

If this is false we should be able to skip needing the isVoidOrUnitReturnType variable right? Let's make that logic lazier

@ZacSweers
Copy link
Collaborator

ZacSweers commented Sep 2, 2024

Sorry @oguzcelik but you've not signed our CLA, so I cannot accept this PR or finish it for you. Instructions were commented here.

@ZacSweers ZacSweers closed this Sep 2, 2024
@oguzcelik
Copy link
Author

Hey @ZacSweers 🖖 hope you enjoyed your sabbatical 🥳

I had already mentioned in the pr description that the CLA link unfortunately doesn't work.

Here's a video in case you are wondering what I mean

Screen.Recording.2024-09-03.at.10.55.58.mov

Please let me know once the cla link and the contributing guidelines links are fixed and I'll try to get back to this to open another pr 👍

@ZacSweers
Copy link
Collaborator

Please use the one in the comment I linked above? The template is unfortunately old but the comment from the salesforce bot should be working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants