GPBWellKnownTypes.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #import <Foundation/Foundation.h>
  31. // This CPP symbol can be defined to use imports that match up to the framework
  32. // imports needed when using CocoaPods.
  33. #if !defined(GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS)
  34. #define GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS 0
  35. #endif
  36. #if GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS
  37. #import <Protobuf/GPBAny.pbobjc.h>
  38. #import <Protobuf/GPBDuration.pbobjc.h>
  39. #import <Protobuf/GPBTimestamp.pbobjc.h>
  40. #else
  41. #import "GPBAny.pbobjc.h"
  42. #import "GPBDuration.pbobjc.h"
  43. #import "GPBTimestamp.pbobjc.h"
  44. #endif
  45. NS_ASSUME_NONNULL_BEGIN
  46. #pragma mark - Errors
  47. /** NSError domain used for errors. */
  48. extern NSString *const GPBWellKnownTypesErrorDomain;
  49. /** Error code for NSError with GPBWellKnownTypesErrorDomain. */
  50. typedef NS_ENUM(NSInteger, GPBWellKnownTypesErrorCode) {
  51. /** The type_url could not be computed for the requested GPBMessage class. */
  52. GPBWellKnownTypesErrorCodeFailedToComputeTypeURL = -100,
  53. /** type_url in a Any doesn’t match that of the requested GPBMessage class. */
  54. GPBWellKnownTypesErrorCodeTypeURLMismatch = -101,
  55. };
  56. #pragma mark - GPBTimestamp
  57. /**
  58. * Category for GPBTimestamp to work with standard Foundation time/date types.
  59. **/
  60. @interface GPBTimestamp (GBPWellKnownTypes)
  61. /** The NSDate representation of this GPBTimestamp. */
  62. @property(nonatomic, readwrite, strong) NSDate *date;
  63. /**
  64. * The NSTimeInterval representation of this GPBTimestamp.
  65. *
  66. * @note: Not all second/nanos combinations can be represented in a
  67. * NSTimeInterval, so getting this could be a lossy transform.
  68. **/
  69. @property(nonatomic, readwrite) NSTimeInterval timeIntervalSince1970;
  70. /**
  71. * Initializes a GPBTimestamp with the given NSDate.
  72. *
  73. * @param date The date to configure the GPBTimestamp with.
  74. *
  75. * @return A newly initialized GPBTimestamp.
  76. **/
  77. - (instancetype)initWithDate:(NSDate *)date;
  78. /**
  79. * Initializes a GPBTimestamp with the given NSTimeInterval.
  80. *
  81. * @param timeIntervalSince1970 Time interval to configure the GPBTimestamp with.
  82. *
  83. * @return A newly initialized GPBTimestamp.
  84. **/
  85. - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970;
  86. @end
  87. #pragma mark - GPBDuration
  88. /**
  89. * Category for GPBDuration to work with standard Foundation time type.
  90. **/
  91. @interface GPBDuration (GBPWellKnownTypes)
  92. /**
  93. * The NSTimeInterval representation of this GPBDuration.
  94. *
  95. * @note: Not all second/nanos combinations can be represented in a
  96. * NSTimeInterval, so getting this could be a lossy transform.
  97. **/
  98. @property(nonatomic, readwrite) NSTimeInterval timeInterval;
  99. /**
  100. * Initializes a GPBDuration with the given NSTimeInterval.
  101. *
  102. * @param timeInterval Time interval to configure the GPBDuration with.
  103. *
  104. * @return A newly initialized GPBDuration.
  105. **/
  106. - (instancetype)initWithTimeInterval:(NSTimeInterval)timeInterval;
  107. // These next two methods are deprecated because GBPDuration has no need of a
  108. // "base" time. The older methods were about symmetry with GBPTimestamp, but
  109. // the unix epoch usage is too confusing.
  110. /** Deprecated, use timeInterval instead. */
  111. @property(nonatomic, readwrite) NSTimeInterval timeIntervalSince1970
  112. __attribute__((deprecated("Use timeInterval")));
  113. /** Deprecated, use initWithTimeInterval: instead. */
  114. - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970
  115. __attribute__((deprecated("Use initWithTimeInterval:")));
  116. @end
  117. #pragma mark - GPBAny
  118. /**
  119. * Category for GPBAny to help work with the message within the object.
  120. **/
  121. @interface GPBAny (GBPWellKnownTypes)
  122. /**
  123. * Convenience method to create a GPBAny containing the serialized message.
  124. * This uses type.googleapis.com/ as the type_url's prefix.
  125. *
  126. * @param message The message to be packed into the GPBAny.
  127. * @param errorPtr Pointer to an error that will be populated if something goes
  128. * wrong.
  129. *
  130. * @return A newly configured GPBAny with the given message, or nil on failure.
  131. */
  132. + (nullable instancetype)anyWithMessage:(nonnull GPBMessage *)message
  133. error:(NSError **)errorPtr;
  134. /**
  135. * Convenience method to create a GPBAny containing the serialized message.
  136. *
  137. * @param message The message to be packed into the GPBAny.
  138. * @param typeURLPrefix The URL prefix to apply for type_url.
  139. * @param errorPtr Pointer to an error that will be populated if something
  140. * goes wrong.
  141. *
  142. * @return A newly configured GPBAny with the given message, or nil on failure.
  143. */
  144. + (nullable instancetype)anyWithMessage:(nonnull GPBMessage *)message
  145. typeURLPrefix:(nonnull NSString *)typeURLPrefix
  146. error:(NSError **)errorPtr;
  147. /**
  148. * Initializes a GPBAny to contain the serialized message. This uses
  149. * type.googleapis.com/ as the type_url's prefix.
  150. *
  151. * @param message The message to be packed into the GPBAny.
  152. * @param errorPtr Pointer to an error that will be populated if something goes
  153. * wrong.
  154. *
  155. * @return A newly configured GPBAny with the given message, or nil on failure.
  156. */
  157. - (nullable instancetype)initWithMessage:(nonnull GPBMessage *)message
  158. error:(NSError **)errorPtr;
  159. /**
  160. * Initializes a GPBAny to contain the serialized message.
  161. *
  162. * @param message The message to be packed into the GPBAny.
  163. * @param typeURLPrefix The URL prefix to apply for type_url.
  164. * @param errorPtr Pointer to an error that will be populated if something
  165. * goes wrong.
  166. *
  167. * @return A newly configured GPBAny with the given message, or nil on failure.
  168. */
  169. - (nullable instancetype)initWithMessage:(nonnull GPBMessage *)message
  170. typeURLPrefix:(nonnull NSString *)typeURLPrefix
  171. error:(NSError **)errorPtr;
  172. /**
  173. * Packs the serialized message into this GPBAny. This uses
  174. * type.googleapis.com/ as the type_url's prefix.
  175. *
  176. * @param message The message to be packed into the GPBAny.
  177. * @param errorPtr Pointer to an error that will be populated if something goes
  178. * wrong.
  179. *
  180. * @return Whether the packing was successful or not.
  181. */
  182. - (BOOL)packWithMessage:(nonnull GPBMessage *)message
  183. error:(NSError **)errorPtr;
  184. /**
  185. * Packs the serialized message into this GPBAny.
  186. *
  187. * @param message The message to be packed into the GPBAny.
  188. * @param typeURLPrefix The URL prefix to apply for type_url.
  189. * @param errorPtr Pointer to an error that will be populated if something
  190. * goes wrong.
  191. *
  192. * @return Whether the packing was successful or not.
  193. */
  194. - (BOOL)packWithMessage:(nonnull GPBMessage *)message
  195. typeURLPrefix:(nonnull NSString *)typeURLPrefix
  196. error:(NSError **)errorPtr;
  197. /**
  198. * Unpacks the serialized message as if it was an instance of the given class.
  199. *
  200. * @note When checking type_url, the base URL is not checked, only the fully
  201. * qualified name.
  202. *
  203. * @param messageClass The class to use to deserialize the contained message.
  204. * @param errorPtr Pointer to an error that will be populated if something
  205. * goes wrong.
  206. *
  207. * @return An instance of the given class populated with the contained data, or
  208. * nil on failure.
  209. */
  210. - (nullable GPBMessage *)unpackMessageClass:(Class)messageClass
  211. error:(NSError **)errorPtr;
  212. @end
  213. NS_ASSUME_NONNULL_END