Created
July 22, 2015 15:40
-
-
Save gfontenot/c0ea07d48c9225944c6a to your computer and use it in GitHub Desktop.
Example of using partial application as a way to solve side loaded JSON with immutable value objects
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
// Returning a function here, instead of a fully realized object | |
extension Company: Decodable { | |
static func decode(j: JSON) -> Decoded<[People] -> Company> { | |
return create | |
<^> j <| "id" | |
<*> j <| ["attributes", "name"] | |
} | |
} | |
func parse(j: JSON) -> Company? { | |
// "included" holds the related people for the company | |
let company = (j <| "data" >>- Company.decode) <*> j <|| "included" | |
return company.value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment