Skip to content

Commit

Permalink
Fix an issue with typed config value injection, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
nickynick committed May 14, 2016
1 parent 4967ba5 commit 2356498
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
63 changes: 30 additions & 33 deletions Source/Definition/Injections/TyphoonInjectionByObjectFromString.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,50 +50,47 @@ - (void)valueToInjectWithContext:(TyphoonInjectionContext *)context completion:(
TyphoonTypeDescriptor *type = context.destinationType;
TyphoonComponentFactory *factory = context.factory;

id value = nil;

if (type) {
value = [self convertText:self.textValue toType:type withTypeConverterRegistry:factory.typeConverterRegistry];
}
else {
value = [self convertText:self.textValue withTypeConverterRegistry:factory.typeConverterRegistry];
}
id value = [self convertText:self.textValue toType:type withTypeConverterRegistry:factory.typeConverterRegistry];

result(value);
}

- (id)convertText:(NSString *)text toType:(TyphoonTypeDescriptor *)type withTypeConverterRegistry:(TyphoonTypeConverterRegistry *)typeConverterRegistry
{
if (type.isPrimitive) {
TyphoonPrimitiveTypeConverter *converter = [typeConverterRegistry primitiveTypeConverter];
return [converter valueFromText:text withType:type];
// First, let's see if the text explicitly states the value type.
NSString *typeStringFromText = [TyphoonTypeConversionUtils typeFromTextValue:text];
if (typeStringFromText) {
id <TyphoonTypeConverter> converter = [typeConverterRegistry converterForType:typeStringFromText];
if (converter) {
return [converter convert:text];
}
}
else {
NSString *typeString;

if (type.typeBeingDescribed) {
typeString = NSStringFromClass(type.typeBeingDescribed);

// In case we know the type from the method argument, let's try to use it.
if (type) {
if (type.isPrimitive) {
TyphoonPrimitiveTypeConverter *converter = [typeConverterRegistry primitiveTypeConverter];
return [converter valueFromText:text withType:type];
}
else {
typeString = [NSString stringWithFormat:@"<%@>", type.declaredProtocol];
}

id<TyphoonTypeConverter> converter = [typeConverterRegistry converterForType:typeString];
return [converter convert:text];
}
}

- (id)convertText:(NSString *)text withTypeConverterRegistry:(TyphoonTypeConverterRegistry *)typeConverterRegistry
{
id result = text;
NSString *typeString = [TyphoonTypeConversionUtils typeFromTextValue:text];
if (typeString) {
id <TyphoonTypeConverter> converter = [typeConverterRegistry converterForType:typeString];
if (converter) {
result = [converter convert:text];
NSString *typeString;

if (type.typeBeingDescribed) {
typeString = NSStringFromClass(type.typeBeingDescribed);
}
else {
typeString = [NSString stringWithFormat:@"<%@>", type.declaredProtocol];
}

id<TyphoonTypeConverter> converter = [typeConverterRegistry converterForType:typeString];
if (converter) {
return [converter convert:text];
}
}
}
return result;

// Fallback to injecting string.
return text;
}

- (NSUInteger)customHash
Expand Down
2 changes: 2 additions & 0 deletions Tests/Configuration/TyphoonConfigPostProcessorTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,15 @@ - (void)test_plist_values
[initializer injectParameterWith:TyphoonConfig(@"plist.damsels")];
}];
[knightDefinition injectProperty:@selector(hasHorseWillTravel) with:TyphoonConfig(@"plist.hasHorse")];
[knightDefinition injectProperty:@selector(foobar) with:TyphoonConfig(@"plist.fooUrl")];
[factory registerDefinition:knightDefinition];

[self postProcessFactory:factory withPostProcessor:_configurer];

Knight *knight = [factory componentForType:[Knight class]];
XCTAssertEqual(knight.damselsRescued, (NSUInteger)28);
XCTAssertEqual(knight.hasHorseWillTravel, (BOOL)YES);
XCTAssertEqualObjects(knight.foobar, [NSURL URLWithString:@"http://google.com/"]);
}

- (void)test_config_as_runtime_argument
Expand Down
2 changes: 2 additions & 0 deletions Tests/Resources/SomeProperties.plist
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<true/>
<key>damsels</key>
<integer>28</integer>
<key>fooUrl</key>
<string>NSURL(http://google.com/)</string>
</dict>
</dict>
</plist>

0 comments on commit 2356498

Please sign in to comment.