GPBWellKnownTypes.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/Any.pbobjc.h>
  38. #import <Protobuf/Duration.pbobjc.h>
  39. #import <Protobuf/Timestamp.pbobjc.h>
  40. #else
  41. #import "google/protobuf/Any.pbobjc.h"
  42. #import "google/protobuf/Duration.pbobjc.h"
  43. #import "google/protobuf/Timestamp.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 timeIntervalSince1970;
  99. /**
  100. * Initializes a GPBDuration with the given NSTimeInterval.
  101. *
  102. * @param timeIntervalSince1970 Time interval to configure the GPBDuration with.
  103. *
  104. * @return A newly initialized GPBDuration.
  105. **/
  106. - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970;
  107. @end
  108. #pragma mark - GPBAny
  109. /**
  110. * Category for GPBAny to help work with the message within the object.
  111. **/
  112. @interface GPBAny (GBPWellKnownTypes)
  113. /**
  114. * Convenience method to create a GPBAny containing the serialized message.
  115. * This uses type.googleapis.com/ as the type_url's prefix.
  116. *
  117. * @param message The message to be packed into the GPBAny.
  118. * @param errorPtr Pointer to an error that will be populated if something goes
  119. * wrong.
  120. *
  121. * @return A newly configured GPBAny with the given message, or nil on failure.
  122. */
  123. + (nullable instancetype)anyWithMessage:(nonnull GPBMessage *)message
  124. error:(NSError **)errorPtr;
  125. /**
  126. * Convenience method to create a GPBAny containing the serialized message.
  127. *
  128. * @param message The message to be packed into the GPBAny.
  129. * @param typeURLPrefix The URL prefix to apply for type_url.
  130. * @param errorPtr Pointer to an error that will be populated if something
  131. * goes wrong.
  132. *
  133. * @return A newly configured GPBAny with the given message, or nil on failure.
  134. */
  135. + (nullable instancetype)anyWithMessage:(nonnull GPBMessage *)message
  136. typeURLPrefix:(nonnull NSString *)typeURLPrefix
  137. error:(NSError **)errorPtr;
  138. /**
  139. * Initializes a GPBAny to contain the serialized message. This uses
  140. * type.googleapis.com/ as the type_url's prefix.
  141. *
  142. * @param message The message to be packed into the GPBAny.
  143. * @param errorPtr Pointer to an error that will be populated if something goes
  144. * wrong.
  145. *
  146. * @return A newly configured GPBAny with the given message, or nil on failure.
  147. */
  148. - (nullable instancetype)initWithMessage:(nonnull GPBMessage *)message
  149. error:(NSError **)errorPtr;
  150. /**
  151. * Initializes a GPBAny to contain the serialized message.
  152. *
  153. * @param message The message to be packed into the GPBAny.
  154. * @param typeURLPrefix The URL prefix to apply for type_url.
  155. * @param errorPtr Pointer to an error that will be populated if something
  156. * goes wrong.
  157. *
  158. * @return A newly configured GPBAny with the given message, or nil on failure.
  159. */
  160. - (nullable instancetype)initWithMessage:(nonnull GPBMessage *)message
  161. typeURLPrefix:(nonnull NSString *)typeURLPrefix
  162. error:(NSError **)errorPtr;
  163. /**
  164. * Packs the serialized message into this GPBAny. This uses
  165. * type.googleapis.com/ as the type_url's prefix.
  166. *
  167. * @param message The message to be packed into the GPBAny.
  168. * @param errorPtr Pointer to an error that will be populated if something goes
  169. * wrong.
  170. *
  171. * @return Whether the packing was successful or not.
  172. */
  173. - (BOOL)packWithMessage:(nonnull GPBMessage *)message
  174. error:(NSError **)errorPtr;
  175. /**
  176. * Packs the serialized message into this GPBAny.
  177. *
  178. * @param message The message to be packed into the GPBAny.
  179. * @param typeURLPrefix The URL prefix to apply for type_url.
  180. * @param errorPtr Pointer to an error that will be populated if something
  181. * goes wrong.
  182. *
  183. * @return Whether the packing was successful or not.
  184. */
  185. - (BOOL)packWithMessage:(nonnull GPBMessage *)message
  186. typeURLPrefix:(nonnull NSString *)typeURLPrefix
  187. error:(NSError **)errorPtr;
  188. /**
  189. * Unpacks the serialized message as if it was an instance of the given class.
  190. *
  191. * @note When checking type_url, the base URL is not checked, only the fully
  192. * qualified name.
  193. *
  194. * @param messageClass The class to use to deserialize the contained message.
  195. * @param errorPtr Pointer to an error that will be populated if something
  196. * goes wrong.
  197. *
  198. * @return An instance of the given class populated with the contained data, or
  199. * nil on failure.
  200. */
  201. - (nullable GPBMessage *)unpackMessageClass:(Class)messageClass
  202. error:(NSError **)errorPtr;
  203. @end
  204. NS_ASSUME_NONNULL_END