{"action":"created","issue":{"url":"https://api.github.com/repos/Hearst-DD/ObjectMapper/issues/384","repository_url":"https://api.github.com/repos/Hearst-DD/ObjectMapper","labels_url":"https://api.github.com/repos/Hearst-DD/ObjectMapper/issues/384/labels{/name}","comments_url":"https://api.github.com/repos/Hearst-DD/ObjectMapper/issues/384/comments","events_url":"https://api.github.com/repos/Hearst-DD/ObjectMapper/issues/384/events","html_url":"https://github.com/Hearst-DD/ObjectMapper/pull/384","id":132361313,"number":384,"title":"[Immutable] Make init throw + Transform & Mappable support","user":{"login":"ldiqual","id":941453,"avatar_url":"https://avatars.githubusercontent.com/u/941453?v=3","gravatar_id":"","url":"https://api.github.com/users/ldiqual","html_url":"https://github.com/ldiqual","followers_url":"https://api.github.com/users/ldiqual/followers","following_url":"https://api.github.com/users/ldiqual/following{/other_user}","gists_url":"https://api.github.com/users/ldiqual/gists{/gist_id}","starred_url":"https://api.github.com/users/ldiqual/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ldiqual/subscriptions","organizations_url":"https://api.github.com/users/ldiqual/orgs","repos_url":"https://api.github.com/users/ldiqual/repos","events_url":"https://api.github.com/users/ldiqual/events{/privacy}","received_events_url":"https://api.github.com/users/ldiqual/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":14,"created_at":"2016-02-09T08:45:10Z","updated_at":"2016-09-14T13:43:57Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/Hearst-DD/ObjectMapper/pulls/384","html_url":"https://github.com/Hearst-DD/ObjectMapper/pull/384","diff_url":"https://github.com/Hearst-DD/ObjectMapper/pull/384.diff","patch_url":"https://github.com/Hearst-DD/ObjectMapper/pull/384.patch"},"body":"@tristanhimmelman \r\n\r\nRelated to https://github.com/Hearst-DD/ObjectMapper/issues/383\r\n\r\nThis PR adds various improvements over the current immutable mapping support (see commented section in https://raw.githubusercontent.com/Hearst-DD/ObjectMapper/master/README.md)\r\n\r\n## Summary of changes\r\n\r\n### Immutable mapping\r\n\r\nImmutable mapping can now be written like this:\r\n\r\n```\r\nclass Base: Mappable {\r\n    let base: String\r\n    let date: NSDate\r\n\r\n    init(_ map: Map) throws {\r\n        base = try map[\"base\"].valueOrFail()\r\n        date = try map[\"date].valueWithTransformOrFail(DateTransform())\r\n        // And a lot more\r\n    }\r\n\r\n    mutating func mapping(map: Map) {\r\n        // unchanged\r\n    }\r\n}\r\n```\r\n\r\nThis required the following change in the `Mappable` protocol:\r\n\r\n```\r\npublic protocol Mappable {\r\n--\tinit?(_ map: Map)\r\n++   init(_ map: Map) throws\r\n```\r\n\r\nThis has a couple advantages:\r\n - There's no more need to call `map.isValid` at the end of the initializer.\r\n - You can exit the initializer early if one of the mappings failed.\r\n - You can add custom validation logic and throw an error if the mapping doesn't fit your needs.\r\n - The library can be extended to throw meaningful errors (like `couldn't map attribute \"base\" with type \"Base\", payload: \"123\"`). As of now it just throws a very basic `ErrorType` subclass (`MappingError`)\r\n\r\n### Transforms & Mappable\r\n\r\nAdded support for the following immutable mapping types. `valueOrFail()` throws, `value()` returns an optional.\r\n - Basic type T, with `T, [T], [String: T]`\r\n - Transforms mapping via `valueWithTransform/valueWithTransformOrFail`. `T, [T], [String: T]`\r\n - Mappable mapping, with `T, [T], [String: T]`.\r\n\r\n### ImmutableObjectTests\r\n\r\nMoved the small `Immutable` test from `ObjectMapperTests` to `ImmutableObjectTests` and added all cases that were covered in this PR. We might want to break it down into multiple tests, but I went for simplicity for this first draft.\r\n\r\n## Discussion\r\n\r\n@tristanhimmelman This PR's main goal is to kick off a conversation about immutable mapping. While I think the interface is acceptable, it's more meant to be a starting point to discuss how the library could be modified to support such a feature.\r\n\r\nI'd love your input on the following points:\r\n - What do you think about the `init` change? Do you think it's acceptable to break the interface for such a feature?\r\n - How about `mapping()`? Right now it's really awkward to have to switch on the mapping type, create variables and map them with an `inout` operator. I was thinking about the following:\r\n  - Only allow `JSON => object` mapping via `init(map: Mapping)` and drop the `<-` operator\r\n  - Add a `->` operator in `mapping(map: Mapping)` and rename that method to `serialize()`.\r\n\r\nAlso, if you'd rather go with another process to discuss immutability changes, just let me know! Thank you :)"},"comment":{"url":"https://api.github.com/repos/Hearst-DD/ObjectMapper/issues/comments/247016996","html_url":"https://github.com/Hearst-DD/ObjectMapper/pull/384#issuecomment-247016996","issue_url":"https://api.github.com/repos/Hearst-DD/ObjectMapper/issues/384","id":247016996,"user":{"login":"hebertialmeida","id":103670,"avatar_url":"https://avatars.githubusercontent.com/u/103670?v=3","gravatar_id":"","url":"https://api.github.com/users/hebertialmeida","html_url":"https://github.com/hebertialmeida","followers_url":"https://api.github.com/users/hebertialmeida/followers","following_url":"https://api.github.com/users/hebertialmeida/following{/other_user}","gists_url":"https://api.github.com/users/hebertialmeida/gists{/gist_id}","starred_url":"https://api.github.com/users/hebertialmeida/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hebertialmeida/subscriptions","organizations_url":"https://api.github.com/users/hebertialmeida/orgs","repos_url":"https://api.github.com/users/hebertialmeida/repos","events_url":"https://api.github.com/users/hebertialmeida/events{/privacy}","received_events_url":"https://api.github.com/users/hebertialmeida/received_events","type":"User","site_admin":false},"created_at":"2016-09-14T13:43:57Z","updated_at":"2016-09-14T13:43:57Z","body":"@tristanhimmelman Thanks, moved to Unbox (https://github.com/JohnSundell/Unbox) and Wrap (https://github.com/JohnSundell/Wrap)."}}
{"actor":{"display_login":"hebertialmeida"}}