Skip to content

Instantly share code, notes, and snippets.

@kkirsanov
Created February 7, 2019 10:25
Show Gist options
  • Save kkirsanov/9b1ccbb4f5d9e116049044d73ec46dbd to your computer and use it in GitHub Desktop.
Save kkirsanov/9b1ccbb4f5d9e116049044d73ec46dbd to your computer and use it in GitHub Desktop.
maybe enum in AVRO
from io import BytesIO
import fastavro
schema = {
"fields": [
{
"name": "maybe_enum",
"type": ["null", {
"type": "enum",
"name": "methods",
"symbols": [
"POST",
"GET",
"PUT",
"DELETE",
"OPTIONS",
"HEAD"
]
}],
},
],
"namespace": "namespace",
"name": "name",
"type": "record"
}
def serialize(schema, data):
bytes_writer = BytesIO()
fastavro.schemaless_writer(bytes_writer, schema, data)
return bytes_writer.getvalue()
def deserialize(schema, binary):
bytes_writer = BytesIO()
bytes_writer.write(binary)
bytes_writer.seek(0)
res = fastavro.schemaless_reader(bytes_writer, schema)
return res
# ok
binary = serialize(schema, dict(maybe_enum=None))
print(deserialize(schema, binary))
binary = serialize(schema, dict(maybe_enum="GET"))
print(deserialize(schema, binary))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment