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.
22
#define CLOSE_EXTERN }
27
#include <avro/platform.h>
30
#include <avro/basics.h>
31
#include <avro/data.h>
32
#include <avro/schema.h>
33
#include <avro/value.h>
36
* This file defines the deprecated interface for handling Avro values.
37
* It's here solely for backwards compatibility. New code should use
38
* the avro_value_t interface (defined in avro/value.h). The
39
* avro_datum_t type has been replaced by the “generic” implementation
40
* of the value interface, which is defined in avro/generic.h. You can
41
* also use your own application-specific types as Avro values by
42
* defining your own avro_value_t implementation for them.
46
* A function used to free a bytes, string, or fixed buffer once it is
47
* no longer needed by the datum that wraps it.
51
(*avro_free_func_t)(void *ptr, size_t sz);
54
* An avro_free_func_t that frees the buffer using the custom allocator
55
* provided to avro_set_allocator.
59
avro_alloc_free_func(void *ptr, size_t sz);
62
* Datum constructors. Each datum stores a reference to the schema that
63
* the datum is an instance of. The primitive datum constructors don't
64
* need to take in an explicit avro_schema_t parameter, since there's
65
* only one schema that they could be an instance of. The complex
66
* constructors do need an explicit schema parameter.
69
typedef struct avro_obj_t *avro_datum_t;
70
avro_datum_t avro_string(const char *str);
71
avro_datum_t avro_givestring(const char *str,
72
avro_free_func_t free);
73
avro_datum_t avro_bytes(const char *buf, int64_t len);
74
avro_datum_t avro_givebytes(const char *buf, int64_t len,
75
avro_free_func_t free);
76
avro_datum_t avro_int32(int32_t i);
77
avro_datum_t avro_int64(int64_t l);
78
avro_datum_t avro_float(float f);
79
avro_datum_t avro_double(double d);
80
avro_datum_t avro_boolean(int8_t i);
81
avro_datum_t avro_null(void);
82
avro_datum_t avro_record(avro_schema_t schema);
83
avro_datum_t avro_enum(avro_schema_t schema, int i);
84
avro_datum_t avro_fixed(avro_schema_t schema,
85
const char *bytes, const int64_t size);
86
avro_datum_t avro_givefixed(avro_schema_t schema,
87
const char *bytes, const int64_t size,
88
avro_free_func_t free);
89
avro_datum_t avro_map(avro_schema_t schema);
90
avro_datum_t avro_array(avro_schema_t schema);
91
avro_datum_t avro_union(avro_schema_t schema,
92
int64_t discriminant, const avro_datum_t datum);
95
* Returns the schema that the datum is an instance of.
98
avro_schema_t avro_datum_get_schema(const avro_datum_t datum);
101
* Constructs a new avro_datum_t instance that's appropriate for holding
102
* values of the given schema.
105
avro_datum_t avro_datum_from_schema(const avro_schema_t schema);
108
int avro_string_get(avro_datum_t datum, char **p);
109
int avro_bytes_get(avro_datum_t datum, char **bytes, int64_t * size);
110
int avro_int32_get(avro_datum_t datum, int32_t * i);
111
int avro_int64_get(avro_datum_t datum, int64_t * l);
112
int avro_float_get(avro_datum_t datum, float *f);
113
int avro_double_get(avro_datum_t datum, double *d);
114
int avro_boolean_get(avro_datum_t datum, int8_t * i);
116
int avro_enum_get(const avro_datum_t datum);
117
const char *avro_enum_get_name(const avro_datum_t datum);
118
int avro_fixed_get(avro_datum_t datum, char **bytes, int64_t * size);
119
int avro_record_get(const avro_datum_t record, const char *field_name,
120
avro_datum_t * value);
123
* A helper macro that extracts the value of the given field of a
127
#define avro_record_get_field_value(rc, rec, typ, fname, ...) \
129
avro_datum_t field = NULL; \
130
(rc) = avro_record_get((rec), (fname), &field); \
132
(rc) = avro_##typ##_get(field, __VA_ARGS__); \
136
int avro_map_get(const avro_datum_t datum, const char *key,
137
avro_datum_t * value);
139
* For maps, the "index" for each entry is based on the order that they
140
* were added to the map.
142
int avro_map_get_key(const avro_datum_t datum, int index,
144
int avro_map_get_index(const avro_datum_t datum, const char *key,
146
size_t avro_map_size(const avro_datum_t datum);
147
int avro_array_get(const avro_datum_t datum, int64_t index, avro_datum_t * value);
148
size_t avro_array_size(const avro_datum_t datum);
151
* These accessors allow you to query the current branch of a union
152
* value, returning either the branch's discriminant value or the
153
* avro_datum_t of the branch. A union value can be uninitialized, in
154
* which case the discriminant will be -1 and the datum NULL.
157
int64_t avro_union_discriminant(const avro_datum_t datum);
158
avro_datum_t avro_union_current_branch(avro_datum_t datum);
161
int avro_string_set(avro_datum_t datum, const char *p);
162
int avro_givestring_set(avro_datum_t datum, const char *p,
163
avro_free_func_t free);
165
int avro_bytes_set(avro_datum_t datum, const char *bytes, const int64_t size);
166
int avro_givebytes_set(avro_datum_t datum, const char *bytes,
168
avro_free_func_t free);
170
int avro_int32_set(avro_datum_t datum, const int32_t i);
171
int avro_int64_set(avro_datum_t datum, const int64_t l);
172
int avro_float_set(avro_datum_t datum, const float f);
173
int avro_double_set(avro_datum_t datum, const double d);
174
int avro_boolean_set(avro_datum_t datum, const int8_t i);
176
int avro_enum_set(avro_datum_t datum, const int symbol_value);
177
int avro_enum_set_name(avro_datum_t datum, const char *symbol_name);
178
int avro_fixed_set(avro_datum_t datum, const char *bytes, const int64_t size);
179
int avro_givefixed_set(avro_datum_t datum, const char *bytes,
181
avro_free_func_t free);
183
int avro_record_set(avro_datum_t record, const char *field_name,
187
* A helper macro that sets the value of the given field of a record.
190
#define avro_record_set_field_value(rc, rec, typ, fname, ...) \
192
avro_datum_t field = NULL; \
193
(rc) = avro_record_get((rec), (fname), &field); \
195
(rc) = avro_##typ##_set(field, __VA_ARGS__); \
198
int avro_map_set(avro_datum_t map, const char *key,
200
int avro_array_append_datum(avro_datum_t array_datum,
204
* This function selects the active branch of a union value, and can be
205
* safely called on an existing union to change the current branch. If
206
* the branch changes, we'll automatically construct a new avro_datum_t
207
* for the new branch's schema type. If the desired branch is already
208
* the active branch of the union, we'll leave the existing datum
209
* instance as-is. The branch datum will be placed into the "branch"
210
* parameter, regardless of whether we have to create a new datum
214
int avro_union_set_discriminant(avro_datum_t unionp,
216
avro_datum_t *branch);
219
* Resets a datum instance. For arrays and maps, this frees all
220
* elements and clears the container. For records and unions, this
221
* recursively resets any child datum instances.
225
avro_datum_reset(avro_datum_t value);
227
/* reference counting */
228
avro_datum_t avro_datum_incref(avro_datum_t value);
229
void avro_datum_decref(avro_datum_t value);
231
void avro_datum_print(avro_datum_t value, FILE * fp);
233
int avro_datum_equal(avro_datum_t a, avro_datum_t b);
236
* Returns a string containing the JSON encoding of an Avro value. You
237
* must free this string when you're done with it, using the standard
238
* free() function. (*Not* using the custom Avro allocator.)
241
int avro_datum_to_json(const avro_datum_t datum,
242
int one_line, char **json_str);
245
int avro_schema_datum_validate(avro_schema_t
246
expected_schema, avro_datum_t datum);
249
* An avro_value_t implementation for avro_datum_t objects.
253
avro_datum_class(void);
256
* Creates a new avro_value_t instance for the given datum.
260
avro_datum_as_value(avro_value_t *value, avro_datum_t src);