Ejemplo n.º 1
0
 def writedata(self, schm, datum, encoder):
     if schm.gettype() == schema.NULL:
         if datum is None:
             return
         raise io.AvroTypeException(schm, datum)
     fn = self.__writefn.get(schm.gettype())
     if fn is not None:
         fn(schm, datum, encoder)
     else:
         raise io.AvroTypeException(schm, datum)
Ejemplo n.º 2
0
 def resolveunion(self, schm, datum):
     index = 0
     for elemtype in schm.getelementtypes():
         if validate(elemtype, datum):
             return index
         index += 1
     raise io.AvroTypeException(schm, datum)
Ejemplo n.º 3
0
    def _union_to_json(self, data_obj, writers_schema):
        index_of_schema = -1
        data_schema = self._get_record_schema_if_available(data_obj)
        for i, candidate_schema in enumerate(writers_schema.schemas):
            # Check for exact matches first.
            if data_schema and candidate_schema.namespace == data_schema.namespace \
                and candidate_schema.name == data_schema.name:
                index_of_schema = i
                break

            # Fallback to schema guessing based on validation.
            if self.validate(candidate_schema, data_obj):
                index_of_schema = i
                if candidate_schema.type == 'boolean':
                    break
        if index_of_schema < 0:
            raise io.AvroTypeException(writers_schema, data_obj)
        candidate_schema = writers_schema.schemas[index_of_schema]
        if candidate_schema.type == 'null':
            return None
        if self.fastavro:
            # Fastavro likes tuples instead of dicts for union types.
            return (self._fullname(candidate_schema),
                    self._generic_to_json(data_obj, candidate_schema))
        return {
            self._fullname(candidate_schema):
            self._generic_to_json(data_obj, candidate_schema)
        }
Ejemplo n.º 4
0
 def writearray(self, schm, datum, encoder):
     if not isinstance(datum, list):
         raise io.AvroTypeException(schm, datum)
     if len(datum) > 0:
         encoder.writelong(len(datum))
         for item in datum:
             self.writedata(schm.getelementtype(), item, encoder)
     encoder.writelong(0)
Ejemplo n.º 5
0
 def writemap(self, schm, datum, encoder):
     if not isinstance(datum, dict):
         raise io.AvroTypeException(schm, datum)
     if len(datum) > 0:
         encoder.writelong(len(datum))
         for k, v in datum.items():
             encoder.writeutf8(k)
             self.writedata(schm.getvaluetype(), v, encoder)
     encoder.writelong(0)
Ejemplo n.º 6
0
    def to_json_object(self, data_obj, writers_schema=None):
        if writers_schema is None:
            writers_schema = self._get_record_schema_if_available(data_obj)

        if writers_schema is None:
            raise Exception(
                "Could not determine writer's schema from the object type and schema was not passed"
            )
        assert isinstance(writers_schema, schema.Schema)

        if not self.validate(writers_schema, data_obj):
            raise io.AvroTypeException(writers_schema, data_obj)

        return self._generic_to_json(data_obj, writers_schema)
Ejemplo n.º 7
0
    def to_json_object(self, data_obj, writers_schema=None):
        if hasattr(type(data_obj), 'RECORD_SCHEMA'):
            writers_schema = type(data_obj).RECORD_SCHEMA

        if writers_schema is None:
            raise Exception(
                "Could not determine writer's schema from the object type and schema was not passed"
            )
        assert isinstance(writers_schema, schema.Schema)

        if not self.validate(writers_schema, data_obj):
            raise io.AvroTypeException(writers_schema, data_obj)

        return self._generic_to_json(data_obj, writers_schema)
Ejemplo n.º 8
0
 def _union_to_json(self, data_obj, writers_schema):
     index_of_schema = -1
     for i, candidate_schema in enumerate(writers_schema.schemas):
         if self.validate(candidate_schema, data_obj):
             index_of_schema = i
             if candidate_schema.type == 'boolean':
                 break
     if index_of_schema < 0:
         raise io.AvroTypeException(writers_schema, data_obj)
     candidate_schema = writers_schema.schemas[index_of_schema]
     if candidate_schema.type == 'null':
         return None
     return {
         self._fullname(candidate_schema):
         self._generic_to_json(data_obj, candidate_schema)
     }
Ejemplo n.º 9
0
 def writerecord(self, schm, datum, encoder):
     if not isinstance(datum, dict):
         raise io.AvroTypeException(schm, datum)
     for field in schm.getfields().values():
         self.writedata(field.getschema(), datum.get(field.getname()),
                        encoder)
Ejemplo n.º 10
0
    def write(self, datum, encoder):
        # validate datum
        if not self.__validate(self.writers_schema, datum):
            raise io.AvroTypeException(self.writers_schema, datum)

        self.write_data(self.writers_schema, datum, encoder)
Ejemplo n.º 11
0
 def union_schema(self, union, datum):
     for index, cls in enumerate(
             types.from_schema(s) for s in union.schemas):
         if isinstance(datum, cls):
             return index, union.schemas[index]
     raise io.AvroTypeException(union, datum)