Skip to content

Commit

Permalink
Merge pull request appsquickly#442 from appsquickly/type-converters-fix
Browse files Browse the repository at this point in the history
Removed the singletone TyphoonTypeConverterRegistry
  • Loading branch information
jasperblues committed Nov 4, 2015
2 parents 916c75e + 4d97ea4 commit 980baaf
Show file tree
Hide file tree
Showing 27 changed files with 536 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,27 @@ - (BOOL)isEqualToCustom:(TyphoonInjectionByObjectFromString *)injection
- (void)valueToInjectWithContext:(TyphoonInjectionContext *)context completion:(TyphoonInjectionValueBlock)result
{
TyphoonTypeDescriptor *type = context.destinationType;
TyphoonComponentFactory *factory = context.factory;

id value = nil;

if (type.isPrimitive) {
TyphoonPrimitiveTypeConverter *converter = [[TyphoonTypeConverterRegistry shared] primitiveTypeConverter];
TyphoonPrimitiveTypeConverter *converter = [factory.typeConverterRegistry primitiveTypeConverter];
value = [converter valueFromText:self.textValue withType:type];
}
else {
value = [self convertText:self.textValue];
value = [self convertText:self.textValue withTypeConverterRegistry:factory.typeConverterRegistry];
}

result(value);
}

- (id)convertText:(NSString *)text
- (id)convertText:(NSString *)text withTypeConverterRegistry:(TyphoonTypeConverterRegistry *)typeConverterRegistry
{
id result = text;
NSString *typeString = [TyphoonTypeConverterRegistry typeFromTextValue:text];
NSString *typeString = [TyphoonTypeConversionUtils typeFromTextValue:text];
if (typeString) {
id <TyphoonTypeConverter> converter = [[TyphoonTypeConverterRegistry shared] converterForType:typeString];
id <TyphoonTypeConverter> converter = [typeConverterRegistry converterForType:typeString];
if (converter) {
result = [converter convert:text];
}
Expand Down
4 changes: 4 additions & 0 deletions Source/Factory/TyphoonComponentFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
@class TyphoonDefinition;
@class TyphoonCallStack;
@class TyphoonRuntimeArguments;
@class TyphoonTypeConverterRegistry;

/**
*
Expand Down Expand Up @@ -102,6 +103,7 @@ Attach a TyphoonComponentFactoryPostProcessor to this component factory.
id <TyphoonComponentsPool> _weakSingletons;

TyphoonCallStack *_stack;
TyphoonTypeConverterRegistry *_typeConverterRegistry;
NSMutableArray *_definitionPostProcessors;
NSMutableArray *_instancePostProcessors;
BOOL _isLoading;
Expand Down Expand Up @@ -172,6 +174,8 @@ Attach a TyphoonComponentFactoryPostProcessor to this component factory.

- (NSArray *)registry;

- (TyphoonTypeConverterRegistry *)typeConverterRegistry;

- (void)enumerateDefinitions:(void(^)(TyphoonDefinition *definition, NSUInteger index, TyphoonDefinition **definitionToReplace, BOOL *stop))block;


Expand Down
6 changes: 6 additions & 0 deletions Source/Factory/TyphoonComponentFactory.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#import "TyphoonWeakComponentsPool.h"
#import "TyphoonFactoryAutoInjectionPostProcessor.h"
#import "TyphoonStackElement.h"
#import "TyphoonTypeConverterRegistry.h"

@interface TyphoonDefinition (TyphoonComponentFactory)

Expand Down Expand Up @@ -75,6 +76,7 @@ - (id)init
_weakSingletons = [TyphoonWeakComponentsPool new];
_objectGraphSharedInstances = (id<TyphoonComponentsPool>)[[NSMutableDictionary alloc] init];
_stack = [TyphoonCallStack stack];
_typeConverterRegistry = [[TyphoonTypeConverterRegistry alloc] init];
_definitionPostProcessors = [[NSMutableArray alloc] init];
_instancePostProcessors = [[NSMutableArray alloc] init];
[self attachPostProcessor:[TyphoonParentReferenceHydratingPostProcessor new]];
Expand Down Expand Up @@ -210,6 +212,10 @@ - (void)makeDefault
}
}

- (TyphoonTypeConverterRegistry *)typeConverterRegistry {
return _typeConverterRegistry;
}

- (NSArray *)registry
{
[self loadIfNeeded];
Expand Down
2 changes: 1 addition & 1 deletion Source/Factory/TyphoonDefinitionRegisterer.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ - (void)registerInfrastructureComponentFromDefinition
[_componentFactory addInstancePostProcessor:infrastructureComponent];
}
else if ([_definition.type conformsToProtocol:@protocol(TyphoonTypeConverter)]) {
[[TyphoonTypeConverterRegistry shared] registerTypeConverter:infrastructureComponent];
[_componentFactory.typeConverterRegistry registerTypeConverter:infrastructureComponent];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ - (id)supportedType

- (id)convert:(NSString *)stringValue
{
stringValue = [TyphoonTypeConverterRegistry textWithoutTypeFromTextValue:stringValue];
stringValue = [TyphoonTypeConversionUtils textWithoutTypeFromTextValue:stringValue];

NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ - (id)supportedType

- (id)convert:(NSString *)stringValue
{
stringValue = [TyphoonTypeConverterRegistry textWithoutTypeFromTextValue:stringValue];
stringValue = [TyphoonTypeConversionUtils textWithoutTypeFromTextValue:stringValue];
__autoreleasing NSURL *url = [NSURL URLWithString:stringValue];
return url;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ - (id)supportedType

- (id)convert:(NSString *)stringValue
{
stringValue = [TyphoonTypeConverterRegistry textWithoutTypeFromTextValue:stringValue];
stringValue = [TyphoonTypeConversionUtils textWithoutTypeFromTextValue:stringValue];

if (_isMutable) {
return [NSMutableString stringWithString:stringValue];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import <Foundation/Foundation.h>
#import "TyphoonTypeConverter.h"

@class TyphoonTypeDescriptor;

@interface TyphoonPrimitiveTypeConverter : NSObject

Expand Down
27 changes: 27 additions & 0 deletions Source/TypeConversion/Helpers/TyphoonColorConversionUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
////////////////////////////////////////////////////////////////////////////////
//
// TYPHOON FRAMEWORK
// Copyright 2015, Typhoon Framework Contributors
// All Rights Reserved.
//
// NOTICE: The authors permit you to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////

#import <Foundation/Foundation.h>

struct RGBA
{
CGFloat red;
CGFloat green;
CGFloat blue;
CGFloat alpha;
} rgba;

@interface TyphoonColorConversionUtils : NSObject

+ (struct RGBA)colorFromHexString:(NSString *)hexString;
+ (struct RGBA)colorFromCssStyleString:(NSString *)cssString;

@end
64 changes: 64 additions & 0 deletions Source/TypeConversion/Helpers/TyphoonColorConversionUtils.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
////////////////////////////////////////////////////////////////////////////////
//
// TYPHOON FRAMEWORK
// Copyright 2015, Typhoon Framework Contributors
// All Rights Reserved.
//
// NOTICE: The authors permit you to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////

#import "TyphoonColorConversionUtils.h"

@implementation TyphoonColorConversionUtils

+ (struct RGBA)colorFromHexString:(NSString *)hexString {
hexString =
[[hexString stringByReplacingOccurrencesOfString:@"#" withString:@""] stringByReplacingOccurrencesOfString:@"0x" withString:@""];

unsigned int red, green, blue, alpha;
if ([hexString length] == 6) {
sscanf([hexString UTF8String], "%02X%02X%02X", &red, &green, &blue);
alpha = 255;
}
else if ([hexString length] == 8) {
sscanf([hexString UTF8String], "%02X%02X%02X%02X", &alpha, &red, &green, &blue);
}
else {
[NSException raise:NSInvalidArgumentException format:@"%@ requires a six or eight digit hex string.",
NSStringFromClass([self class])];
}
return [self colorFromRed:red green:green blue:blue alpha:(CGFloat)(alpha / 255.0)];
}

+ (struct RGBA)colorFromCssStyleString:(NSString *)cssString {
NSArray *colorComponents = [cssString componentsSeparatedByString:@","];

unsigned int red, green, blue;
float alpha;
if ([colorComponents count] == 3) {
sscanf([cssString UTF8String], "%d,%d,%d", &red, &green, &blue);
alpha = 1.0;
}
else if ([colorComponents count] == 4) {
sscanf([cssString UTF8String], "%d,%d,%d,%f", &red, &green, &blue, &alpha);
}
else {
[NSException raise:NSInvalidArgumentException format:@"%@ requires css style format UIColor(r,g,b) or UIColor(r,g,b,a).",
NSStringFromClass([self class])];
}
return [self colorFromRed:red green:green blue:blue alpha:alpha];
}

+ (struct RGBA)colorFromRed:(NSUInteger)red green:(NSUInteger)green blue:(NSUInteger)blue alpha:(CGFloat)alpha
{
struct RGBA color;
color.red = (CGFloat)red / 255.0;
color.green = (CGFloat)green / 255.0;
color.blue = (CGFloat)blue / 255.0;
color.alpha = (CGFloat)alpha;
return color;
}

@end
29 changes: 29 additions & 0 deletions Source/TypeConversion/TyphoonTypeConversionUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
////////////////////////////////////////////////////////////////////////////////
//
// TYPHOON FRAMEWORK
// Copyright 2015, Typhoon Framework Contributors
// All Rights Reserved.
//
// NOTICE: The authors permit you to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////

#import <Foundation/Foundation.h>

/**
* A collection of helper methods for type conversion purposes
*/
@interface TyphoonTypeConversionUtils : NSObject

/**
* Returns the type from the text, e.g. NSURL for NSURL(http://typhoonframework.org)
*/
+ (NSString *)typeFromTextValue:(NSString *)textValue;

/**
* Returns the type from the text, e.g. http://typhoonframework.org for NSURL(http://typhoonframework.org)
*/
+ (NSString *)textWithoutTypeFromTextValue:(NSString *)textValue;

@end
45 changes: 45 additions & 0 deletions Source/TypeConversion/TyphoonTypeConversionUtils.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
////////////////////////////////////////////////////////////////////////////////
//
// TYPHOON FRAMEWORK
// Copyright 2015, Typhoon Framework Contributors
// All Rights Reserved.
//
// NOTICE: The authors permit you to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////

#import "TyphoonTypeConversionUtils.h"

@implementation TyphoonTypeConversionUtils

+ (NSString *)typeFromTextValue:(NSString *)textValue
{
NSString *type = nil;

NSRange openBraceRange = [textValue rangeOfString:@"("];
BOOL hasBraces = [textValue hasSuffix:@")"] && openBraceRange.location != NSNotFound;
if (hasBraces) {
type = [textValue substringToIndex:openBraceRange.location];
}

return type;
}

+ (NSString *)textWithoutTypeFromTextValue:(NSString *)textValue
{
NSString *result = textValue;

NSRange openBraceRange = [textValue rangeOfString:@"("];
BOOL hasBraces = [textValue hasSuffix:@")"] && openBraceRange.location != NSNotFound;

if (hasBraces) {
NSRange range = NSMakeRange(openBraceRange.location + openBraceRange.length, 0);
range.length = [textValue length] - range.location - 1;
result = [textValue substringWithRange:range];
}

return result;
}

@end
2 changes: 1 addition & 1 deletion Source/TypeConversion/TyphoonTypeConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#import <Foundation/Foundation.h>
#import "TyphoonTypeConverterRegistry.h"

#import "TyphoonTypeConversionUtils.h"

/**
* Declares a contract for converting configuration arguments to their required runtime type.
Expand Down
15 changes: 0 additions & 15 deletions Source/TypeConversion/TyphoonTypeConverterRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,12 @@
#import <Foundation/Foundation.h>

@protocol TyphoonTypeConverter;
@class TyphoonTypeDescriptor;
@class TyphoonPrimitiveTypeConverter;


/**
* Registry of type converters, with special treatment for primitives.
*/
@interface TyphoonTypeConverterRegistry : NSObject
{
TyphoonPrimitiveTypeConverter *_primitiveTypeConverter;
NSMutableDictionary *_typeConverters;
}

/**
* Returns the shard/default registry instance used by the container.
*/
+ (TyphoonTypeConverterRegistry *)shared;


+ (NSString *)typeFromTextValue:(NSString *)textValue;
+ (NSString *)textWithoutTypeFromTextValue:(NSString *)textValue;

/**
* Returns the type converter for the given type string. Usually type is class of object you want to convert.
Expand Down
Loading

0 comments on commit 980baaf

Please sign in to comment.