| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2015 Google Inc. All rights reserved. | 2 // Copyright 2015 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 // Importing sources here to force the linker to include our category methods in | 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 | 32 // the static library. If these were compiled separately, the category methods |
| 33 // below would be stripped by the linker. | 33 // below would be stripped by the linker. |
| 34 | 34 |
| 35 #import "google/protobuf/Timestamp.pbobjc.m" |
| 36 #import "google/protobuf/Duration.pbobjc.m" |
| 35 #import "GPBWellKnownTypes.h" | 37 #import "GPBWellKnownTypes.h" |
| 36 | 38 |
| 37 #import "GPBUtilities_PackagePrivate.h" | |
| 38 | |
| 39 NSString *const GPBWellKnownTypesErrorDomain = | |
| 40 GPBNSStringifySymbol(GPBWellKnownTypesErrorDomain); | |
| 41 | |
| 42 static NSString *kTypePrefixGoogleApisCom = @"type.googleapis.com/"; | |
| 43 | |
| 44 static NSTimeInterval TimeIntervalSince1970FromSecondsAndNanos(int64_t seconds, | 39 static NSTimeInterval TimeIntervalSince1970FromSecondsAndNanos(int64_t seconds, |
| 45 int32_t nanos) { | 40 int32_t nanos) { |
| 46 return seconds + (NSTimeInterval)nanos / 1e9; | 41 return seconds + (NSTimeInterval)nanos / 1e9; |
| 47 } | 42 } |
| 48 | 43 |
| 49 static int32_t SecondsAndNanosFromTimeIntervalSince1970(NSTimeInterval time, | 44 static int32_t SecondsAndNanosFromTimeIntervalSince1970(NSTimeInterval time, |
| 50 int64_t *outSeconds) { | 45 int64_t *outSeconds) { |
| 51 NSTimeInterval seconds; | 46 NSTimeInterval seconds; |
| 52 NSTimeInterval nanos = modf(time, &seconds); | 47 NSTimeInterval nanos = modf(time, &seconds); |
| 53 nanos *= 1e9; | 48 nanos *= 1e9; |
| 54 *outSeconds = (int64_t)seconds; | 49 *outSeconds = (int64_t)seconds; |
| 55 return (int32_t)nanos; | 50 return (int32_t)nanos; |
| 56 } | 51 } |
| 57 | 52 |
| 58 static NSString *BuildTypeURL(NSString *typeURLPrefix, NSString *fullName) { | |
| 59 if (typeURLPrefix.length == 0) { | |
| 60 return fullName; | |
| 61 } | |
| 62 | |
| 63 if ([typeURLPrefix hasSuffix:@"/"]) { | |
| 64 return [typeURLPrefix stringByAppendingString:fullName]; | |
| 65 } | |
| 66 | |
| 67 return [NSString stringWithFormat:@"%@/%@", typeURLPrefix, fullName]; | |
| 68 } | |
| 69 | |
| 70 static NSString *ParseTypeFromURL(NSString *typeURLString) { | |
| 71 NSRange range = [typeURLString rangeOfString:@"/" options:NSBackwardsSearch]; | |
| 72 if ((range.location == NSNotFound) || | |
| 73 (NSMaxRange(range) == typeURLString.length)) { | |
| 74 return nil; | |
| 75 } | |
| 76 NSString *result = [typeURLString substringFromIndex:range.location + 1]; | |
| 77 return result; | |
| 78 } | |
| 79 | |
| 80 #pragma mark - GPBTimestamp | |
| 81 | |
| 82 @implementation GPBTimestamp (GBPWellKnownTypes) | 53 @implementation GPBTimestamp (GBPWellKnownTypes) |
| 83 | 54 |
| 84 - (instancetype)initWithDate:(NSDate *)date { | 55 - (instancetype)initWithDate:(NSDate *)date { |
| 85 return [self initWithTimeIntervalSince1970:date.timeIntervalSince1970]; | 56 return [self initWithTimeIntervalSince1970:date.timeIntervalSince1970]; |
| 86 } | 57 } |
| 87 | 58 |
| 88 - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1
970 { | 59 - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1
970 { |
| 89 if ((self = [super init])) { | 60 if ((self = [super init])) { |
| 90 int64_t seconds; | 61 int64_t seconds; |
| 91 int32_t nanos = SecondsAndNanosFromTimeIntervalSince1970( | 62 int32_t nanos = SecondsAndNanosFromTimeIntervalSince1970( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 111 - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 { | 82 - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 { |
| 112 int64_t seconds; | 83 int64_t seconds; |
| 113 int32_t nanos = | 84 int32_t nanos = |
| 114 SecondsAndNanosFromTimeIntervalSince1970(timeIntervalSince1970, &seconds); | 85 SecondsAndNanosFromTimeIntervalSince1970(timeIntervalSince1970, &seconds); |
| 115 self.seconds = seconds; | 86 self.seconds = seconds; |
| 116 self.nanos = nanos; | 87 self.nanos = nanos; |
| 117 } | 88 } |
| 118 | 89 |
| 119 @end | 90 @end |
| 120 | 91 |
| 121 #pragma mark - GPBDuration | |
| 122 | |
| 123 @implementation GPBDuration (GBPWellKnownTypes) | 92 @implementation GPBDuration (GBPWellKnownTypes) |
| 124 | 93 |
| 125 - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1
970 { | 94 - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1
970 { |
| 126 if ((self = [super init])) { | 95 if ((self = [super init])) { |
| 127 int64_t seconds; | 96 int64_t seconds; |
| 128 int32_t nanos = SecondsAndNanosFromTimeIntervalSince1970( | 97 int32_t nanos = SecondsAndNanosFromTimeIntervalSince1970( |
| 129 timeIntervalSince1970, &seconds); | 98 timeIntervalSince1970, &seconds); |
| 130 self.seconds = seconds; | 99 self.seconds = seconds; |
| 131 self.nanos = nanos; | 100 self.nanos = nanos; |
| 132 } | 101 } |
| 133 return self; | 102 return self; |
| 134 } | 103 } |
| 135 | 104 |
| 136 - (NSTimeInterval)timeIntervalSince1970 { | 105 - (NSTimeInterval)timeIntervalSince1970 { |
| 137 return TimeIntervalSince1970FromSecondsAndNanos(self.seconds, self.nanos); | 106 return TimeIntervalSince1970FromSecondsAndNanos(self.seconds, self.nanos); |
| 138 } | 107 } |
| 139 | 108 |
| 140 - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 { | 109 - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 { |
| 141 int64_t seconds; | 110 int64_t seconds; |
| 142 int32_t nanos = | 111 int32_t nanos = |
| 143 SecondsAndNanosFromTimeIntervalSince1970(timeIntervalSince1970, &seconds); | 112 SecondsAndNanosFromTimeIntervalSince1970(timeIntervalSince1970, &seconds); |
| 144 self.seconds = seconds; | 113 self.seconds = seconds; |
| 145 self.nanos = nanos; | 114 self.nanos = nanos; |
| 146 } | 115 } |
| 147 | 116 |
| 148 @end | 117 @end |
| 149 | |
| 150 #pragma mark - GPBAny | |
| 151 | |
| 152 @implementation GPBAny (GBPWellKnownTypes) | |
| 153 | |
| 154 + (instancetype)anyWithMessage:(GPBMessage *)message | |
| 155 error:(NSError **)errorPtr { | |
| 156 return [self anyWithMessage:message | |
| 157 typeURLPrefix:kTypePrefixGoogleApisCom | |
| 158 error:errorPtr]; | |
| 159 } | |
| 160 | |
| 161 + (instancetype)anyWithMessage:(GPBMessage *)message | |
| 162 typeURLPrefix:(NSString *)typeURLPrefix | |
| 163 error:(NSError **)errorPtr { | |
| 164 return [[[self alloc] initWithMessage:message | |
| 165 typeURLPrefix:typeURLPrefix | |
| 166 error:errorPtr] autorelease]; | |
| 167 } | |
| 168 | |
| 169 - (instancetype)initWithMessage:(GPBMessage *)message | |
| 170 error:(NSError **)errorPtr { | |
| 171 return [self initWithMessage:message | |
| 172 typeURLPrefix:kTypePrefixGoogleApisCom | |
| 173 error:errorPtr]; | |
| 174 } | |
| 175 | |
| 176 - (instancetype)initWithMessage:(GPBMessage *)message | |
| 177 typeURLPrefix:(NSString *)typeURLPrefix | |
| 178 error:(NSError **)errorPtr { | |
| 179 self = [self init]; | |
| 180 if (self) { | |
| 181 if (![self packWithMessage:message | |
| 182 typeURLPrefix:typeURLPrefix | |
| 183 error:errorPtr]) { | |
| 184 [self release]; | |
| 185 self = nil; | |
| 186 } | |
| 187 } | |
| 188 return self; | |
| 189 } | |
| 190 | |
| 191 - (BOOL)packWithMessage:(GPBMessage *)message | |
| 192 error:(NSError **)errorPtr { | |
| 193 return [self packWithMessage:message | |
| 194 typeURLPrefix:kTypePrefixGoogleApisCom | |
| 195 error:errorPtr]; | |
| 196 } | |
| 197 | |
| 198 - (BOOL)packWithMessage:(GPBMessage *)message | |
| 199 typeURLPrefix:(NSString *)typeURLPrefix | |
| 200 error:(NSError **)errorPtr { | |
| 201 NSString *fullName = [message descriptor].fullName; | |
| 202 if (fullName.length == 0) { | |
| 203 if (errorPtr) { | |
| 204 *errorPtr = | |
| 205 [NSError errorWithDomain:GPBWellKnownTypesErrorDomain | |
| 206 code:GPBWellKnownTypesErrorCodeFailedToComputeType
URL | |
| 207 userInfo:nil]; | |
| 208 } | |
| 209 return NO; | |
| 210 } | |
| 211 if (errorPtr) { | |
| 212 *errorPtr = nil; | |
| 213 } | |
| 214 self.typeURL = BuildTypeURL(typeURLPrefix, fullName); | |
| 215 self.value = message.data; | |
| 216 return YES; | |
| 217 } | |
| 218 | |
| 219 - (GPBMessage *)unpackMessageClass:(Class)messageClass | |
| 220 error:(NSError **)errorPtr { | |
| 221 NSString *fullName = [messageClass descriptor].fullName; | |
| 222 if (fullName.length == 0) { | |
| 223 if (errorPtr) { | |
| 224 *errorPtr = | |
| 225 [NSError errorWithDomain:GPBWellKnownTypesErrorDomain | |
| 226 code:GPBWellKnownTypesErrorCodeFailedToComputeType
URL | |
| 227 userInfo:nil]; | |
| 228 } | |
| 229 return nil; | |
| 230 } | |
| 231 | |
| 232 NSString *expectedFullName = ParseTypeFromURL(self.typeURL); | |
| 233 if ((expectedFullName == nil) || ![expectedFullName isEqual:fullName]) { | |
| 234 if (errorPtr) { | |
| 235 *errorPtr = | |
| 236 [NSError errorWithDomain:GPBWellKnownTypesErrorDomain | |
| 237 code:GPBWellKnownTypesErrorCodeTypeURLMismatch | |
| 238 userInfo:nil]; | |
| 239 } | |
| 240 return nil; | |
| 241 } | |
| 242 | |
| 243 // Any is proto3, which means no extensions, so this assumes anything put | |
| 244 // within an any also won't need extensions. A second helper could be added | |
| 245 // if needed. | |
| 246 return [messageClass parseFromData:self.value | |
| 247 error:errorPtr]; | |
| 248 } | |
| 249 | |
| 250 @end | |
| OLD | NEW |