Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit e011808

Browse files
committed
Recursively add array items and support strings and numbers
1 parent e08a686 commit e011808

File tree

2 files changed

+74
-64
lines changed

2 files changed

+74
-64
lines changed

packages/in_app_purchase/in_app_purchase_storekit/example/ios/RunnerTests/TranslatorTests.m

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -158,50 +158,59 @@ - (void)testError {
158158
XCTAssertEqualObjects(map, self.errorMap);
159159
}
160160

161+
- (void)testErrorWithNSNumberAsUserInfo {
162+
NSError *error = [NSError errorWithDomain:SKErrorDomain
163+
code:3
164+
userInfo:@{ @"key": @42 }];
165+
NSDictionary *expectedMap = @{
166+
@"domain" : SKErrorDomain,
167+
@"code" : @3,
168+
@"userInfo" : @{ @"key": @42 }
169+
};
170+
NSDictionary *map = [FIAObjectTranslator getMapFromNSError:error];
171+
XCTAssertEqualObjects(expectedMap, map);
172+
}
173+
161174
- (void)testErrorWithMultipleUnderlyingErrors {
162-
if (@available(iOS 10.0, *)) {
163-
NSError *underlyingErrorOne = [NSError errorWithDomain:SKErrorDomain code:2 userInfo:nil];
164-
NSError *underlyingErrorTwo = [NSError errorWithDomain:SKErrorDomain code:1 userInfo:nil];
165-
NSError *mainError = [NSError
166-
errorWithDomain:SKErrorDomain
167-
code:3
168-
userInfo:@{@"underlyingErrors" : @[ underlyingErrorOne, underlyingErrorTwo ]}];
169-
NSDictionary *expectedMap = @{
170-
@"domain" : SKErrorDomain,
171-
@"code" : @3,
172-
@"userInfo" : @{
173-
@"underlyingErrors" : @[
174-
@{@"domain" : SKErrorDomain, @"code" : @2, @"userInfo" : @{}},
175-
@{@"domain" : SKErrorDomain, @"code" : @1, @"userInfo" : @{}}
176-
]
177-
}
178-
};
179-
NSDictionary *map = [FIAObjectTranslator getMapFromNSError:mainError];
180-
XCTAssertEqualObjects(expectedMap, map);
181-
}
175+
NSError *underlyingErrorOne = [NSError errorWithDomain:SKErrorDomain code:2 userInfo:nil];
176+
NSError *underlyingErrorTwo = [NSError errorWithDomain:SKErrorDomain code:1 userInfo:nil];
177+
NSError *mainError = [NSError
178+
errorWithDomain:SKErrorDomain
179+
code:3
180+
userInfo:@{@"underlyingErrors" : @[ underlyingErrorOne, underlyingErrorTwo ]}];
181+
NSDictionary *expectedMap = @{
182+
@"domain" : SKErrorDomain,
183+
@"code" : @3,
184+
@"userInfo" : @{
185+
@"underlyingErrors" : @[
186+
@{@"domain" : SKErrorDomain, @"code" : @2, @"userInfo" : @{}},
187+
@{@"domain" : SKErrorDomain, @"code" : @1, @"userInfo" : @{}}
188+
]
189+
}
190+
};
191+
NSDictionary *map = [FIAObjectTranslator getMapFromNSError:mainError];
192+
XCTAssertEqualObjects(expectedMap, map);
182193
}
183194

184195
- (void)testErrorWithUnsupportedUserInfo {
185-
if (@available(iOS 10.0, *)) {
186-
NSError *error = [NSError errorWithDomain:SKErrorDomain
187-
code:3
188-
userInfo:@{@"user_info" : [[NSObject alloc] init]}];
189-
NSDictionary *expectedMap = @{
190-
@"domain" : SKErrorDomain,
191-
@"code" : @3,
192-
@"userInfo" : @{
193-
@"user_info" : [NSString
194-
stringWithFormat:
195-
@"Unable to encode native userInfo object of type %@ to map. Please submit an "
196-
@"issue at https://github.com/flutter/flutter/issues/new with the title "
197-
@"\"[in_app_purchase_storekit] Unable to encode userInfo of type %@\" and add "
198-
@"reproduction steps and the error details in the description field.",
199-
[NSObject class], [NSObject class]]
200-
}
201-
};
202-
NSDictionary *map = [FIAObjectTranslator getMapFromNSError:error];
203-
XCTAssertEqualObjects(expectedMap, map);
204-
}
196+
NSError *error = [NSError errorWithDomain:SKErrorDomain
197+
code:3
198+
userInfo:@{@"user_info" : [[NSObject alloc] init]}];
199+
NSDictionary *expectedMap = @{
200+
@"domain" : SKErrorDomain,
201+
@"code" : @3,
202+
@"userInfo" : @{
203+
@"user_info" : [NSString
204+
stringWithFormat:
205+
@"Unable to encode native userInfo object of type %@ to map. Please submit an "
206+
@"issue at https://github.com/flutter/flutter/issues/new with the title "
207+
@"\"[in_app_purchase_storekit] Unable to encode userInfo of type %@\" and add "
208+
@"reproduction steps and the error details in the description field.",
209+
[NSObject class], [NSObject class]]
210+
}
211+
};
212+
NSDictionary *map = [FIAObjectTranslator getMapFromNSError:error];
213+
XCTAssertEqualObjects(expectedMap, map);
205214
}
206215

207216
- (void)testLocaleToMap {

packages/in_app_purchase/in_app_purchase_storekit/ios/Classes/FIAObjectTranslator.m

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -168,37 +168,38 @@ + (NSDictionary *)getMapFromNSError:(NSError *)error {
168168
return nil;
169169
}
170170

171-
NSString *unsupportedMessage =
172-
@"Unable to encode native userInfo object of type %@ to map. Please submit an issue at "
173-
@"https://github.com/flutter/flutter/issues/new with the title \"[in_app_purchase_storekit] "
174-
@"Unable to encode userInfo of type %@\" and add reproduction steps and the error details in "
175-
@"the description field.";
176-
177171
NSMutableDictionary *userInfo = [NSMutableDictionary new];
178172
for (NSErrorUserInfoKey key in error.userInfo) {
179173
id value = error.userInfo[key];
180-
if ([value isKindOfClass:[NSError class]]) {
181-
userInfo[key] = [FIAObjectTranslator getMapFromNSError:value];
182-
} else if ([value isKindOfClass:[NSURL class]]) {
183-
userInfo[key] = [value absoluteString];
184-
} else if ([value isKindOfClass:[NSArray class]]) {
185-
NSMutableArray *errors = [[NSMutableArray alloc] init];
186-
for (id error in value) {
187-
if ([error isKindOfClass:[NSError class]]) {
188-
[errors addObject:[FIAObjectTranslator getMapFromNSError:error]];
189-
} else {
190-
[errors addObject:[NSString
191-
stringWithFormat:unsupportedMessage, [value class], [value class]]];
192-
}
193-
}
194-
userInfo[key] = errors;
195-
} else {
196-
userInfo[key] = [NSString stringWithFormat:unsupportedMessage, [value class], [value class]];
197-
}
174+
userInfo[key] = [FIAObjectTranslator encodeNSErrorUserInfo:value];
198175
}
199176
return @{@"code" : @(error.code), @"domain" : error.domain ?: @"", @"userInfo" : userInfo};
200177
}
201178

179+
+ (id)encodeNSErrorUserInfo:(id) value {
180+
if ([value isKindOfClass:[NSError class]]) {
181+
return [FIAObjectTranslator getMapFromNSError:value];
182+
} else if ([value isKindOfClass:[NSURL class]]) {
183+
return [value absoluteString];
184+
} else if ([value isKindOfClass:[NSNumber class]]) {
185+
return value;
186+
} else if ([value isKindOfClass:[NSString class]]) {
187+
return value;
188+
} else if ([value isKindOfClass:[NSArray class]]) {
189+
NSMutableArray *errors = [[NSMutableArray alloc] init];
190+
for (id error in value) {
191+
[errors addObject:[FIAObjectTranslator encodeNSErrorUserInfo:error]];
192+
}
193+
return errors;
194+
} else {
195+
return [NSString stringWithFormat:@"Unable to encode native userInfo object of type %@ to map. Please submit an issue at "
196+
@"https://github.com/flutter/flutter/issues/new with the title \"[in_app_purchase_storekit] "
197+
@"Unable to encode userInfo of type %@\" and add reproduction steps and the error details in "
198+
@"the description field.", [value class], [value class]];
199+
}
200+
201+
}
202+
202203
+ (NSDictionary *)getMapFromSKStorefront:(SKStorefront *)storefront {
203204
if (!storefront) {
204205
return nil;

0 commit comments

Comments
 (0)