DEV Community

Discussion on: Help, these objects have the same shape but don't share an interface!

Collapse
 
alainvanhout profile image
Alain Van Hout • Edited

If you’ll forgive my crossing from C# into Java (where I feel more comfortable), this is how I would approach this issue:

  1. create a concrete class or set of classes that are essentially the shared parts of the various versions of their objects
  2. Translate their object to yours, using recursive reflection to go over each field of your own concrete class. For each field of the top/root object: 2.1. Get the associated value from the e.g. V1 version of their object 2.2. Translate that value into your version 2.3. Put the translated version into the field of your object

It’s probably a bit of work, but it’ll at least remove the differences you are blocked by. In case it’s needed, the same/mirrored way can be used to convert back into any version of theirs.


EDIT:

Scratch all that. There is a far simpler way, based on the following pseudocode:
FooV1 fooV1 = fromJson(toJson(fooV2), FooV1)

Basically, given that you only care about the overlapping fields, you can just translate between the different version by using json as a go-between. That way, you still retain the benefits of static typing without any real effort.