2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to you under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14
* implied. See the License for the specific language governing
15
* permissions and limitations under the License.
19
#include "avro_private.h"
23
static const char *schema_json =
25
" \"type\": \"record\","
26
" \"name\": \"test\","
28
" { \"name\": \"i\", \"type\": \"int\" },"
29
" { \"name\": \"l\", \"type\": \"long\" },"
30
" { \"name\": \"s\", \"type\": \"string\" },"
32
" \"name\": \"subrec\","
34
" \"type\": \"record\","
37
" { \"name\": \"f\", \"type\": \"float\" },"
38
" { \"name\": \"d\", \"type\": \"double\" }"
46
populate_complex_record(avro_value_t *p_val)
50
avro_value_get_by_index(p_val, 0, &field, NULL);
51
avro_value_set_int(&field, 42);
53
avro_value_get_by_index(p_val, 1, &field, NULL);
54
avro_value_set_long(&field, 4242);
56
avro_wrapped_buffer_t wbuf;
57
avro_wrapped_buffer_new_string(&wbuf, "Follow your bliss.");
58
avro_value_get_by_index(p_val, 2, &field, NULL);
59
avro_value_give_string_len(&field, &wbuf);
62
avro_value_get_by_index(p_val, 3, &subrec, NULL);
64
avro_value_get_by_index(&subrec, 0, &field, NULL);
65
avro_value_set_float(&field, 3.14159265);
67
avro_value_get_by_index(&subrec, 1, &field, NULL);
68
avro_value_set_double(&field, 2.71828183);
75
static char buf[4096];
77
avro_file_writer_t file_writer;
78
avro_file_reader_t file_reader;
79
const char *outpath = "test-1379.avro";
81
avro_schema_t schema = NULL;
82
avro_schema_error_t error = NULL;
83
check(rval, avro_schema_from_json(schema_json, strlen(schema_json), &schema, &error));
85
avro_value_iface_t *iface = avro_generic_class_from_schema(schema);
88
avro_generic_value_new(iface, &val);
91
avro_generic_value_new(iface, &out);
94
avro_value_reset(&val);
95
populate_complex_record(&val);
97
/* create the writers */
98
writer = avro_writer_memory(buf, sizeof(buf));
99
check(rval, avro_file_writer_create(outpath, schema, &file_writer));
101
fprintf(stderr, "Writing to buffer\n");
102
check(rval, avro_value_write(writer, &val));
104
fprintf(stderr, "Writing buffer to %s "
105
"using avro_file_writer_append_encoded()\n", outpath);
106
len = avro_writer_tell(writer);
107
check(rval, avro_file_writer_append_encoded(file_writer, buf, len));
108
check(rval, avro_file_writer_close(file_writer));
110
check(rval, avro_file_reader(outpath, &file_reader));
111
fprintf(stderr, "Re-reading value to verify\n");
112
check(rval, avro_file_reader_read_value(file_reader, &out));
113
fprintf(stderr, "Verifying value...");
114
if (!avro_value_equal(&val, &out)) {
115
fprintf(stderr, "fail!\n");
118
fprintf(stderr, "ok\n");
119
check(rval, avro_file_reader_close(file_reader));