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

Feature: mappable meta #12

Merged
merged 10 commits into from
May 10, 2016
Merged

Feature: mappable meta #12

merged 10 commits into from
May 10, 2016

Conversation

vadymmarkov
Copy link
Contributor

@zenangst
Got a new idea regarding our discussions about meta. So instead of using enums or magic generators we can use Mappable structs 😄

struct ProductMeta {
  var id = 0
  var name: String?
}

extension ProductMeta: Mappable {
  init(_ map: JSONDictionary) {
    id <- map.property("id")
    name <- map.property("name")
  }
}

let productMeta = ProductMeta(id: 11, name: "Name")
let viewModel = ViewModel(title: "Title", meta: productMeta)
// ...
let meta: ProductMeta = viewModel.metaInstance()
print(meta.id) // => 11

My only concern is than it might be too much code, because you have to implement mapping, don't see any way to avoid this, even though property key always reflects the property name. But with this approach you write string only once which is nice.

@zenangst
Copy link
Contributor

@vadymmarkov what about the README? 😁
Btw, this is bloody fantastic...

0d41808f60af8871fa122b3b0f37ab1b_360 gif png

@zenangst zenangst merged commit d951259 into master May 10, 2016
@zenangst zenangst deleted the feature/mappable-meta branch May 10, 2016 10:27
@onmyway133
Copy link
Contributor

onmyway133 commented May 10, 2016

@vadymmarkov 🎉

My wonder is about this let viewModel = ViewModel(title: "Title", meta: productMeta)
What if title also goes into ProductMeta? So there is no ViewModel anymore?

@vadymmarkov
Copy link
Contributor Author

This is interesting idea @onmyway133. What do you think @zenangst? It could be some additional protocol, so you should have title, subtitle, kind and so on on your struct. But it's Mappable at the same time.

@zenangst
Copy link
Contributor

I like this idea! 😁

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

Successfully merging this pull request may close these issues.

3 participants