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

Add packTo and unpackFrom in google.protobuf.Any. #602

Merged
merged 1 commit into from Jul 22, 2015

Conversation

TeBoring
Copy link
Contributor

These two methods make it easy to transform between any and normal message.
unPackeTo will throw error if the type url in any doesn't match the type of the message to be transformed to.

XCTAssertEqualObjects(
[GPBTypeGoogleApisComPrefix stringByAppendingString:from2.descriptor.name],
any.typeURL);
// XCTAssertEqualObjects(@"", any.value); // don't know why this failed.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

failed: ((@"") equal to (any.value)) failed: ("") is not equal to ("<>")
Why it's "<>"

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you asking me?

@@ -49,4 +51,22 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970;
@end

// Extension to GPBAny to support packFrom and unpackTo for arbitrary messages.
@interface GPBAny (GPBWellKnownTypes)
// Initialize any with the given message. e.g., for google.protobuf.foo, type url
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it make sense to capitalize "any" so that it reads a little better, or quote it? I find it very difficult to read the documentation because I'm not sure if "any" is part of the sentence, or the type.

#if !defined(NS_BLOCK_ASSERTIONS)
XCTAssertThrows([any wrapsMessageOfClass:[NSString class]]);
#endif
}
Copy link
Contributor

Choose a reason for hiding this comment

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

add #else and test the NO case

@TeBoring TeBoring force-pushed the objectivec branch 2 times, most recently from 53a8c59 to 8a28e06 Compare July 21, 2015 21:48
NSAssert([self.typeURL hasPrefix:GPBTypeGoogleApisComPrefix],
@"Invalid any type url (%@).", self.typeURL);
if (![self.typeURL hasPrefix:GPBTypeGoogleApisComPrefix]) {
return @"";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we return @"", we can treat it as normal return value without additional checking for nil.
In the places calling typeName, we can guarantee that returned value from typeName is always compared with a non-empty string.

Copy link
Contributor

Choose a reason for hiding this comment

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

you rarely if ever need to check for nil in Objective C. A message to nil just returns nil/0. Returning nil is very standard.

XCTAssertTrue([any wrapsMessageOfClass:[from2 class]]);
XCTAssertFalse([any wrapsMessageOfClass:[from class]]);
#if !defined(NS_BLOCK_ASSERTIONS)
XCTAssertThrows([any wrapsMessageOfClass:[NSString class]]);
Copy link
Contributor

Choose a reason for hiding this comment

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

May want to add a comment why the test is conditionalized here.

return [self.typeURL substringFromIndex:[GPBTypeGoogleApisComPrefix length]];
}

- (instancetype)initWithMessage:(GPBMessage*)message {
Copy link
Contributor

Choose a reason for hiding this comment

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

Final complaint. Move this to top of implementation block. Then LGTM.

The previous two methods make it easy to transform between any and normal message.
unPackeTo will throw error if the type url in any doesn't match the type of the message to be transformed to.
is checks any's type url matches the give GPBMessage type.
TeBoring added a commit that referenced this pull request Jul 22, 2015
Add packTo and unpackFrom in google.protobuf.Any.
@TeBoring TeBoring merged commit 1647e63 into protocolbuffers:master Jul 22, 2015
@TeBoring TeBoring deleted the objectivec branch July 22, 2015 03:50
taoso pushed a commit to taoso/protobuf that referenced this pull request Aug 1, 2018
adellahlou pushed a commit to adellahlou/protobuf that referenced this pull request Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants