GPBWellKnownTypes.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2015 Google Inc. All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style
  5. // license that can be found in the LICENSE file or at
  6. // https://developers.google.com/open-source/licenses/bsd
  7. // Importing sources here to force the linker to include our category methods in
  8. // the static library. If these were compiled separately, the category methods
  9. // below would be stripped by the linker.
  10. #import "GPBWellKnownTypes.h"
  11. #import "GPBUtilities_PackagePrivate.h"
  12. NSString *const GPBWellKnownTypesErrorDomain = GPBNSStringifySymbol(GPBWellKnownTypesErrorDomain);
  13. static NSString *kTypePrefixGoogleApisCom = @"type.googleapis.com/";
  14. static NSTimeInterval TimeIntervalFromSecondsAndNanos(int64_t seconds, int32_t nanos) {
  15. return seconds + (NSTimeInterval)nanos / 1e9;
  16. }
  17. static int32_t SecondsAndNanosFromTimeInterval(NSTimeInterval time, int64_t *outSeconds,
  18. BOOL nanosMustBePositive) {
  19. NSTimeInterval seconds;
  20. NSTimeInterval nanos = modf(time, &seconds);
  21. if (nanosMustBePositive && (nanos < 0)) {
  22. // Per Timestamp.proto, nanos is non-negative and "Negative second values with
  23. // fractions must still have non-negative nanos values that count forward in
  24. // time. Must be from 0 to 999,999,999 inclusive."
  25. --seconds;
  26. nanos = 1.0 + nanos;
  27. }
  28. nanos *= 1e9;
  29. *outSeconds = (int64_t)seconds;
  30. return (int32_t)nanos;
  31. }
  32. static NSString *BuildTypeURL(NSString *typeURLPrefix, NSString *fullName) {
  33. if (typeURLPrefix.length == 0) {
  34. return fullName;
  35. }
  36. if ([typeURLPrefix hasSuffix:@"/"]) {
  37. return [typeURLPrefix stringByAppendingString:fullName];
  38. }
  39. return [NSString stringWithFormat:@"%@/%@", typeURLPrefix, fullName];
  40. }
  41. static NSString *ParseTypeFromURL(NSString *typeURLString) {
  42. NSRange range = [typeURLString rangeOfString:@"/" options:NSBackwardsSearch];
  43. if ((range.location == NSNotFound) || (NSMaxRange(range) == typeURLString.length)) {
  44. return nil;
  45. }
  46. NSString *result = [typeURLString substringFromIndex:range.location + 1];
  47. return result;
  48. }
  49. #pragma mark - GPBTimestamp
  50. @implementation GPBTimestamp (GBPWellKnownTypes)
  51. - (instancetype)initWithDate:(NSDate *)date {
  52. return [self initWithTimeIntervalSince1970:date.timeIntervalSince1970];
  53. }
  54. - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  55. if ((self = [super init])) {
  56. int64_t seconds;
  57. int32_t nanos = SecondsAndNanosFromTimeInterval(timeIntervalSince1970, &seconds, YES);
  58. self.seconds = seconds;
  59. self.nanos = nanos;
  60. }
  61. return self;
  62. }
  63. - (NSDate *)date {
  64. return [NSDate dateWithTimeIntervalSince1970:self.timeIntervalSince1970];
  65. }
  66. - (void)setDate:(NSDate *)date {
  67. self.timeIntervalSince1970 = date.timeIntervalSince1970;
  68. }
  69. - (NSTimeInterval)timeIntervalSince1970 {
  70. return TimeIntervalFromSecondsAndNanos(self.seconds, self.nanos);
  71. }
  72. - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  73. int64_t seconds;
  74. int32_t nanos = SecondsAndNanosFromTimeInterval(timeIntervalSince1970, &seconds, YES);
  75. self.seconds = seconds;
  76. self.nanos = nanos;
  77. }
  78. @end
  79. #pragma mark - GPBDuration
  80. @implementation GPBDuration (GBPWellKnownTypes)
  81. - (instancetype)initWithTimeInterval:(NSTimeInterval)timeInterval {
  82. if ((self = [super init])) {
  83. int64_t seconds;
  84. int32_t nanos = SecondsAndNanosFromTimeInterval(timeInterval, &seconds, NO);
  85. self.seconds = seconds;
  86. self.nanos = nanos;
  87. }
  88. return self;
  89. }
  90. - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  91. return [self initWithTimeInterval:timeIntervalSince1970];
  92. }
  93. - (NSTimeInterval)timeInterval {
  94. return TimeIntervalFromSecondsAndNanos(self.seconds, self.nanos);
  95. }
  96. - (void)setTimeInterval:(NSTimeInterval)timeInterval {
  97. int64_t seconds;
  98. int32_t nanos = SecondsAndNanosFromTimeInterval(timeInterval, &seconds, NO);
  99. self.seconds = seconds;
  100. self.nanos = nanos;
  101. }
  102. - (NSTimeInterval)timeIntervalSince1970 {
  103. return self.timeInterval;
  104. }
  105. - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  106. self.timeInterval = timeIntervalSince1970;
  107. }
  108. @end
  109. #pragma mark - GPBAny
  110. @implementation GPBAny (GBPWellKnownTypes)
  111. + (instancetype)anyWithMessage:(GPBMessage *)message error:(NSError **)errorPtr {
  112. return [self anyWithMessage:message typeURLPrefix:kTypePrefixGoogleApisCom error:errorPtr];
  113. }
  114. + (instancetype)anyWithMessage:(GPBMessage *)message
  115. typeURLPrefix:(NSString *)typeURLPrefix
  116. error:(NSError **)errorPtr {
  117. return [[[self alloc] initWithMessage:message typeURLPrefix:typeURLPrefix
  118. error:errorPtr] autorelease];
  119. }
  120. - (instancetype)initWithMessage:(GPBMessage *)message error:(NSError **)errorPtr {
  121. return [self initWithMessage:message typeURLPrefix:kTypePrefixGoogleApisCom error:errorPtr];
  122. }
  123. - (instancetype)initWithMessage:(GPBMessage *)message
  124. typeURLPrefix:(NSString *)typeURLPrefix
  125. error:(NSError **)errorPtr {
  126. self = [self init];
  127. if (self) {
  128. if (![self packWithMessage:message typeURLPrefix:typeURLPrefix error:errorPtr]) {
  129. [self release];
  130. self = nil;
  131. }
  132. }
  133. return self;
  134. }
  135. - (BOOL)packWithMessage:(GPBMessage *)message error:(NSError **)errorPtr {
  136. return [self packWithMessage:message typeURLPrefix:kTypePrefixGoogleApisCom error:errorPtr];
  137. }
  138. - (BOOL)packWithMessage:(GPBMessage *)message
  139. typeURLPrefix:(NSString *)typeURLPrefix
  140. error:(NSError **)errorPtr {
  141. NSString *fullName = [message descriptor].fullName;
  142. if (fullName.length == 0) {
  143. if (errorPtr) {
  144. *errorPtr = [NSError errorWithDomain:GPBWellKnownTypesErrorDomain
  145. code:GPBWellKnownTypesErrorCodeFailedToComputeTypeURL
  146. userInfo:nil];
  147. }
  148. return NO;
  149. }
  150. if (errorPtr) {
  151. *errorPtr = nil;
  152. }
  153. self.typeURL = BuildTypeURL(typeURLPrefix, fullName);
  154. self.value = message.data;
  155. return YES;
  156. }
  157. - (GPBMessage *)unpackMessageClass:(Class)messageClass error:(NSError **)errorPtr {
  158. return [self unpackMessageClass:messageClass extensionRegistry:nil error:errorPtr];
  159. }
  160. - (nullable GPBMessage *)unpackMessageClass:(Class)messageClass
  161. extensionRegistry:(nullable id<GPBExtensionRegistry>)extensionRegistry
  162. error:(NSError **)errorPtr {
  163. NSString *fullName = [messageClass descriptor].fullName;
  164. if (fullName.length == 0) {
  165. if (errorPtr) {
  166. *errorPtr = [NSError errorWithDomain:GPBWellKnownTypesErrorDomain
  167. code:GPBWellKnownTypesErrorCodeFailedToComputeTypeURL
  168. userInfo:nil];
  169. }
  170. return nil;
  171. }
  172. NSString *expectedFullName = ParseTypeFromURL(self.typeURL);
  173. if ((expectedFullName == nil) || ![expectedFullName isEqual:fullName]) {
  174. if (errorPtr) {
  175. *errorPtr = [NSError errorWithDomain:GPBWellKnownTypesErrorDomain
  176. code:GPBWellKnownTypesErrorCodeTypeURLMismatch
  177. userInfo:nil];
  178. }
  179. return nil;
  180. }
  181. return [messageClass parseFromData:self.value extensionRegistry:extensionRegistry error:errorPtr];
  182. }
  183. @end