GPBWellKnownTypes.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 TimeIntervalSince1970FromSecondsAndNanos(int64_t seconds,
  39. int32_t nanos) {
  40. return seconds + (NSTimeInterval)nanos / 1e9;
  41. }
  42. static int32_t SecondsAndNanosFromTimeIntervalSince1970(NSTimeInterval time,
  43. int64_t *outSeconds) {
  44. NSTimeInterval seconds;
  45. NSTimeInterval nanos = modf(time, &seconds);
  46. nanos *= 1e9;
  47. *outSeconds = (int64_t)seconds;
  48. return (int32_t)nanos;
  49. }
  50. static NSString *BuildTypeURL(NSString *typeURLPrefix, NSString *fullName) {
  51. if (typeURLPrefix.length == 0) {
  52. return fullName;
  53. }
  54. if ([typeURLPrefix hasSuffix:@"/"]) {
  55. return [typeURLPrefix stringByAppendingString:fullName];
  56. }
  57. return [NSString stringWithFormat:@"%@/%@", typeURLPrefix, fullName];
  58. }
  59. static NSString *ParseTypeFromURL(NSString *typeURLString) {
  60. NSRange range = [typeURLString rangeOfString:@"/" options:NSBackwardsSearch];
  61. if ((range.location == NSNotFound) ||
  62. (NSMaxRange(range) == typeURLString.length)) {
  63. return nil;
  64. }
  65. NSString *result = [typeURLString substringFromIndex:range.location + 1];
  66. return result;
  67. }
  68. #pragma mark - GPBTimestamp
  69. @implementation GPBTimestamp (GBPWellKnownTypes)
  70. - (instancetype)initWithDate:(NSDate *)date {
  71. return [self initWithTimeIntervalSince1970:date.timeIntervalSince1970];
  72. }
  73. - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  74. if ((self = [super init])) {
  75. int64_t seconds;
  76. int32_t nanos = SecondsAndNanosFromTimeIntervalSince1970(
  77. timeIntervalSince1970, &seconds);
  78. self.seconds = seconds;
  79. self.nanos = nanos;
  80. }
  81. return self;
  82. }
  83. - (NSDate *)date {
  84. return [NSDate dateWithTimeIntervalSince1970:self.timeIntervalSince1970];
  85. }
  86. - (void)setDate:(NSDate *)date {
  87. self.timeIntervalSince1970 = date.timeIntervalSince1970;
  88. }
  89. - (NSTimeInterval)timeIntervalSince1970 {
  90. return TimeIntervalSince1970FromSecondsAndNanos(self.seconds, self.nanos);
  91. }
  92. - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  93. int64_t seconds;
  94. int32_t nanos =
  95. SecondsAndNanosFromTimeIntervalSince1970(timeIntervalSince1970, &seconds);
  96. self.seconds = seconds;
  97. self.nanos = nanos;
  98. }
  99. @end
  100. #pragma mark - GPBDuration
  101. @implementation GPBDuration (GBPWellKnownTypes)
  102. - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  103. if ((self = [super init])) {
  104. int64_t seconds;
  105. int32_t nanos = SecondsAndNanosFromTimeIntervalSince1970(
  106. timeIntervalSince1970, &seconds);
  107. self.seconds = seconds;
  108. self.nanos = nanos;
  109. }
  110. return self;
  111. }
  112. - (NSTimeInterval)timeIntervalSince1970 {
  113. return TimeIntervalSince1970FromSecondsAndNanos(self.seconds, self.nanos);
  114. }
  115. - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
  116. int64_t seconds;
  117. int32_t nanos =
  118. SecondsAndNanosFromTimeIntervalSince1970(timeIntervalSince1970, &seconds);
  119. self.seconds = seconds;
  120. self.nanos = nanos;
  121. }
  122. @end
  123. #pragma mark - GPBAny
  124. @implementation GPBAny (GBPWellKnownTypes)
  125. + (instancetype)anyWithMessage:(GPBMessage *)message
  126. error:(NSError **)errorPtr {
  127. return [self anyWithMessage:message
  128. typeURLPrefix:kTypePrefixGoogleApisCom
  129. error:errorPtr];
  130. }
  131. + (instancetype)anyWithMessage:(GPBMessage *)message
  132. typeURLPrefix:(NSString *)typeURLPrefix
  133. error:(NSError **)errorPtr {
  134. return [[[self alloc] initWithMessage:message
  135. typeURLPrefix:typeURLPrefix
  136. error:errorPtr] autorelease];
  137. }
  138. - (instancetype)initWithMessage:(GPBMessage *)message
  139. error:(NSError **)errorPtr {
  140. return [self initWithMessage:message
  141. typeURLPrefix:kTypePrefixGoogleApisCom
  142. error:errorPtr];
  143. }
  144. - (instancetype)initWithMessage:(GPBMessage *)message
  145. typeURLPrefix:(NSString *)typeURLPrefix
  146. error:(NSError **)errorPtr {
  147. self = [self init];
  148. if (self) {
  149. if (![self packWithMessage:message
  150. typeURLPrefix:typeURLPrefix
  151. error:errorPtr]) {
  152. [self release];
  153. self = nil;
  154. }
  155. }
  156. return self;
  157. }
  158. - (BOOL)packWithMessage:(GPBMessage *)message
  159. error:(NSError **)errorPtr {
  160. return [self packWithMessage:message
  161. typeURLPrefix:kTypePrefixGoogleApisCom
  162. error:errorPtr];
  163. }
  164. - (BOOL)packWithMessage:(GPBMessage *)message
  165. typeURLPrefix:(NSString *)typeURLPrefix
  166. error:(NSError **)errorPtr {
  167. NSString *fullName = [message descriptor].fullName;
  168. if (fullName.length == 0) {
  169. if (errorPtr) {
  170. *errorPtr =
  171. [NSError errorWithDomain:GPBWellKnownTypesErrorDomain
  172. code:GPBWellKnownTypesErrorCodeFailedToComputeTypeURL
  173. userInfo:nil];
  174. }
  175. return NO;
  176. }
  177. if (errorPtr) {
  178. *errorPtr = nil;
  179. }
  180. self.typeURL = BuildTypeURL(typeURLPrefix, fullName);
  181. self.value = message.data;
  182. return YES;
  183. }
  184. - (GPBMessage *)unpackMessageClass:(Class)messageClass
  185. error:(NSError **)errorPtr {
  186. NSString *fullName = [messageClass descriptor].fullName;
  187. if (fullName.length == 0) {
  188. if (errorPtr) {
  189. *errorPtr =
  190. [NSError errorWithDomain:GPBWellKnownTypesErrorDomain
  191. code:GPBWellKnownTypesErrorCodeFailedToComputeTypeURL
  192. userInfo:nil];
  193. }
  194. return nil;
  195. }
  196. NSString *expectedFullName = ParseTypeFromURL(self.typeURL);
  197. if ((expectedFullName == nil) || ![expectedFullName isEqual:fullName]) {
  198. if (errorPtr) {
  199. *errorPtr =
  200. [NSError errorWithDomain:GPBWellKnownTypesErrorDomain
  201. code:GPBWellKnownTypesErrorCodeTypeURLMismatch
  202. userInfo:nil];
  203. }
  204. return nil;
  205. }
  206. // Any is proto3, which means no extensions, so this assumes anything put
  207. // within an any also won't need extensions. A second helper could be added
  208. // if needed.
  209. return [messageClass parseFromData:self.value
  210. error:errorPtr];
  211. }
  212. @end