{"action":"created","issue":{"url":"https://api.github.com/repos/thoughtbot/Argo/issues/316","labels_url":"https://api.github.com/repos/thoughtbot/Argo/issues/316/labels{/name}","comments_url":"https://api.github.com/repos/thoughtbot/Argo/issues/316/comments","events_url":"https://api.github.com/repos/thoughtbot/Argo/issues/316/events","html_url":"https://github.com/thoughtbot/Argo/issues/316","id":125483787,"number":316,"title":"Optional Date Parse","user":{"login":"RLovelett","id":335572,"avatar_url":"https://avatars.githubusercontent.com/u/335572?v=3","gravatar_id":"","url":"https://api.github.com/users/RLovelett","html_url":"https://github.com/RLovelett","followers_url":"https://api.github.com/users/RLovelett/followers","following_url":"https://api.github.com/users/RLovelett/following{/other_user}","gists_url":"https://api.github.com/users/RLovelett/gists{/gist_id}","starred_url":"https://api.github.com/users/RLovelett/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RLovelett/subscriptions","organizations_url":"https://api.github.com/users/RLovelett/orgs","repos_url":"https://api.github.com/users/RLovelett/repos","events_url":"https://api.github.com/users/RLovelett/events{/privacy}","received_events_url":"https://api.github.com/users/RLovelett/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-01-07T21:02:30Z","updated_at":"2016-01-07T21:24:34Z","closed_at":"2016-01-07T21:24:34Z","body":"I'm hoping this is possible and my hope is that my functional foo is not high enough yet to understand how to do this properly yet.\r\n\r\nEffectively what I'd like to do is decode a `JSON.String` into a `NSDate` and fail if it doesn't decode properly and for another decode a `JSON.String` into a `NSDate?`. For the sake of this discussion the two properties are `always` and `sometimes`. As you can probably infer `always` should fail decoding if it doesn't decode properly and `sometimes` should decode successfully and return `Optional.None` if it doesn't.\r\n\r\nBelow is some code that gets me most of the way there.\r\n\r\n```swift2\r\nstruct DecodedObj: Decodable {\r\n  let always: NSDate\r\n  var sometimes: NSDate?\r\n\r\n  internal static func decode(json: JSON) -> Decoded<DecodedObj> {\r\n    return curry(DecodedObj.init)\r\n      <^> json <| \"always\"\r\n      <*> json <|? \"sometimes\"\r\n  }\r\n}\r\n\r\nextension NSDate: Decodable {\r\n  public typealias DecodedType = NSDate\r\n\r\n  static func parseISO8601(string: String,\r\n    locale: NSLocale = NSLocale.autoupdatingCurrentLocale(),\r\n    timeZone: NSTimeZone = NSTimeZone.systemTimeZone()) -> NSDate?\r\n  { ... }\r\n\r\n  public static func decode(json: JSON) -> Decoded<NSDate> {\r\n    switch json {\r\n    case .String(let jsonString):\r\n      guard let result = NSDate.parseISO8601(jsonString).map(pure) else { fallthrough }\r\n      return result\r\n    default:\r\n      return .typeMismatch(\"ISO8601 Date\", actual: json)\r\n    }\r\n  }\r\n}\r\n```\r\n\r\n```json\r\n{\r\n  \"always\": \"2016-01-05T070808.34+00:00\",\r\n  \"sometimes\": \"2016-01-05T070808.34+00:00\"\r\n}\r\n```\r\n:heavy_check_mark:  Expect `sometimes` to be `Decoded.Success(Optional.Some(NSDate))`.\r\n\r\n```json\r\n{\r\n  \"always\": \"2016-01-05T070808.34+00:00\",\r\n  \"sometimes\": \"TODO\"\r\n}\r\n```\r\n:x: Expect `sometimes` to be `Decoded.Success(Optional.None)`. Get `Decoded.Failure()`\r\n\r\n```json\r\n{\r\n  \"always\": \"2016-01-05T070808.34+00:00\",\r\n  \"sometimes\": null\r\n}\r\n```\r\n:heavy_check_mark: Expect `sometimes` to be `Decoded.Success(Optional.None)`.\r\n\r\nI guess the first question: is this even possible with Argo? The second is how would you go about it?\r\n\r\n(Sorry if this is the wrong place. Or if it's been asked, I tried searching to no avail.)"},"comment":{"url":"https://api.github.com/repos/thoughtbot/Argo/issues/comments/169809155","html_url":"https://github.com/thoughtbot/Argo/issues/316#issuecomment-169809155","issue_url":"https://api.github.com/repos/thoughtbot/Argo/issues/316","id":169809155,"user":{"login":"RLovelett","id":335572,"avatar_url":"https://avatars.githubusercontent.com/u/335572?v=3","gravatar_id":"","url":"https://api.github.com/users/RLovelett","html_url":"https://github.com/RLovelett","followers_url":"https://api.github.com/users/RLovelett/followers","following_url":"https://api.github.com/users/RLovelett/following{/other_user}","gists_url":"https://api.github.com/users/RLovelett/gists{/gist_id}","starred_url":"https://api.github.com/users/RLovelett/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RLovelett/subscriptions","organizations_url":"https://api.github.com/users/RLovelett/orgs","repos_url":"https://api.github.com/users/RLovelett/repos","events_url":"https://api.github.com/users/RLovelett/events{/privacy}","received_events_url":"https://api.github.com/users/RLovelett/received_events","type":"User","site_admin":false},"created_at":"2016-01-07T21:24:34Z","updated_at":"2016-01-07T21:24:34Z","body":"Gracias. So simple I wish I had realized that _without_ asking. I feel foolish now.\r\n\r\nAgain thank you. :clap: "}}