Created
January 22, 2018 17:08
-
-
Save juliengdt/d4e8509072ce93172300f42f43c57122 to your computer and use it in GitHub Desktop.
Backend dude, what have you done ?
This file contains 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
/* 1 - When your backend dev doesn't know how to REST*/ | |
let backEndLOL: [[String: String]] = [["Key":"CAPACITY","Value":"323"],["Key":"rate","Value":"3.00"]] | |
// Okay let's do his/her job | |
extension Collection where Iterator.Element == [String: String] { | |
var trueFuckingJson: [String: String] { | |
let tupleArray: [(String, String)] = self.map{ dict -> [String: String] in | |
guard let key = dict["Key"], let val = dict["Value"] else { return [:] } | |
return [key: val] | |
}.flatMap{ $0 } | |
return Dictionary<String, String>(tupleArray, uniquingKeysWith: { (first, last) in last }) | |
} | |
} | |
// For backend who piss on W3C recommandation on REST Services: LOWERCASED YOUR GOD DAMN KEYS | |
extension Dictionary where Key == String { | |
var respectW3C: Dictionary { | |
var newDictionary = Dictionary() | |
for k in keys { | |
newDictionary[k.lowercased()] = self[k] | |
} | |
return newDictionary | |
} | |
} | |
let frontEndReformat = backEndLOL.trueFuckingJson.respectW3C | |
/* 2 - When your backend dev DOES know how to REST and how to improve data consumption */ | |
let trueBackEnd: [String: String] = ["capacity":"323","rate":"3.00"] | |
// ... and in fact that's all |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment