utahiosmac / Marshal

Marshaling the typeless wild west of [String: Any]
MIT License
698 stars 63 forks source link

Adding marshaled() to Array<Marshaling> #104

Open pepasflo opened 7 years ago

pepasflo commented 7 years ago

Hey! Thanks for writing Marshal.

Consider these nested types:

struct Person {
    let name: String
}

struct Group {
    let name: String
    let members: [Person]
}

I'm guessing the implementation of Marshaling on Group.members would look like this?

extension Person: Marshaling {
    func marshaled() -> [String: Any] {
        return ["name": self.name]
    }
}

extension Group: Marshaling {
    func marshaled() -> [String: Any] {
        return [
            "name": name,
            "members": self.members.map { $0.marshaled() }
        ]
    }
}

Is that the expected way to do that, or did I miss something obvious?

This PR adds Marshaling on Array<Marshaling>, so that you can do this instead:

extension Group: Marshaling {
    func marshaled() -> [String: Any] {
        return [
            "name": name,
            "members": self.members.marshaled()
        ]
    }
}
jarsen commented 7 years ago

Hey! Yeah, using map would be the correct way to do that.

As to the addition, I think that looks great. Let us mull it over for a day or two though. Recently we've had a lot of great contributions but Marshal has really started to grow beyond it's original simple and small state. Let's get some discussion @bwhiteley what do you think? I think this is a good addition.

jarsen commented 7 years ago

Hey @pepasflo. I'm good to merge this. Would you be willing to write up a few tests?

pepasflo commented 7 years ago

@jarsen sure, good idea! I'll get this done within the next few days.

gaming-hacker commented 6 years ago

Did this get merged?

JetForMe commented 1 year ago

I'd like to see this merged, too!