SRserves85 / avro-to-python

Light tool for compiling avro schema files (.avsc) to python classes
MIT License
25 stars 19 forks source link

Support union as array item type #30

Closed fmiguelez closed 1 year ago

fmiguelez commented 1 year ago

Defining an array with an item of type "union" is not supported and raises following error:

    Traceback (most recent call last):
      File "/usr/local/bin/avro-to-python", line 8, in <module>
        sys.exit(main())
      File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1128, in __call__
        return self.main(*args, **kwargs)
      File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1053, in main
        rv = self.invoke(ctx)
      File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1395, in invoke
        return ctx.invoke(self.callback, **ctx.params)
      File "/usr/local/lib/python3.9/site-packages/click/core.py", line 754, in invoke
        return __callback(*args, **kwargs)
      File "/usr/local/lib/python3.9/site-packages/avro_to_python/cli.py", line 34, in main
        reader.read()
      File "/usr/local/lib/python3.9/site-packages/avro_to_python/reader/read.py", line 81, in read
        self._build_namespace_tree()
      File "/usr/local/lib/python3.9/site-packages/avro_to_python/reader/read.py", line 164, in _build_namespace_tree
        _record_file(file, item, queue)
      File "/usr/local/lib/python3.9/site-packages/avro_to_python/utils/avro/files/record.py", line 80, in _record_file
        field = _union_field(
      File "/usr/local/lib/python3.9/site-packages/avro_to_python/utils/avro/types/union.py", line 80, in _union_field
        kwargs['union_types'].append(_array_field(field={'name': 'arraytype', 'type': typ}, parent_namespace=parent_namespace, queue=queue, references=references))
      File "/usr/local/lib/python3.9/site-packages/avro_to_python/utils/avro/types/array.py", line 50, in _array_field
        field_item_type = _get_field_type(
      File "/usr/local/lib/python3.9/site-packages/avro_to_python/utils/avro/types/type_factory.py", line 22, in _get_field_type
        if isinstance(field['type'], dict):
    TypeError: list indices must be integers or slices, not str
    EXIT on build_script with code 1

This is an example of AVSC that triggers the error:

{
    "type": "record",
    "name": "RecordWithArrayOfUnion",
    "namespace": "records",
    "fields": [
        {
            "name": "arrayOfUnion",
            "type": {
                "type": "array",
                "items": [
                    {
                        "type": "array",
                        "items": {
                            "type": "Thing"
                        }
                    },
                    "int"
                ]
            }
        }
    ]
}