Exemplo n.º 1
0
int avro_datum_equal(const avro_datum_t a, const avro_datum_t b)
{
	if (!(is_avro_datum(a) && is_avro_datum(b))) {
		return 0;
	}
	if (avro_typeof(a) != avro_typeof(b)) {
		return 0;
	}
	switch (avro_typeof(a)) {
	case AVRO_STRING:
		return strcmp(avro_datum_to_string(a)->s,
			      avro_datum_to_string(b)->s) == 0;
	case AVRO_BYTES:
		return (avro_datum_to_bytes(a)->size ==
			avro_datum_to_bytes(b)->size)
		    && memcmp(avro_datum_to_bytes(a)->bytes,
			      avro_datum_to_bytes(b)->bytes,
			      avro_datum_to_bytes(a)->size) == 0;
	case AVRO_INT32:
		return avro_datum_to_int32(a)->i32 ==
		    avro_datum_to_int32(b)->i32;
	case AVRO_INT64:
		return avro_datum_to_int64(a)->i64 ==
		    avro_datum_to_int64(b)->i64;
	case AVRO_FLOAT:
		return avro_datum_to_float(a)->f == avro_datum_to_float(b)->f;
	case AVRO_DOUBLE:
		return avro_datum_to_double(a)->d == avro_datum_to_double(b)->d;
	case AVRO_BOOLEAN:
		return avro_datum_to_boolean(a)->i ==
		    avro_datum_to_boolean(b)->i;
	case AVRO_NULL:
		return 1;
	case AVRO_ARRAY:
		return array_equal(avro_datum_to_array(a),
				   avro_datum_to_array(b));
	case AVRO_MAP:
		return map_equal(avro_datum_to_map(a), avro_datum_to_map(b));

	case AVRO_RECORD:
		return record_equal(avro_datum_to_record(a),
				    avro_datum_to_record(b));

	case AVRO_ENUM:
		return enum_equal(avro_datum_to_enum(a), avro_datum_to_enum(b));

	case AVRO_FIXED:
		return fixed_equal(avro_datum_to_fixed(a),
				   avro_datum_to_fixed(b));

	case AVRO_UNION:
		return union_equal(avro_datum_to_union(a),
				   avro_datum_to_union(b));

	case AVRO_LINK:
		/*
		 * TODO 
		 */
		return 0;
	}
	return 0;
}
Exemplo n.º 2
0
static int write_datum(avro_writer_t writer, const avro_encoding_t * enc,
		       avro_schema_t writers_schema, avro_datum_t datum)
{
	if (is_avro_schema(writers_schema) && is_avro_link(writers_schema)) {
		return write_datum(writer, enc,
				   (avro_schema_to_link(writers_schema))->to,
				   datum);
	}

	switch (avro_typeof(datum)) {
	case AVRO_NULL:
		return enc->write_null(writer);

	case AVRO_BOOLEAN:
		return enc->write_boolean(writer,
					  avro_datum_to_boolean(datum)->i);

	case AVRO_STRING:
		return enc->write_string(writer,
					 avro_datum_to_string(datum)->s);

	case AVRO_BYTES:
		return enc->write_bytes(writer,
					avro_datum_to_bytes(datum)->bytes,
					avro_datum_to_bytes(datum)->size);

	case AVRO_INT32:
	case AVRO_INT64:{
			int64_t val = avro_typeof(datum) == AVRO_INT32 ?
			    avro_datum_to_int32(datum)->i32 :
			    avro_datum_to_int64(datum)->i64;
			if (is_avro_schema(writers_schema)) {
				/* handle promotion */
				if (is_avro_float(writers_schema)) {
					return enc->write_float(writer,
								(float)val);
				} else if (is_avro_double(writers_schema)) {
					return enc->write_double(writer,
								 (double)val);
				}
			}
			return enc->write_long(writer, val);
		}

	case AVRO_FLOAT:{
			float val = avro_datum_to_float(datum)->f;
			if (is_avro_schema(writers_schema)
			    && is_avro_double(writers_schema)) {
				/* handle promotion */
				return enc->write_double(writer, (double)val);
			}
			return enc->write_float(writer, val);
		}

	case AVRO_DOUBLE:
		return enc->write_double(writer,
					 avro_datum_to_double(datum)->d);

	case AVRO_RECORD:
		return write_record(writer, enc,
				    avro_schema_to_record(writers_schema),
				    datum);

	case AVRO_ENUM:
		return write_enum(writer, enc,
				  avro_schema_to_enum(writers_schema),
				  avro_datum_to_enum(datum));

	case AVRO_FIXED:
		return avro_write(writer,
				  avro_datum_to_fixed(datum)->bytes,
				  avro_datum_to_fixed(datum)->size);

	case AVRO_MAP:
		return write_map(writer, enc,
				 avro_schema_to_map(writers_schema),
				 avro_datum_to_map(datum));

	case AVRO_ARRAY:
		return write_array(writer, enc,
				   avro_schema_to_array(writers_schema),
				   avro_datum_to_array(datum));

	case AVRO_UNION:
		return write_union(writer, enc,
				   avro_schema_to_union(writers_schema),
				   avro_datum_to_union(datum));

	case AVRO_LINK:
		break;
	}

	return 0;
}