You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Rob Torop <ro...@gmail.com> on 2017/12/25 14:34:56 UTC

Polymorphic lists

What's best practice for a situation like this? I want to have a
polymorphic list, which presumably has to be a list where the elements are
a union.  Then how best to let consuming code know what kind of object each
list element is? For example:

I have Animal, which can be Goat, Hen or Worm. I want a schema that
represents an AnimalList - a list of animals along with a name for the list.

When code is interpreting the individual animals in an AnimalList, it needs
to know whether an individual Animal is a Goat, Hen or Worm.  I am
wondering the best way to approach it.

Choice 1

{ "name"  : "my animal pals",
 "animals" : [
                      {
                         "type" : "goat",
                         "name" : "caneater",
                         "animal" : { "horn_length" : 12 }
                     },

                          "type" : "hen",
                          "name" : "madameclucko",
                         "animal" : { "eggs_per_day" : 2 }
                     },
                           "type" : "hen",
                          "name":"agoodoldhen",
                         "animal" : { "eggs_per_day" : 12 }
                     },
       ]
}

Choice 2

{ "name"  : "my animal pals",
 "animals" : [
                      {

                         "name" : "caneater",
                         "animal" : { "type" : "goat", "horn_length" : 12 }
                     },


                          "name" : "madameclucko",
                         "animal" : { "type" : "hen", "eggs_per_day" : 2 }
                     },

                          "name":"agoodoldhen",
                         "animal" : { "type":"hen", "eggs_per_day" : 12 }
                     },
       ]
}


Choice 3

{ "name"  : "my animal pals",
 "animals" : [
                      {

                         "name" : "caneater",
                         "animal" : {
"goat" : {horn_length" : 12 }
                     },


                          "name" : "madameclucko",
                         "animal" : { "type" : "hen", "eggs_per_day" : 2 }
                     },

                          "name":"agoodoldhen",
                         "animal" : { "type":"hen", "eggs_per_day" : 12 }
                     },
       ]
}