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" | |
37 #import "GPBWellKnownTypes.h" | 35 #import "GPBWellKnownTypes.h" |
38 | 36 |
| 37 #import "GPBUtilities_PackagePrivate.h" |
| 38 |
| 39 NSString *const GPBWellKnownTypesErrorDomain = |
| 40 GPBNSStringifySymbol(GPBWellKnownTypesErrorDomain); |
| 41 |
| 42 static NSString *kTypePrefixGoogleApisCom = @"type.googleapis.com/"; |
| 43 |
39 static NSTimeInterval TimeIntervalSince1970FromSecondsAndNanos(int64_t seconds, | 44 static NSTimeInterval TimeIntervalSince1970FromSecondsAndNanos(int64_t seconds, |
40 int32_t nanos) { | 45 int32_t nanos) { |
41 return seconds + (NSTimeInterval)nanos / 1e9; | 46 return seconds + (NSTimeInterval)nanos / 1e9; |
42 } | 47 } |
43 | 48 |
44 static int32_t SecondsAndNanosFromTimeIntervalSince1970(NSTimeInterval time, | 49 static int32_t SecondsAndNanosFromTimeIntervalSince1970(NSTimeInterval time, |
45 int64_t *outSeconds) { | 50 int64_t *outSeconds) { |
46 NSTimeInterval seconds; | 51 NSTimeInterval seconds; |
47 NSTimeInterval nanos = modf(time, &seconds); | 52 NSTimeInterval nanos = modf(time, &seconds); |
48 nanos *= 1e9; | 53 nanos *= 1e9; |
49 *outSeconds = (int64_t)seconds; | 54 *outSeconds = (int64_t)seconds; |
50 return (int32_t)nanos; | 55 return (int32_t)nanos; |
51 } | 56 } |
52 | 57 |
| 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 |
53 @implementation GPBTimestamp (GBPWellKnownTypes) | 82 @implementation GPBTimestamp (GBPWellKnownTypes) |
54 | 83 |
55 - (instancetype)initWithDate:(NSDate *)date { | 84 - (instancetype)initWithDate:(NSDate *)date { |
56 return [self initWithTimeIntervalSince1970:date.timeIntervalSince1970]; | 85 return [self initWithTimeIntervalSince1970:date.timeIntervalSince1970]; |
57 } | 86 } |
58 | 87 |
59 - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1
970 { | 88 - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1
970 { |
60 if ((self = [super init])) { | 89 if ((self = [super init])) { |
61 int64_t seconds; | 90 int64_t seconds; |
62 int32_t nanos = SecondsAndNanosFromTimeIntervalSince1970( | 91 int32_t nanos = SecondsAndNanosFromTimeIntervalSince1970( |
(...skipping 19 matching lines...) Expand all Loading... |
82 - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 { | 111 - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 { |
83 int64_t seconds; | 112 int64_t seconds; |
84 int32_t nanos = | 113 int32_t nanos = |
85 SecondsAndNanosFromTimeIntervalSince1970(timeIntervalSince1970, &seconds); | 114 SecondsAndNanosFromTimeIntervalSince1970(timeIntervalSince1970, &seconds); |
86 self.seconds = seconds; | 115 self.seconds = seconds; |
87 self.nanos = nanos; | 116 self.nanos = nanos; |
88 } | 117 } |
89 | 118 |
90 @end | 119 @end |
91 | 120 |
| 121 #pragma mark - GPBDuration |
| 122 |
92 @implementation GPBDuration (GBPWellKnownTypes) | 123 @implementation GPBDuration (GBPWellKnownTypes) |
93 | 124 |
94 - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1
970 { | 125 - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1
970 { |
95 if ((self = [super init])) { | 126 if ((self = [super init])) { |
96 int64_t seconds; | 127 int64_t seconds; |
97 int32_t nanos = SecondsAndNanosFromTimeIntervalSince1970( | 128 int32_t nanos = SecondsAndNanosFromTimeIntervalSince1970( |
98 timeIntervalSince1970, &seconds); | 129 timeIntervalSince1970, &seconds); |
99 self.seconds = seconds; | 130 self.seconds = seconds; |
100 self.nanos = nanos; | 131 self.nanos = nanos; |
101 } | 132 } |
102 return self; | 133 return self; |
103 } | 134 } |
104 | 135 |
105 - (NSTimeInterval)timeIntervalSince1970 { | 136 - (NSTimeInterval)timeIntervalSince1970 { |
106 return TimeIntervalSince1970FromSecondsAndNanos(self.seconds, self.nanos); | 137 return TimeIntervalSince1970FromSecondsAndNanos(self.seconds, self.nanos); |
107 } | 138 } |
108 | 139 |
109 - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 { | 140 - (void)setTimeIntervalSince1970:(NSTimeInterval)timeIntervalSince1970 { |
110 int64_t seconds; | 141 int64_t seconds; |
111 int32_t nanos = | 142 int32_t nanos = |
112 SecondsAndNanosFromTimeIntervalSince1970(timeIntervalSince1970, &seconds); | 143 SecondsAndNanosFromTimeIntervalSince1970(timeIntervalSince1970, &seconds); |
113 self.seconds = seconds; | 144 self.seconds = seconds; |
114 self.nanos = nanos; | 145 self.nanos = nanos; |
115 } | 146 } |
116 | 147 |
117 @end | 148 @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 |