forked from firebase/firebase-ios-sdk
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFUNSerializer.m
239 lines (220 loc) · 7.47 KB
/
FUNSerializer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2017 Google
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import"Functions/FirebaseFunctions/FUNSerializer.h"
#import"Functions/FirebaseFunctions/FUNUsageValidation.h"
#import"Functions/FirebaseFunctions/Public/FirebaseFunctions/FIRError.h"
NS_ASSUME_NONNULL_BEGIN
staticNSString *constkLongType = @"type.googleapis.com/google.protobuf.Int64Value";
staticNSString *constkUnsignedLongType = @"type.googleapis.com/google.protobuf.UInt64Value";
staticNSString *constkDateType = @"type.googleapis.com/google.protobuf.Timestamp";
@interfaceFUNSerializer () {
NSDateFormatter *_dateFormatter;
}
@end
@implementationFUNSerializer
- (instancetype)init {
self = [superinit];
if (self) {
_dateFormatter = [[NSDateFormatteralloc] init];
_dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
_dateFormatter.timeZone = [NSTimeZonetimeZoneWithName:@"UTC"];
}
return self;
}
- (id)encodeNumber:(NSNumber *)number {
// Recover the underlying type of the number, using the method described here:
// http://stackoverflow.com/questions/2518761/get-type-of-nsnumber
constchar *cType = [number objCType];
// Type Encoding values taken from
// https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/
// Articles/ocrtTypeEncodings.html
switch (cType[0]) {
case'q':
// "long long" might be larger than JS supports, so make it a string.
return @{
@"@type" : kLongType,
@"value" : [NSStringstringWithFormat:@"%@", number],
};
case'Q':
// "unsigned long long" might be larger than JS supports, so make it a string.
return @{
@"@type" : kUnsignedLongType,
@"value" : [NSStringstringWithFormat:@"%@", number],
};
case'i':
case's':
case'l':
case'I':
case'S':
// If it's an integer that isn't too long, so just use the number.
return number;
case'f':
case'd':
// It's a float/double that's not too large.
return number;
case'B':
case'c':
case'C':
// Boolean values are weird.
//
// On arm64, objCType of a BOOL-valued NSNumber will be "c", even though @encode(BOOL)
// returns "B". "c" is the same as @encode(signed char). Unfortunately this means that
// legitimate usage of signed chars is impossible, but this should be rare.
//
// Just return Boolean values as-is.
return number;
default:
// All documented codes should be handled above, so this shouldn't happen.
FUNThrowInvalidArgument(@"Unknown NSNumber objCType %s on %@", cType, number);
}
}
- (id)encode:(id)object {
if ([object isEqual:[NSNullnull]]) {
return object;
}
if ([object isKindOfClass:[NSNumberclass]]) {
return [selfencodeNumber:object];
}
if ([object isKindOfClass:[NSStringclass]]) {
return object;
}
if ([object isKindOfClass:[NSDictionaryclass]]) {
NSMutableDictionary *encoded = [NSMutableDictionarydictionary];
[object
enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL *_Nonnull stop) {
encoded[key] = [selfencode:obj];
}];
return encoded;
}
if ([object isKindOfClass:[NSArrayclass]]) {
NSMutableArray *encoded = [NSMutableArrayarrayWithCapacity:[object count]];
for (id obj in object) {
[encoded addObject:[selfencode:obj]];
}
return encoded;
}
// TODO(klimt): Add this back when we support NSDate.
/*
if ([object isKindOfClass:[NSDate class]]) {
NSString *iso8601 = [_dateFormatter stringFromDate:object];
return @{
@"@type" : kDateType,
@"value" : iso8601,
};
}
*/
FUNThrowInvalidArgument(@"Unsupported type: %@ for value %@", NSStringFromClass([object class]),
object);
}
NSError *FUNInvalidNumberError(id value, id wrapped) {
NSString *description = [NSStringstringWithFormat:@"Invalid number: %@ for %@", value, wrapped];
NSDictionary *userInfo = @{
NSLocalizedDescriptionKey : description,
};
return [NSErrorerrorWithDomain:FIRFunctionsErrorDomain
code:FIRFunctionsErrorCodeInternal
userInfo:userInfo];
}
- (nullable id)decodeWrappedType:(NSDictionary *)wrappederror:(NSError **)error {
NSAssert(error, @"error must not be nil");
NSString *type = wrapped[@"@type"];
NSString *value = wrapped[@"value"];
if (!value) {
returnnil;
}
if ([type isEqualToString:kLongType]) {
NSNumberFormatter *formatter = [[NSNumberFormatteralloc] init];
NSNumber *n = [formatter numberFromString:value];
if (n == nil) {
if (error != NULL) {
*error = FUNInvalidNumberError(value, wrapped);
}
returnnil;
}
return n;
} elseif ([type isEqualToString:kUnsignedLongType]) {
// NSNumber formatter doesn't handle unsigned long long, so we have to parse it.
constchar *str = value.UTF8String;
char *end = NULL;
unsignedlonglong n = strtoull(str, &end, 10);
if (errno == ERANGE) {
// This number was actually too big for an unsigned long long.
if (error != NULL) {
*error = FUNInvalidNumberError(value, wrapped);
}
returnnil;
}
if (*end) {
// The whole string wasn't parsed.
if (error != NULL) {
*error = FUNInvalidNumberError(value, wrapped);
}
returnnil;
}
return @(n);
}
returnnil;
}
- (nullable id)decode:(id)objecterror:(NSError **)error {
NSAssert(error, @"error must not be nil");
if ([object isKindOfClass:[NSDictionaryclass]]) {
if (object[@"@type"]) {
id result = [selfdecodeWrappedType:object error:error];
if (*error) {
returnnil;
}
if (result) {
return result;
}
// Treat unknown types as dictionaries, so we don't crash old clients when we add types.
}
NSMutableDictionary *decoded = [NSMutableDictionarydictionary];
__block NSError *decodeError = nil;
[object
enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL *_Nonnull stop) {
id decodedItem = [selfdecode:obj error:&decodeError];
if (decodeError) {
*stop = YES;
return;
}
decoded[key] = decodedItem;
}];
if (decodeError) {
if (error != NULL) {
*error = decodeError;
}
returnnil;
}
return decoded;
}
if ([object isKindOfClass:[NSArrayclass]]) {
NSMutableArray *result = [NSMutableArrayarrayWithCapacity:[object count]];
for (id obj in object) {
id decoded = [selfdecode:obj error:error];
if (*error) {
returnnil;
}
[result addObject:decoded];
}
return result;
}
if ([object isKindOfClass:[NSNumberclass]] || [object isKindOfClass:[NSStringclass]] ||
[object isEqual:[NSNullnull]]) {
return object;
}
FUNThrowInvalidArgument(@"Unsupported type: %@ for value %@", NSStringFromClass([object class]),
object);
}
@end
NS_ASSUME_NONNULL_END