'A union is encoded by first writing a long value indicating
the zero-based position within the union of the schema of its value.
The value is then encoded per the indicated schema within the union.'

def write_union(self, writers_schema, datum, encoder):

    index_of_schema = (-1)
    for (i, candidate_schema) in enumerate(writers_schema.schemas):
        if validate(candidate_schema, datum):
            index_of_schema = i
    if (index_of_schema < 0):
        raise AvroTypeException(writers_schema, datum)
    encoder.write_long(index_of_schema)
    self.write_data(writers_schema.schemas[index_of_schema], datum, encoder)
