Created
February 7, 2019 10:25
-
-
Save kkirsanov/9b1ccbb4f5d9e116049044d73ec46dbd to your computer and use it in GitHub Desktop.
maybe enum in AVRO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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