Created
February 12, 2015 18:13
-
-
Save nicolasbrugneaux/c3622f320d658729c1da to your computer and use it in GitHub Desktop.
swift experimentations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fieldAliases: [String: String] = | |
[ | |
"SCMActionConfirmed" : "confirmed", | |
"SCMAmount" : "amount", | |
"SCMBrand" : "brand", | |
"SCMCategory" : "category", | |
"SCMCurrency" : "currency", | |
"SCMDescription" : "description", | |
"SCMProductID" : "identifier", | |
"SCMProductImageURL" : "photo", | |
"SCMProductName" : "fn", | |
"SCMProductURL" : "url", | |
"SCMQuantity" : "quantity", | |
"SCMSalePrice" : "price", | |
"SCMScore" : "score", | |
"SCMTimestamp" : "date", | |
"SCMValidityTimestamp" : "valid" | |
] | |
let customerAliases: [String: String] = | |
[ | |
"SCMCustomerAgeGroup" :"agegroup", | |
"SCMCustomerEducation" :"education", | |
"SCMCustomerGender" :"gender", | |
"SCMCustomerID" :"identifier", | |
"SCMCustomerMHash" :"mhash", | |
"SCMCustomerSegment" :"segment", | |
"SCMCustomerTargeting" :"targeting" | |
] | |
let aliases: [String: String] = $.merge( dictionaries: fieldAliases, customerAliases ) | |
/** | |
* Function changes the keys of an object given by the client into the keys that the server expects. | |
*/ | |
private func mapAliases( parameters: [String: AnyObject] ) -> [String: AnyObject] | |
{ | |
var _parameters: [String: AnyObject] = [:] | |
for ( key, value ) in parameters | |
{ | |
var _key = nil != aliases[key] ? aliases[key]! : key | |
if let dictionary = value as? [String: AnyObject] | |
{ | |
_parameters[_key] = self.mapAliases( dictionary ) | |
} | |
else if let array = value as? [[String: AnyObject]] | |
{ | |
_parameters[_key] = array.map( self.mapAliases ) | |
//equivalent de | |
_parameters[_key] = array.map { self.mapAliases( $0 ) } | |
// mais en plus rapide, vu que y'a pas de creation de closures | |
} | |
else | |
{ | |
_parameters[_key] = value | |
} | |
} | |
return _parameters | |
} | |
/* | |
quand la signature de mapAliases c'etait | |
private func mapAliases( parameters: [String: AnyObject]? ) -> [String: AnyObject] | |
ca marchait pas, a cause de parameters qui etaient optionel ^ | |
du coup ca matchait pas la signature de la fonction `map` | |
probleme? pas d'erreur detectees par XCode, mais runtime error. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment