Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@juliengdt
Created January 22, 2018 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliengdt/d4e8509072ce93172300f42f43c57122 to your computer and use it in GitHub Desktop.
Save juliengdt/d4e8509072ce93172300f42f43c57122 to your computer and use it in GitHub Desktop.
Backend dude, what have you done ?
/* 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