Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 'Generic Response Object Serialization' section on README by using protocol extension #1257

Closed
wants to merge 1 commit into from

Conversation

raphaeloliveira
Copy link

Hi guys, I'm wondering if would be a good idea on README to use a protocol extension instead of a direct implementation of ResponseCollectionSerializable on User. This way it's easier to make any type conform to ResponseCollectionSerializable.

If you guys feel like a more functional approach I can change to

extension ResponseCollectionSerializable where Self: ResponseObjectSerializable {
    static func collection(response response: NSHTTPURLResponse, representation: AnyObject) -> [Self] {
        guard let representation = representation as? [[String: AnyObject]] else { return [] }
        return representation.flatMap { Self(response: response, representation: $0) }
    }
}

Happy to hear your thoughts. 🙂

@cnoon
Copy link
Member

cnoon commented Jun 12, 2016

I think this is a great improvement @raphaeloliveira...thanks for putting this together! I rebased your change and pushed in master as dae3ed1 while keeping your attribution.

Cheers. 🍻

@cnoon cnoon closed this Jun 12, 2016
@cnoon cnoon added this to the 3.5.0 milestone Jun 12, 2016
@cnoon cnoon self-assigned this Jun 12, 2016
@cnoon cnoon modified the milestones: 3.4.1, 3.5.0 Jun 13, 2016
@giofid
Copy link

giofid commented May 24, 2017

I think that in the Generic Response Object Serialization section on README

struct User: ResponseObjectSerializable, ResponseCollectionSerializable, CustomStringConvertible {
    let username: String
    let name: String

    var description: String {
        return "User: { username: \(username), name: \(name) }"
    }

    init?(response: HTTPURLResponse, representation: Any) {
        guard
            let username = response.url?.lastPathComponent,
            let representation = representation as? [String: Any],
            let name = representation["name"] as? String
        else { return nil }

        self.username = username
        self.name = name
    }
}

should be

struct User: ResponseObjectSerializable, CustomStringConvertible {
    let username: String
    let name: String

    var description: String {
        return "User: { username: \(username), name: \(name) }"
    }

    init?(response: HTTPURLResponse, representation: Any) {
        guard
            let username = response.url?.lastPathComponent,
            let representation = representation as? [String: Any],
            let name = representation["name"] as? String
        else { return nil }

        self.username = username
        self.name = name
    }
}

User shouldn't adopt the ResponseCollectionSerializable protocol.

@raphaeloliveira
Copy link
Author

Hi @giofid, why are you suggesting User should not be able to be parsed as a collection of Users?

@giofid
Copy link

giofid commented Jul 12, 2017

Hi @raphaeloliveira, you're absolutely right! My mistake, sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants