Argo icon indicating copy to clipboard operation
Argo copied to clipboard

Relationships up the chain

Open eimantas opened this issue 8 years ago • 2 comments

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?

eimantas avatar Jun 30 '16 10:06 eimantas

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.

tonyd256 avatar Jul 07 '16 16:07 tonyd256

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

I have problem to make this work

Dragna avatar Aug 24 '16 16:08 Dragna