Skip to content

Commit

Permalink
AVRO Export includes "namespace"
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenchrist committed Apr 18, 2024
1 parent 847eae1 commit c2a5c63
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
10 changes: 6 additions & 4 deletions datacontract/export/avro_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@


def to_avro_schema(model_name, model) -> dict:
return to_avro_record(model_name, model.fields, model.description)
return to_avro_record(model_name, model.fields, model.description, model.namespace)


def to_avro_schema_json(model_name, model) -> str:
schema = to_avro_schema(model_name, model)
return json.dumps(schema, indent=2, sort_keys=False)


def to_avro_record(name, fields, description) -> dict:
def to_avro_record(name, fields, description, namespace) -> dict:
schema = {"type": "record", "name": name}
if description is not None:
schema["doc"] = description
if namespace is not None:
schema["namespace"] = namespace
schema["fields"] = to_avro_fields(fields)
return schema

Expand All @@ -35,7 +37,7 @@ def to_avro_field(field, field_name):
return avro_field


def to_avro_type(field: Field, field_name: str) -> str:
def to_avro_type(field: Field, field_name: str) -> str | dict:
if field.type is None:
return "null"
if field.type in ["string", "varchar", "text"]:
Expand All @@ -60,7 +62,7 @@ def to_avro_type(field: Field, field_name: str) -> str:
elif field.type in ["time"]:
return "long"
elif field.type in ["object", "record", "struct"]:
return to_avro_record(field_name, field.fields, field.description)
return to_avro_record(field_name, field.fields, field.description, None)
elif field.type in ["binary"]:
return "bytes"
elif field.type in ["array"]:
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/kafka-avro-remote/datacontract.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ models:
orders:
type: table
description: My Model
namespace: com.example.checkout
fields:
ordertime:
type: bigint
Expand Down
1 change: 1 addition & 0 deletions tests/test_export_avro.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def test_to_avro_schema():
}
],
"name": "orders",
"namespace": "com.example.checkout",
"doc": "My Model",
"type": "record"
}
Expand Down

0 comments on commit c2a5c63

Please sign in to comment.