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 // Importing sources here to force the linker to include our category methods in
32 // the static library. If these were compiled separately, the category methods
33 // below would be stripped by the linker.
35 #import "GPBWellKnownTypes.h"
37 #import "GPBUtilities_PackagePrivate.h"
39 NSString *const GPBWellKnownTypesErrorDomain =
40 GPBNSStringifySymbol(GPBWellKnownTypesErrorDomain);
42 static NSString *kTypePrefixGoogleApisCom = @"type.googleapis.com/";
44 static NSTimeInterval TimeIntervalFromSecondsAndNanos(int64_t seconds,
46 return seconds + (NSTimeInterval)nanos / 1e9;
49 static int32_t SecondsAndNanosFromTimeInterval(NSTimeInterval time,
51 BOOL nanosMustBePositive) {
52 NSTimeInterval seconds;
53 NSTimeInterval nanos = modf(time, &seconds);
55 if (nanosMustBePositive && (nanos < 0)) {
56 // Per Timestamp.proto, nanos is non-negative and "Negative second values with
57 // fractions must still have non-negative nanos values that count forward in
58 // time. Must be from 0 to 999,999,999 inclusive."
64 *outSeconds = (int64_t)seconds;
65 return (int32_t)nanos;
68 static NSString *BuildTypeURL(NSString *typeURLPrefix, NSString *fullName) {
69 if (typeURLPrefix.length == 0) {
73 if ([typeURLPrefix hasSuffix:@"/"]) {
74 return [typeURLPrefix stringByAppendingString:fullName];
77 return [NSString stringWithFormat:@"%@/%@", typeURLPrefix, fullName];
80 static NSString *ParseTypeFromURL(NSString *typeURLString) {
81 NSRange range = [typeURLString rangeOfString:@"/" options:NSBackwardsSearch];
82 if ((range.location == NSNotFound) ||
83 (NSMaxRange(range) == typeURLString.length)) {
86 NSString *result = [typeURLString substringFromIndex:range.location + 1];
90 #pragma mark - GPBTimestamp
92 @implementation GPBTimestamp (GBPWellKnownTypes)
94 - (instancetype)initWithDate:(NSDate *)date {
95 return [self initWithTimeIntervalSince1970:date.timeIntervalSince1970];
98 - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
99 if ((self = [super init])) {
101 int32_t nanos = SecondsAndNanosFromTimeInterval(
102 timeIntervalSince1970, &seconds, YES);
103 self.seconds = seconds;
110 return [NSDate dateWithTimeIntervalSince1970:self.timeIntervalSince1970];
113 - (void)setDate:(NSDate *)date {
114 self.timeIntervalSince1970 = date.timeIntervalSince1970;
117 - (NSTimeInterval)timeIntervalSince1970 {
118 return TimeIntervalFromSecondsAndNanos(self.seconds, self.nanos);
121 - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
124 SecondsAndNanosFromTimeInterval(timeIntervalSince1970, &seconds, YES);
125 self.seconds = seconds;
131 #pragma mark - GPBDuration
133 @implementation GPBDuration (GBPWellKnownTypes)
135 - (instancetype)initWithTimeInterval:(NSTimeInterval)timeInterval {
136 if ((self = [super init])) {
138 int32_t nanos = SecondsAndNanosFromTimeInterval(
139 timeInterval, &seconds, NO);
140 self.seconds = seconds;
146 - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
147 return [self initWithTimeInterval:timeIntervalSince1970];
150 - (NSTimeInterval)timeInterval {
151 return TimeIntervalFromSecondsAndNanos(self.seconds, self.nanos);
154 - (void)setTimeInterval:(NSTimeInterval)timeInterval {
157 SecondsAndNanosFromTimeInterval(timeInterval, &seconds, NO);
158 self.seconds = seconds;
162 - (NSTimeInterval)timeIntervalSince1970 {
163 return self.timeInterval;
166 - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 {
167 self.timeInterval = timeIntervalSince1970;
172 #pragma mark - GPBAny
174 @implementation GPBAny (GBPWellKnownTypes)
176 + (instancetype)anyWithMessage:(GPBMessage *)message
177 error:(NSError **)errorPtr {
178 return [self anyWithMessage:message
179 typeURLPrefix:kTypePrefixGoogleApisCom
183 + (instancetype)anyWithMessage:(GPBMessage *)message
184 typeURLPrefix:(NSString *)typeURLPrefix
185 error:(NSError **)errorPtr {
186 return [[[self alloc] initWithMessage:message
187 typeURLPrefix:typeURLPrefix
188 error:errorPtr] autorelease];
191 - (instancetype)initWithMessage:(GPBMessage *)message
192 error:(NSError **)errorPtr {
193 return [self initWithMessage:message
194 typeURLPrefix:kTypePrefixGoogleApisCom
198 - (instancetype)initWithMessage:(GPBMessage *)message
199 typeURLPrefix:(NSString *)typeURLPrefix
200 error:(NSError **)errorPtr {
203 if (![self packWithMessage:message
204 typeURLPrefix:typeURLPrefix
213 - (BOOL)packWithMessage:(GPBMessage *)message
214 error:(NSError **)errorPtr {
215 return [self packWithMessage:message
216 typeURLPrefix:kTypePrefixGoogleApisCom
220 - (BOOL)packWithMessage:(GPBMessage *)message
221 typeURLPrefix:(NSString *)typeURLPrefix
222 error:(NSError **)errorPtr {
223 NSString *fullName = [message descriptor].fullName;
224 if (fullName.length == 0) {
227 [NSError errorWithDomain:GPBWellKnownTypesErrorDomain
228 code:GPBWellKnownTypesErrorCodeFailedToComputeTypeURL
236 self.typeURL = BuildTypeURL(typeURLPrefix, fullName);
237 self.value = message.data;
241 - (GPBMessage *)unpackMessageClass:(Class)messageClass
242 error:(NSError **)errorPtr {
243 NSString *fullName = [messageClass descriptor].fullName;
244 if (fullName.length == 0) {
247 [NSError errorWithDomain:GPBWellKnownTypesErrorDomain
248 code:GPBWellKnownTypesErrorCodeFailedToComputeTypeURL
254 NSString *expectedFullName = ParseTypeFromURL(self.typeURL);
255 if ((expectedFullName == nil) || ![expectedFullName isEqual:fullName]) {
258 [NSError errorWithDomain:GPBWellKnownTypesErrorDomain
259 code:GPBWellKnownTypesErrorCodeTypeURLMismatch
265 // Any is proto3, which means no extensions, so this assumes anything put
266 // within an any also won't need extensions. A second helper could be added
268 return [messageClass parseFromData:self.value