GPBWellKnownTypes.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2015 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Importing sources here to force the linker to include our category methods in
  31. // the static library. If these were compiled separately, the category methods
  32. // below would be stripped by the linker.
  33. #import "GPBWellKnownTypes.h"
  34. #import "GPBUtilities_PackagePrivate.h"
  35. NSString *const GPBWellKnownTypesErrorDomain =
  36. GPBNSStringifySymbol(GPBWellKnownTypesErrorDomain);
  37. static NSString *kTypePrefixGoogleApisCom = @"type.googleapis.com/";
  38. static NSTimeInterval TimeIntervalFromSecondsAndNanos(int64_t seconds,
  39. int32_t nanos) {
  40. return seconds + (NSTimeInterval)nanos / 1e9;
  41. }
  42. static int32_t SecondsAndNanosFromTimeInterval(NSTimeInterval time,
  43. int64_t *outSeconds,
  44. BOOL nanosMustBePositive) {
  45. NSTimeInterval seconds;
  46. NSTimeInterval nanos = modf(time, &seconds);
  47. if (nanosMustBePositive && (nanos < 0)) {
  48. // Per Timestamp.proto, nanos is non-negative and "Negative second values with
  49. // fractions must still have non-negative nanos values that count forward in
  50. // time. Must be from 0 to 999,999,999 inclusive."
  51. --seconds;
  52. nanos = 1.0 + nanos;
  53. }
  54. nanos *= 1e9;
  55. *outSeconds = (int64_t)seconds;
  56. return (int32_t)nanos;
  57. }
  58. static NSString *BuildTypeURL(NSString *typeURLPrefix, NSString *fullName) {
  59. if (typeURLPrefix.length == 0) {
  60. return fullName;
  61. }
  62. if ([typeURLPrefix hasSuffix:@"/"]) {
  63. return [typeURLPrefix stringByAppendingString:fullName];
  64. }
  65. return [NSString stringWithFormat:@"%@/%@", typeURLPrefix, fullName];
  66. }
  67. static NSString *ParseTypeFromURL(NSString *typeURLString) {
  68. NSRange range = [typeURLString rangeOfString:@"/" options:NSBackwardsSearch];
  69. if ((range.location == NSNotFound) ||
  70. (NSMaxRange(range) == typeURLString.length)) {
  71. return nil;
  72. }
  73. NSString *result = [typeURLString substringFromIndex:range.location + 1];
  74. return result;
  75. }
  76. #pragma mark - GPBTimestamp
  77. @implementation GPBTimestamp (GBPWellKnownTypes)
  78. - (instancetype)initWithDate:(NSDate *)date {
  79. return [self initWithTimeIntervalSince1970:date.timeIntervalSince1970];
  80. }
  81. - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  82. if ((self = [super init])) {
  83. int64_t seconds;
  84. int32_t nanos = SecondsAndNanosFromTimeInterval(
  85. timeIntervalSince1970, &seconds, YES);
  86. self.seconds = seconds;
  87. self.nanos = nanos;
  88. }
  89. return self;
  90. }
  91. - (NSDate *)date {
  92. return [NSDate dateWithTimeIntervalSince1970:self.timeIntervalSince1970];
  93. }
  94. - (void)setDate:(NSDate *)date {
  95. self.timeIntervalSince1970 = date.timeIntervalSince1970;
  96. }
  97. - (NSTimeInterval)timeIntervalSince1970 {
  98. return TimeIntervalFromSecondsAndNanos(self.seconds, self.nanos);
  99. }
  100. - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  101. int64_t seconds;
  102. int32_t nanos =
  103. SecondsAndNanosFromTimeInterval(timeIntervalSince1970, &seconds, YES);
  104. self.seconds = seconds;
  105. self.nanos = nanos;
  106. }
  107. @end
  108. #pragma mark - GPBDuration
  109. @implementation GPBDuration (GBPWellKnownTypes)
  110. - (instancetype)initWithTimeInterval:(NSTimeInterval)timeInterval {
  111. if ((self = [super init])) {
  112. int64_t seconds;
  113. int32_t nanos = SecondsAndNanosFromTimeInterval(
  114. timeInterval, &seconds, NO);
  115. self.seconds = seconds;
  116. self.nanos = nanos;
  117. }
  118. return self;
  119. }
  120. - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  121. return [self initWithTimeInterval:timeIntervalSince1970];
  122. }
  123. - (NSTimeInterval)timeInterval {
  124. return TimeIntervalFromSecondsAndNanos(self.seconds, self.nanos);
  125. }
  126. - (void)setTimeInterval:(NSTimeInterval)timeInterval {
  127. int64_t seconds;
  128. int32_t nanos =
  129. SecondsAndNanosFromTimeInterval(timeInterval, &seconds, NO);
  130. self.seconds = seconds;
  131. self.nanos = nanos;
  132. }
  133. - (NSTimeInterval)timeIntervalSince1970 {
  134. return self.timeInterval;
  135. }
  136. - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  137. self.timeInterval = timeIntervalSince1970;
  138. }
  139. @end
  140. #pragma mark - GPBAny
  141. @implementation GPBAny (GBPWellKnownTypes)
  142. + (instancetype)anyWithMessage:(GPBMessage *)message
  143. error:(NSError **)errorPtr {
  144. return [self anyWithMessage:message
  145. typeURLPrefix:kTypePrefixGoogleApisCom
  146. error:errorPtr];
  147. }
  148. + (instancetype)anyWithMessage:(GPBMessage *)message
  149. typeURLPrefix:(NSString *)typeURLPrefix
  150. error:(NSError **)errorPtr {
  151. return [[[self alloc] initWithMessage:message
  152. typeURLPrefix:typeURLPrefix
  153. error:errorPtr] autorelease];
  154. }
  155. - (instancetype)initWithMessage:(GPBMessage *)message
  156. error:(NSError **)errorPtr {
  157. return [self initWithMessage:message
  158. typeURLPrefix:kTypePrefixGoogleApisCom
  159. error:errorPtr];
  160. }
  161. - (instancetype)initWithMessage:(GPBMessage *)message
  162. typeURLPrefix:(NSString *)typeURLPrefix
  163. error:(NSError **)errorPtr {
  164. self = [self init];
  165. if (self) {
  166. if (![self packWithMessage:message
  167. typeURLPrefix:typeURLPrefix
  168. error:errorPtr]) {
  169. [self release];
  170. self = nil;
  171. }
  172. }
  173. return self;
  174. }
  175. - (BOOL)packWithMessage:(GPBMessage *)message
  176. error:(NSError **)errorPtr {
  177. return [self packWithMessage:message
  178. typeURLPrefix:kTypePrefixGoogleApisCom
  179. error:errorPtr];
  180. }
  181. - (BOOL)packWithMessage:(GPBMessage *)message
  182. typeURLPrefix:(NSString *)typeURLPrefix
  183. error:(NSError **)errorPtr {
  184. NSString *fullName = [message descriptor].fullName;
  185. if (fullName.length == 0) {
  186. if (errorPtr) {
  187. *errorPtr =
  188. [NSError errorWithDomain:GPBWellKnownTypesErrorDomain
  189. code:GPBWellKnownTypesErrorCodeFailedToComputeTypeURL
  190. userInfo:nil];
  191. }
  192. return NO;
  193. }
  194. if (errorPtr) {
  195. *errorPtr = nil;
  196. }
  197. self.typeURL = BuildTypeURL(typeURLPrefix, fullName);
  198. self.value = message.data;
  199. return YES;
  200. }
  201. - (GPBMessage *)unpackMessageClass:(Class)messageClass
  202. error:(NSError **)errorPtr {
  203. NSString *fullName = [messageClass descriptor].fullName;
  204. if (fullName.length == 0) {
  205. if (errorPtr) {
  206. *errorPtr =
  207. [NSError errorWithDomain:GPBWellKnownTypesErrorDomain
  208. code:GPBWellKnownTypesErrorCodeFailedToComputeTypeURL
  209. userInfo:nil];
  210. }
  211. return nil;
  212. }
  213. NSString *expectedFullName = ParseTypeFromURL(self.typeURL);
  214. if ((expectedFullName == nil) || ![expectedFullName isEqual:fullName]) {
  215. if (errorPtr) {
  216. *errorPtr =
  217. [NSError errorWithDomain:GPBWellKnownTypesErrorDomain
  218. code:GPBWellKnownTypesErrorCodeTypeURLMismatch
  219. userInfo:nil];
  220. }
  221. return nil;
  222. }
  223. // Any is proto3, which means no extensions, so this assumes anything put
  224. // within an any also won't need extensions. A second helper could be added
  225. // if needed.
  226. return [messageClass parseFromData:self.value
  227. error:errorPtr];
  228. }
  229. @end