GPBWellKnownTypes.h 7.8 KB

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