static int avro_datum_value_get_size(const avro_value_iface_t *iface, const void *vself, size_t *size) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); if (is_avro_array(self)) { *size = avro_array_size(self); return 0; } if (is_avro_map(self)) { *size = avro_map_size(self); return 0; } if (is_avro_record(self)) { avro_schema_t schema = avro_datum_get_schema(self); *size = avro_schema_record_size(schema); return 0; } avro_set_error("Can only get size of array, map, or record, %d", avro_typeof(self)); return EINVAL; }
static int avro_datum_value_add(const avro_value_iface_t *iface, void *vself, const char *key, avro_value_t *child, size_t *index, int *is_new) { AVRO_UNUSED(iface); avro_datum_t self = (avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); if (!is_avro_map(self)) { avro_set_error("Can only add to map"); return EINVAL; } int rval; avro_datum_t child_datum; if (avro_map_get(self, key, &child_datum) == 0) { /* key already exists */ if (is_new != NULL) { *is_new = 0; } if (index != NULL) { int real_index; avro_map_get_index(self, key, &real_index); *index = real_index; } return avro_datum_as_child_value(child, child_datum); } /* key is new */ avro_schema_t map_schema = avro_datum_get_schema(self); avro_schema_t child_schema = avro_schema_map_values(map_schema); child_datum = avro_datum_from_schema(child_schema); if (child_datum == NULL) { return ENOMEM; } rval = avro_map_set(self, key, child_datum); avro_datum_decref(child_datum); if (rval != 0) { return rval; } if (is_new != NULL) { *is_new = 1; } if (index != NULL) { *index = avro_map_size(self) - 1; } return avro_datum_as_child_value(child, child_datum); }
static int avro_datum_value_get_by_index(const avro_value_iface_t *iface, const void *vself, size_t index, avro_value_t *child, const char **name) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); int rval; avro_datum_t child_datum; if (is_avro_array(self)) { check(rval, avro_array_get(self, index, &child_datum)); return avro_datum_as_child_value(child, child_datum); } if (is_avro_map(self)) { const char *real_key; check(rval, avro_map_get_key(self, index, &real_key)); if (name != NULL) { *name = real_key; } check(rval, avro_map_get(self, real_key, &child_datum)); return avro_datum_as_child_value(child, child_datum); } if (is_avro_record(self)) { avro_schema_t schema = avro_datum_get_schema(self); const char *field_name = avro_schema_record_field_name(schema, index); if (field_name == NULL) { return EINVAL; } if (name != NULL) { *name = field_name; } check(rval, avro_record_get(self, field_name, &child_datum)); return avro_datum_as_child_value(child, child_datum); } avro_set_error("Can only get by index from array, map, or record"); return EINVAL; }
int avro_schema_equal(avro_schema_t a, avro_schema_t b) { if (!a || !b) { /* * this is an error. protecting from segfault. */ return 0; } else if (a == b) { /* * an object is equal to itself */ return 1; } else if (avro_typeof(a) != avro_typeof(b)) { return 0; } else if (is_avro_record(a)) { return schema_record_equal(avro_schema_to_record(a), avro_schema_to_record(b)); } else if (is_avro_enum(a)) { return schema_enum_equal(avro_schema_to_enum(a), avro_schema_to_enum(b)); } else if (is_avro_fixed(a)) { return schema_fixed_equal(avro_schema_to_fixed(a), avro_schema_to_fixed(b)); } else if (is_avro_map(a)) { return schema_map_equal(avro_schema_to_map(a), avro_schema_to_map(b)); } else if (is_avro_array(a)) { return schema_array_equal(avro_schema_to_array(a), avro_schema_to_array(b)); } else if (is_avro_union(a)) { return schema_union_equal(avro_schema_to_union(a), avro_schema_to_union(b)); } else if (is_avro_link(a)) { return schema_link_equal(avro_schema_to_link(a), avro_schema_to_link(b)); } return 1; }
const char *avro_schema_type_name(const avro_schema_t schema) { if (is_avro_record(schema)) { return (avro_schema_to_record(schema))->name; } else if (is_avro_enum(schema)) { return (avro_schema_to_enum(schema))->name; } else if (is_avro_fixed(schema)) { return (avro_schema_to_fixed(schema))->name; } else if (is_avro_union(schema)) { return "union"; } else if (is_avro_array(schema)) { return "array"; } else if (is_avro_map(schema)) { return "map"; } else if (is_avro_int32(schema)) { return "int"; } else if (is_avro_int64(schema)) { return "long"; } else if (is_avro_float(schema)) { return "float"; } else if (is_avro_double(schema)) { return "double"; } else if (is_avro_boolean(schema)) { return "boolean"; } else if (is_avro_null(schema)) { return "null"; } else if (is_avro_string(schema)) { return "string"; } else if (is_avro_bytes(schema)) { return "bytes"; } else if (is_avro_link(schema)) { avro_schema_t target = avro_schema_link_target(schema); return avro_schema_type_name(target); } avro_set_error("Unknown schema type"); return NULL; }
static int avro_datum_value_get_by_name(const avro_value_iface_t *iface, const void *vself, const char *name, avro_value_t *child, size_t *index) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); int rval; avro_datum_t child_datum; if (is_avro_map(self)) { if (index != NULL) { int real_index; check(rval, avro_map_get_index(self, name, &real_index)); *index = real_index; } check(rval, avro_map_get(self, name, &child_datum)); return avro_datum_as_child_value(child, child_datum); } if (is_avro_record(self)) { if (index != NULL) { avro_schema_t schema = avro_datum_get_schema(self); *index = avro_schema_record_field_get_index(schema, name); } check(rval, avro_record_get(self, name, &child_datum)); return avro_datum_as_child_value(child, child_datum); } avro_set_error("Can only get by name from map or record"); return EINVAL; }
avro_schema_t avro_schema_get_subschema(const avro_schema_t schema, const char *name) { if (is_avro_record(schema)) { const struct avro_record_schema_t *rschema = avro_schema_to_record(schema); union { st_data_t data; struct avro_record_field_t *field; } field; if (st_lookup(rschema->fields_byname, (st_data_t) name, &field.data)) { return field.field->type; } avro_set_error("No record field named %s", name); return NULL; } else if (is_avro_union(schema)) { const struct avro_union_schema_t *uschema = avro_schema_to_union(schema); long i; for (i = 0; i < uschema->branches->num_entries; i++) { union { st_data_t data; avro_schema_t schema; } val; st_lookup(uschema->branches, i, &val.data); if (strcmp(avro_schema_type_name(val.schema), name) == 0) { return val.schema; } } avro_set_error("No union branch named %s", name); return NULL; } else if (is_avro_array(schema)) { if (strcmp(name, "[]") == 0) { const struct avro_array_schema_t *aschema = avro_schema_to_array(schema); return aschema->items; } avro_set_error("Array subschema must be called \"[]\""); return NULL; } else if (is_avro_map(schema)) { if (strcmp(name, "{}") == 0) { const struct avro_map_schema_t *mschema = avro_schema_to_map(schema); return mschema->values; } avro_set_error("Map subschema must be called \"{}\""); return NULL; } avro_set_error("Can only retrieve subschemas from record, union, array, or map"); return NULL; }