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

Support bit mask on the RKRequestMethod method param #1476

Closed

Conversation

dulacp
Copy link
Contributor

@dulacp dulacp commented Jul 1, 2013

Is there a reason why you didn't use a bitwise aware enum that could let us do things like :

RKRoute *createUpdateDeleteRoute = [RKRoute routeWithClass:[Album class]
                                               pathPattern:@"/api/album/:albumID/"
                                                    method:(RKRequestMethodGET | RKRequestMethodPUT | RKRequestMethodDELETE)];

Just to save us from duplicate lines ;)

@ghost ghost assigned blakewatters Jun 28, 2013
@blakewatters
Copy link
Member

Yeah I have thought about this recently. Should definitely make the change -- should be able to do so transparently

@dulacp
Copy link
Contributor Author

dulacp commented Jun 28, 2013

Allright, thanks for the fast answer that's comforting me on making a pull request (and keep it transparently of course)

@dulacp
Copy link
Contributor Author

dulacp commented Jun 29, 2013

Oh! I'm facing some design issues... the fact is that if we store the bitmask method value in the route.method property then there is no way to make the RKStringFromRequestMethod function work :/ because it's mapping one value to one string.

So one solution would be to have a new class method on RKRoute that returns an array of routes, but that's not very transparent. What do you think ?

@blakewatters
Copy link
Member

You should still be able to do an exact value comparison with a bitmask. On the request/response descriptors it would make sense to use them as a bit mask, but in routes and other situations they will need to be pinned to an exact value and raise an NSInvalidArgumentException when used inappropriately.

@dulacp
Copy link
Contributor Author

dulacp commented Jul 1, 2013

Totally agree, we can make exact value comparison of course.

On the request/response descriptors it would make sense to use them as a bit mask

Can't find where RKRequestMethod values are used for request/response descriptors... could you give me a hint ?

@blakewatters
Copy link
Member

Ah it's actually not currently in use on the descriptors -- I have a branch where I had introduced it as a new option that is unmerged. Had forgotten

@dulacp
Copy link
Contributor Author

dulacp commented Jul 1, 2013

Nice! I'm reassured, I was doubting of my searching skill ;)
So I had to wait for this one ? otherwise I will violate the "A single development branch should represent changes related to a single issue" principle...
Or maybe this issue can be part of the branch you are talking about ?

@blakewatters
Copy link
Member

Just go ahead with your development and shoot a pull request over whenever you are ready. I'll take care of integration with any other branches

@dulacp
Copy link
Contributor Author

dulacp commented Jul 1, 2013

Fine with me, I'll do that. Thanks for the hints

@blakewatters
Copy link
Member

Looks good -- the only other change I would make is to introduce RKRequestMethodAny, which should have all the bits flipped so that it will match any value. This is used within the routing layer to define a route that matches any method.

@dulacp
Copy link
Contributor Author

dulacp commented Jul 1, 2013

Thanks!
Good idea for the RKRequestMethodAny, I'm thinking of two ways

first, declare that at the end of the enum

RKRequestMethodAny = (RKRequestMethodGET |
                      RKRequestMethodPOST |
                      RKRequestMethodPUT |
                      RKRequestMethodDELETE |
                      RKRequestMethodHEAD |
                      RKRequestMethodPATCH |
                      RKRequestMethodOPTIONS)

or second method, less explicit

RKRequestMethodAny = ~(0)

got a preference ?

@segiddins
Copy link
Member

Go with the more explicit option

@blakewatters
Copy link
Member

Well the advantage of the second one would be that it would automatically include any additional methods defined, though I feel like the explicit option is easy to read.

@dulacp
Copy link
Contributor Author

dulacp commented Jul 1, 2013

Exactly, it would be more robust for future additions but harder to read, that's why I was asking for a preference.
On second thoughts I will probably go with the explicit option since adding methods will be a very rare case in the future I think, and @segiddins gave his preference.

@blakewatters
Copy link
Member

Sold.

and clean up declarations of RKRequestMethodAny in other classes

- (void)testThatYesIsReturnedWhenTheGivenRequestMethodIsAny
{
expect(RKIsAnExactRequestMethodMatch(RKRequestMethodAny)).to.beTruthy();
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this return false?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Humm no, what I meant by RKIsAnExactRequestMethodMatch is that the given HTTP method passed to the function is equal to one and only one of the RKRequestMethod options.
The idea is to check if the method is a power of two, or equals to RKRequestMethodAny.

Does that make sense ?

Copy link
Member

Choose a reason for hiding this comment

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

But RKRequestMethodAny is a mask representing all request methods (so it's not exact)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, that's why I've added this condition
We can rename the static function if that's unclear of course

Copy link
Member

Choose a reason for hiding this comment

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

I guess that just doesn't jibe with my interpretation of the word exact

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @segiddins -- RKRequestMethodANY should not be considered an exact method (nor should RKRequestMethodInvalid. What I am looking for in the conditions is that the given value corresponds to exactly one valid HTTP request method.

Maybe a better name is RKAssertRequestMethodSpecifiesHTTPMethod()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can rename it RKIsMatchingASingleRequestMethodOption if you want to ?

@blakewatters
Copy link
Member

Sorry another revision: It is valid to use an the method as a bit mask for routing purposes with:

+ (instancetype)routeWithClass:(Class)objectClass pathPattern:(NSString *)pathPattern method:(RKRequestMethod)method;
+ (instancetype)routeWithRelationshipName:(NSString *)name objectClass:(Class)objectClass pathPattern:(NSString *)pathPattern method:(RKRequestMethod)method;

This is because these methods are matched by HTTP method to URL, while named routes go from a name -> URL and method.

If multiple routes are registered that overlap and the value of one is not RKRequestMethodAny then an exception should be raised. The logic being that you can register a wildcard route that is a fallback in the event that there is not a more specific route in place.

@segiddins
Copy link
Member

This will also make #1439 a lot easier to implement

@blakewatters
Copy link
Member

Did some additional cleanup and merged to development in c73ac51

@dulacp dulacp deleted the prepare-support-for-bitmasks-methods branch July 2, 2013 22:40
@dulacp
Copy link
Contributor Author

dulacp commented Jul 2, 2013

Great! glad to here that

@segiddins
Copy link
Member

some things we might have missed:
- (NSArray *)enqueuedObjectRequestOperationsWithMethod:(RKRequestMethod)method matchingPathPattern:(NSString *)pathPattern does not match against the bitmask
RKStringDescribingRequestMethod should be public, as it will need to be used for the description of RKRequestDescriptor and RKResponseDescriptor

@blakewatters
Copy link
Member

Taking care of the enqueuedObjectRequestOperationsWithMethod:matchingPathPattern: gap now.

blakewatters pushed a commit that referenced this pull request Jul 3, 2013
…tRequestOperationsWithMethod:matchingPathPattern:` refs #1476
4PixelsDev pushed a commit to CenterDevice/RestKit that referenced this pull request Dec 12, 2013
…tRequestOperationsWithMethod:matchingPathPattern:` refs RestKit#1476
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants