Skip to content

Commit

Permalink
Add 2 more reading methods for dictionary reader.
Browse files Browse the repository at this point in the history
  • Loading branch information
lxcid committed Jul 6, 2015
1 parent 5d7e9a6 commit 74ab324
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions LXSupportKit/LXDictionaryReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ NS_ASSUME_NONNULL_BEGIN

- (nullable NSArray *)arrayForKey:(id<NSCopying>)key;
- (nullable NSNumber *)booleanForKey:(id<NSCopying>)key;
- (nullable NSDictionary *)dictionaryForKey:(id<NSCopying>)key;
- (nullable NSNumber *)numberForKey:(id<NSCopying>)key;
- (nullable NSDate *)iso8601DateForKey:(id<NSCopying>)key;
- (nullable NSString *)strictStringForKey:(id<NSCopying>)key;
- (nullable NSString *)stringForKey:(id<NSCopying>)key;

@end
Expand Down
23 changes: 22 additions & 1 deletion LXSupportKit/LXDictionaryReader.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ - (nullable NSNumber *)booleanForKey:(nonnull id<NSCopying>)key {
return [LXBooleanTransformer fromValue:value];
}

- (nullable NSDictionary *)dictionaryForKey:(id<NSCopying>)key {
id value = self.dictionary[key];
return [self _dictionaryFromValue:value];
}

- (nullable NSNumber *)numberForKey:(nonnull id<NSCopying>)key {
id value = self.dictionary[key];
return [LXNumberTransformer fromValue:value];
Expand All @@ -44,14 +49,19 @@ - (nullable NSDate *)iso8601DateForKey:(nonnull id<NSCopying>)key {
return [LXISO8601DateTransformer fromValue:value];
}

- (nullable NSString *)strictStringForKey:(nonnull id<NSCopying>)key {
id value = self.dictionary[key];
return [LXStrictStringTransformer fromValue:value];
}

- (nullable NSString *)stringForKey:(nonnull id<NSCopying>)key {
id value = self.dictionary[key];
return [LXStringTransformer fromValue:value];
}

#pragma mark - Private

- (NSArray *)_arrayFromValue:(id)value {
- (nullable NSArray *)_arrayFromValue:(nullable id)value {
if (!value) {
return nil;
}
Expand All @@ -62,4 +72,15 @@ - (NSArray *)_arrayFromValue:(id)value {
}
}

- (nullable NSDictionary *)_dictionaryFromValue:(nullable id)value {
if (!value) {
return nil;
}
if ([value isKindOfClass:[NSDictionary class]]) {
return value;
} else {
return nil;
}
}

@end
6 changes: 6 additions & 0 deletions LXSupportKitTests/LXDictionaryReaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class LXDictionaryReaderTests: XCTestCase {
"true" : true,
"array" : [],
"string" : "Hello World!",
"dict" : [ "Hello" : "World" ]
])
XCTAssertEqual(reader.arrayForKey("array")! as NSArray, [])
XCTAssertNil(reader.arrayForKey("one"))
Expand All @@ -25,5 +26,10 @@ class LXDictionaryReaderTests: XCTestCase {
XCTAssertEqual(reader.booleanForKey("true")!, true)
XCTAssertEqual(reader.iso8601DateForKey("ISO8601Date")!, NSDate(timeIntervalSince1970: 1434911058))
XCTAssertEqual(reader.stringForKey("string")!, "Hello World!")
XCTAssertEqual(reader.dictionaryForKey("dict")! as NSDictionary, [ "Hello" : "World" ])
XCTAssertNil(reader.dictionaryForKey("one"))
XCTAssertNil(reader.dictionaryForKey("nil"))
XCTAssertEqual(reader.strictStringForKey("string")!, "Hello World!")
XCTAssertNil(reader.strictStringForKey("one"))
}
}

0 comments on commit 74ab324

Please sign in to comment.