Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)}
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)
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)
writer = datafile.DataFileWriter(f, io.DatumWriter(), s, codec = 'deflate')
failed = 0
for line in sys.stdin:
line = line.strip()
try:
data = json.loads(line)
except ValueError as detail:
continue
try:
writer.append(data)
except io.AvroTypeException as detail:
print line
failed += 1
writer.close()
print str(failed) + " failed in schema"
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)
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)
{"": value}
Then used one that matches to serialize `datum`
:param schema: Avro schema for this union
:param datum: Data to serialize
:return: dict {"type": value} or "null"
"""
for candidate_schema in schema.schemas:
if validate(candidate_schema, datum):
if candidate_schema.type == "null":
return self._process_null()
else:
field_type_name = self._union_name(candidate_schema)
return {
field_type_name: self._process_data(candidate_schema, datum)
}
raise AvroTypeException(schema, datum)
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)