Example #1
0
def deserialize(
    data: bytes,
    schema: typing.Dict,
    serialization_type: str = "avro",
    writer_schema: typing.Optional[typing.Dict] = None,
) -> typing.Dict:
    if serialization_type == "avro":
        input_stream: typing.Union[io.BytesIO, io.StringIO] = io.BytesIO(data)

        payload = fastavro.schemaless_reader(
            input_stream,
            writer_schema=writer_schema or schema,
            reader_schema=schema,
        )

    elif serialization_type == "avro-json":
        input_stream = io.StringIO(data.decode())
        # This is an iterator, but not a container
        records = fastavro.json_reader(input_stream, schema)
        # records can have multiple payloads, but in this case we return the first one
        payload = list(records)[0]
    else:
        raise ValueError(
            f"Serialization type should be `avro` or `avro-json`, not {serialization_type}"
        )

    input_stream.flush()

    return payload
Example #2
0
def roundtrip(schema, records):
    new_file = StringIO()
    json_writer(new_file, schema, records)
    new_file.seek(0)

    new_records = list(json_reader(new_file, schema))
    return new_records
Example #3
0
 def callback(self, ch, method, properties, body):
     if body:
         read_schema_response = self._schema_registry.get_schema(
             properties.headers[MESSAGE_PROPERTY_SUBJECT],
             properties.headers[MESSAGE_PROPERTY_VERSION])
         read_schema_obj = json.loads(
             read_schema_response[RESPONSE_KEY_SCHEMA])
         read_schema = parse_schema(read_schema_obj)
         string_reader = StringIO(body.decode("utf-8"))
         for sample in json_reader(string_reader, read_schema):
             print(sample)
     else:
         print("There was no body with the message - try again.")
Example #4
0
def test_default_value_missing():
    """https://github.com/fastavro/fastavro/issues/485"""
    schema = {
        "type": "record",
        "name": "test_default_value_missing",
        "fields": [{"name": "string", "type": "string"}],
    }

    record = {}

    new_file = StringIO(json.dumps(record))
    with pytest.raises(ValueError, match="no value and no default"):
        next(json_reader(new_file, schema))
Example #5
0
def test_default_union_values():
    """https://github.com/fastavro/fastavro/issues/485"""
    schema = {
        "type": "record",
        "name": "User",
        "fields": [
            {"name": "name", "type": "string"},
            {"name": "age", "type": "long"},
            {
                "name": "pets",
                "type": {"type": "array", "items": "string"},
            },
            {
                "name": "accounts",
                "type": {"type": "map", "values": "long"},
            },
            {
                "name": "favorite_colors",
                "type": {
                    "type": "enum",
                    "name": "favorite_color",
                    "symbols": ["BLUE", "YELLOW", "GREEN"],
                },
            },
            {"name": "country", "type": ["string", "null"], "default": "Argentina"},
            {"name": "address", "type": ["null", "string"], "default": None},
        ],
        "doc": "An User",
        "namespace": "User.v1",
        "aliases": ["user-v1", "super user"],
    }

    record = {
        "name": "MgXqfDAqzbgJSTTHDXtN",
        "age": 551,
        "pets": ["aRvwODwbOWfrkxYYkJiI"],
        "accounts": {"DQSZRzofFrNCiOhhIOvX": 4431},
        "favorite_colors": "GREEN",
        "address": {"string": "YgmVDKhXctMgODKkhNHJ"},
    }

    new_file = StringIO(json.dumps(record))
    read_record = next(json_reader(new_file, schema))

    assert read_record["country"] == "Argentina"
Example #6
0
    def decode(self, message: bytes, version: str) -> Any:
        schema_response = self._schema_response(version)
        string_reader = StringIO(message.decode("utf-8"))

        return fastavro.json_reader(string_reader,
                                    self._schema(schema_response))
Example #7
0
def test_all_default_values():
    """https://github.com/fastavro/fastavro/issues/485"""
    default_boolean = True
    default_string = "default_string"
    default_bytes = "default_bytes"
    default_int = -1
    default_long = -2
    default_float = 1.1
    default_double = 2.2
    default_fixed = "12345"
    default_union = None
    default_enum = "FOO"
    default_array = ["a", "b"]
    default_map = {"a": 1, "b": 2}
    default_record = {"sub_int": -3}
    schema = {
        "type":
        "record",
        "name":
        "test_all_default_values",
        "fields": [
            {
                "name": "boolean",
                "type": "boolean",
                "default": default_boolean
            },
            {
                "name": "string",
                "type": "string",
                "default": default_string
            },
            {
                "name": "bytes",
                "type": "bytes",
                "default": default_bytes
            },
            {
                "name": "int",
                "type": "int",
                "default": default_int
            },
            {
                "name": "long",
                "type": "long",
                "default": default_long
            },
            {
                "name": "float",
                "type": "float",
                "default": default_float
            },
            {
                "name": "double",
                "type": "double",
                "default": default_double
            },
            {
                "name": "fixed",
                "type": {
                    "type": "fixed",
                    "name": "fixed_field",
                    "size": 5
                },
                "default": default_fixed,
            },
            {
                "name":
                "union",
                "type": [
                    "null",
                    "int",
                    {
                        "type":
                        "record",
                        "name":
                        "union_record",
                        "fields": [{
                            "name": "union_record_field",
                            "type": "string"
                        }],
                    },
                ],
                "default":
                default_union,
            },
            {
                "name": "enum",
                "type": {
                    "type": "enum",
                    "name": "enum_field",
                    "symbols": ["FOO", "BAR"],
                },
                "default": default_enum,
            },
            {
                "name": "array",
                "type": {
                    "type": "array",
                    "items": "string"
                },
                "default": deepcopy(default_array),
            },
            {
                "name": "map",
                "type": {
                    "type": "map",
                    "values": "int"
                },
                "default": deepcopy(default_map),
            },
            {
                "name": "record",
                "type": {
                    "type": "record",
                    "name": "subrecord",
                    "fields": [{
                        "name": "sub_int",
                        "type": "int"
                    }],
                },
                "default": default_record,
            },
        ],
    }

    record = {}

    new_file = StringIO(json.dumps(record))
    read_record = next(json_reader(new_file, schema))

    assert read_record["boolean"] == default_boolean
    assert read_record["string"] == default_string
    assert read_record["bytes"] == default_bytes.encode("iso-8859-1")
    assert read_record["int"] == default_int
    assert read_record["long"] == default_long
    assert read_record["float"] == default_float
    assert read_record["double"] == default_double
    assert read_record["fixed"] == default_fixed.encode("iso-8859-1")
    assert read_record["union"] == default_union
    assert read_record["enum"] == default_enum
    assert read_record["array"] == default_array
    assert read_record["map"] == default_map
    assert read_record["record"] == default_record