1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2015 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 #import <Foundation/Foundation.h>
33 // This CPP symbol can be defined to use imports that match up to the framework
34 // imports needed when using CocoaPods.
35 #if !defined(GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS)
36 #define GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS 0
39 #if GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS
40 #import <Protobuf/Any.pbobjc.h>
41 #import <Protobuf/Duration.pbobjc.h>
42 #import <Protobuf/Timestamp.pbobjc.h>
44 #import "google/protobuf/Any.pbobjc.h"
45 #import "google/protobuf/Duration.pbobjc.h"
46 #import "google/protobuf/Timestamp.pbobjc.h"
49 NS_ASSUME_NONNULL_BEGIN
53 /** NSError domain used for errors. */
54 extern NSString *const GPBWellKnownTypesErrorDomain;
56 /** Error code for NSError with GPBWellKnownTypesErrorDomain. */
57 typedef NS_ENUM(NSInteger, GPBWellKnownTypesErrorCode) {
58 /** The type_url could not be computed for the requested GPBMessage class. */
59 GPBWellKnownTypesErrorCodeFailedToComputeTypeURL = -100,
60 /** type_url in a Any doesn’t match that of the requested GPBMessage class. */
61 GPBWellKnownTypesErrorCodeTypeURLMismatch = -101,
64 #pragma mark - GPBTimestamp
67 * Category for GPBTimestamp to work with standard Foundation time/date types.
69 @interface GPBTimestamp (GBPWellKnownTypes)
71 /** The NSDate representation of this GPBTimestamp. */
72 @property(nonatomic, readwrite, strong) NSDate *date;
75 * The NSTimeInterval representation of this GPBTimestamp.
77 * @note: Not all second/nanos combinations can be represented in a
78 * NSTimeInterval, so getting this could be a lossy transform.
80 @property(nonatomic, readwrite) NSTimeInterval timeIntervalSince1970;
83 * Initializes a GPBTimestamp with the given NSDate.
85 * @param date The date to configure the GPBTimestamp with.
87 * @return A newly initialized GPBTimestamp.
89 - (instancetype)initWithDate:(NSDate *)date;
92 * Initializes a GPBTimestamp with the given NSTimeInterval.
94 * @param timeIntervalSince1970 Time interval to configure the GPBTimestamp with.
96 * @return A newly initialized GPBTimestamp.
98 - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970;
102 #pragma mark - GPBDuration
105 * Category for GPBDuration to work with standard Foundation time type.
107 @interface GPBDuration (GBPWellKnownTypes)
110 * The NSTimeInterval representation of this GPBDuration.
112 * @note: Not all second/nanos combinations can be represented in a
113 * NSTimeInterval, so getting this could be a lossy transform.
115 @property(nonatomic, readwrite) NSTimeInterval timeInterval;
118 * Initializes a GPBDuration with the given NSTimeInterval.
120 * @param timeInterval Time interval to configure the GPBDuration with.
122 * @return A newly initialized GPBDuration.
124 - (instancetype)initWithTimeInterval:(NSTimeInterval)timeInterval;
126 // These next two methods are deprecated because GBPDuration has no need of a
127 // "base" time. The older methods were about symmetry with GBPTimestamp, but
128 // the unix epoch usage is too confusing.
130 /** Deprecated, use timeInterval instead. */
131 @property(nonatomic, readwrite) NSTimeInterval timeIntervalSince1970
132 __attribute__((deprecated("Use timeInterval")));
133 /** Deprecated, use initWithTimeInterval: instead. */
134 - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970
135 __attribute__((deprecated("Use initWithTimeInterval:")));
139 #pragma mark - GPBAny
142 * Category for GPBAny to help work with the message within the object.
144 @interface GPBAny (GBPWellKnownTypes)
147 * Convenience method to create a GPBAny containing the serialized message.
148 * This uses type.googleapis.com/ as the type_url's prefix.
150 * @param message The message to be packed into the GPBAny.
151 * @param errorPtr Pointer to an error that will be populated if something goes
154 * @return A newly configured GPBAny with the given message, or nil on failure.
156 + (nullable instancetype)anyWithMessage:(nonnull GPBMessage *)message
157 error:(NSError **)errorPtr;
160 * Convenience method to create a GPBAny containing the serialized message.
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
167 * @return A newly configured GPBAny with the given message, or nil on failure.
169 + (nullable instancetype)anyWithMessage:(nonnull GPBMessage *)message
170 typeURLPrefix:(nonnull NSString *)typeURLPrefix
171 error:(NSError **)errorPtr;
174 * Initializes a GPBAny to contain the serialized message. This uses
175 * type.googleapis.com/ as the type_url's prefix.
177 * @param message The message to be packed into the GPBAny.
178 * @param errorPtr Pointer to an error that will be populated if something goes
181 * @return A newly configured GPBAny with the given message, or nil on failure.
183 - (nullable instancetype)initWithMessage:(nonnull GPBMessage *)message
184 error:(NSError **)errorPtr;
187 * Initializes a GPBAny to contain the serialized message.
189 * @param message The message to be packed into the GPBAny.
190 * @param typeURLPrefix The URL prefix to apply for type_url.
191 * @param errorPtr Pointer to an error that will be populated if something
194 * @return A newly configured GPBAny with the given message, or nil on failure.
196 - (nullable instancetype)initWithMessage:(nonnull GPBMessage *)message
197 typeURLPrefix:(nonnull NSString *)typeURLPrefix
198 error:(NSError **)errorPtr;
201 * Packs the serialized message into this GPBAny. This uses
202 * type.googleapis.com/ as the type_url's prefix.
204 * @param message The message to be packed into the GPBAny.
205 * @param errorPtr Pointer to an error that will be populated if something goes
208 * @return Whether the packing was successful or not.
210 - (BOOL)packWithMessage:(nonnull GPBMessage *)message
211 error:(NSError **)errorPtr;
214 * Packs the serialized message into this GPBAny.
216 * @param message The message to be packed into the GPBAny.
217 * @param typeURLPrefix The URL prefix to apply for type_url.
218 * @param errorPtr Pointer to an error that will be populated if something
221 * @return Whether the packing was successful or not.
223 - (BOOL)packWithMessage:(nonnull GPBMessage *)message
224 typeURLPrefix:(nonnull NSString *)typeURLPrefix
225 error:(NSError **)errorPtr;
228 * Unpacks the serialized message as if it was an instance of the given class.
230 * @note When checking type_url, the base URL is not checked, only the fully
233 * @param messageClass The class to use to deserialize the contained message.
234 * @param errorPtr Pointer to an error that will be populated if something
237 * @return An instance of the given class populated with the contained data, or
240 - (nullable GPBMessage *)unpackMessageClass:(Class)messageClass
241 error:(NSError **)errorPtr;
245 NS_ASSUME_NONNULL_END