Exemplo n.º 1
0
static int test_map(void)
{
    avro_schema_t schema = avro_schema_map(avro_schema_long());
    avro_datum_t datum = avro_map(schema);
    int64_t i = 0;
    char *nums[] =
    { "zero", "one", "two", "three", "four", "five", "six", NULL };
    while (nums[i]) {
        avro_datum_t i_datum = avro_int64(i);
        avro_map_set(datum, nums[i], i_datum);
        avro_datum_decref(i_datum);
        i++;
    }

    if (avro_array_size(datum) != 7) {
        fprintf(stderr, "Unexpected map size\n");
        exit(EXIT_FAILURE);
    }

    avro_datum_t value;
    const char  *key;
    avro_map_get_key(datum, 2, &key);
    avro_map_get(datum, key, &value);
    int64_t  val;
    avro_int64_get(value, &val);

    if (val != 2) {
        fprintf(stderr, "Unexpected map value 2\n");
        exit(EXIT_FAILURE);
    }

    int  index;
    if (avro_map_get_index(datum, "two", &index)) {
        fprintf(stderr, "Can't get index for key \"two\": %s\n",
                avro_strerror());
        exit(EXIT_FAILURE);
    }
    if (index != 2) {
        fprintf(stderr, "Unexpected index for key \"two\"\n");
        exit(EXIT_FAILURE);
    }
    if (!avro_map_get_index(datum, "foobar", &index)) {
        fprintf(stderr, "Unexpected index for key \"foobar\"\n");
        exit(EXIT_FAILURE);
    }

    write_read_check(schema, datum, NULL, NULL, "map");
    test_json(datum,
              "{\"zero\": 0, \"one\": 1, \"two\": 2, \"three\": 3, "
              "\"four\": 4, \"five\": 5, \"six\": 6}");
    avro_datum_decref(datum);
    avro_schema_decref(schema);
    return 0;
}
Exemplo n.º 2
0
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);
}
Exemplo n.º 3
0
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;
}