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

Relationships up the chain #383

Open
eimantas opened this issue Jun 30, 2016 · 2 comments
Open

Relationships up the chain #383

eimantas opened this issue Jun 30, 2016 · 2 comments
Labels

Comments

@eimantas
Copy link

It's very easy in Argo to decode related objects if they are children. I have a situation where I want to link to the parent objects going up the chain.

Say I have an API that returns this hierarchy of objects:

  • Foo
    • Bar1
      • Baz11
    • Bar2
      • Baz21

When they are all decoded, i can get to the Baz21 by saying foo.bars[1].bazes[0]. Now, how, having bazes[0] can I refer back to the foo? The initialisation process is somewhat blackboxed in this case since each struct's decode method takes json only for own and child objects. And there is no reference for the parental objects at all.

Has anyone done anything similar to this?

@tonyd256
Copy link
Contributor

tonyd256 commented Jul 7, 2016

This particular use case is difficult with Argo while maintaining maximum type safety. We have been successful in the past in this by having our bazes decode function return a decoded function instead of the Baz object that takes the final argument and returns the Baz. For instance:

extension Baz: Decodable {
  static func decode(j: JSON) -> Decoded<String -> Baz> {
    return curry(Baz.init) <^> j <| "" <*> y <| ""
  }
}

Where that final String value needs to come from foo. Then in the Foo decode function, get the string and finish the application with <*>. It's a little weird though and we've been thinking of different methods to tackle this.

@Dragna
Copy link

Dragna commented Aug 24, 2016

@tonyd256
Thanks for the explanation, but what would be the implementation of decodefor the Barand Foo classes.

I have problem to make this work

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

No branches or pull requests

4 participants