summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/avro/src/datum_value.c
blob: a4fa55a0c9e29afe7c9d1b72e1df2e5ff978461a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */

#include <avro/platform.h>
#include <stdlib.h>
#include <string.h>

#include "avro/allocation.h"
#include "avro/basics.h"
#include "avro/errors.h"
#include "avro/legacy.h"
#include "avro/refcount.h"
#include "avro/schema.h"
#include "avro/value.h"
#include "avro_private.h"

extern avro_value_iface_t  AVRO_DATUM_VALUE_CLASS;

avro_value_iface_t *
avro_datum_class(void)
{
	return &AVRO_DATUM_VALUE_CLASS;
}

int
avro_datum_as_value(avro_value_t *value, avro_datum_t src)
{
	value->iface = &AVRO_DATUM_VALUE_CLASS;
	value->self = avro_datum_incref(src);
	return 0;
}

static int
avro_datum_as_child_value(avro_value_t *value, avro_datum_t src)
{
	value->iface = &AVRO_DATUM_VALUE_CLASS;
	value->self = src;
	return 0;
}

static void
avro_datum_value_incref(avro_value_t *value)
{
	avro_datum_t  self = (avro_datum_t) value->self;
	avro_datum_incref(self);
}

static void
avro_datum_value_decref(avro_value_t *value)
{
	avro_datum_t  self = (avro_datum_t) value->self;
	avro_datum_decref(self);
}

static int
avro_datum_value_reset(const avro_value_iface_t *iface, void *vself)
{
	AVRO_UNUSED(iface);
	avro_datum_t  self = (avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");
	return avro_datum_reset(self);
}

static avro_type_t
avro_datum_value_get_type(const avro_value_iface_t *iface, const void *vself)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
#ifdef _WIN32
#pragma message("#warning: Bug: EINVAL is not of type avro_type_t.")
#else
#warning "Bug: EINVAL is not of type avro_type_t."
#endif
        /* We shouldn't use EINVAL as the return value to
         * check_param(), because EINVAL (= 22) is not a valid enum
         * avro_type_t. This is a structural issue -- we would need a
         * different interface on all the get_type functions to fix
         * this. For now, suppressing the error by casting EINVAL to
         * (avro_type_t) so the code compiles under C++.
         */
	check_param((avro_type_t) EINVAL, self, "datum instance");
	return avro_typeof(self);
}

static avro_schema_t
avro_datum_value_get_schema(const avro_value_iface_t *iface, const void *vself)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(NULL, self, "datum instance");
	return avro_datum_get_schema(self);
}


static int
avro_datum_value_get_boolean(const avro_value_iface_t *iface,
			     const void *vself, int *out)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	int  rval;
	int8_t  value;
	check(rval, avro_boolean_get(self, &value));
	*out = value;
	return 0;
}

static int
avro_datum_value_get_bytes(const avro_value_iface_t *iface,
			   const void *vself, const void **buf, size_t *size)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	int  rval;
	char  *bytes;
	int64_t  sz;
	check(rval, avro_bytes_get(self, &bytes, &sz));
	if (buf != NULL) {
		*buf = (const void *) bytes;
	}
	if (size != NULL) {
		*size = sz;
	}
	return 0;
}

static int
avro_datum_value_grab_bytes(const avro_value_iface_t *iface,
			    const void *vself, avro_wrapped_buffer_t *dest)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	int  rval;
	char  *bytes;
	int64_t  sz;
	check(rval, avro_bytes_get(self, &bytes, &sz));

	/* nothing clever, just make a copy */
	return avro_wrapped_buffer_new_copy(dest, bytes, sz);
}

static int
avro_datum_value_get_double(const avro_value_iface_t *iface,
			    const void *vself, double *out)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	int  rval;
	double  value;
	check(rval, avro_double_get(self, &value));
	*out = value;
	return 0;
}

static int
avro_datum_value_get_float(const avro_value_iface_t *iface,
			   const void *vself, float *out)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	int  rval;
	float  value;
	check(rval, avro_float_get(self, &value));
	*out = value;
	return 0;
}

static int
avro_datum_value_get_int(const avro_value_iface_t *iface,
			 const void *vself, int32_t *out)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	int  rval;
	int32_t  value;
	check(rval, avro_int32_get(self, &value));
	*out = value;
	return 0;
}

static int
avro_datum_value_get_long(const avro_value_iface_t *iface,
			  const void *vself, int64_t *out)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	int  rval;
	int64_t  value;
	check(rval, avro_int64_get(self, &value));
	*out = value;
	return 0;
}

static int
avro_datum_value_get_null(const avro_value_iface_t *iface,
			  const void *vself)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, is_avro_null(self), "datum instance");
	return 0;
}

static int
avro_datum_value_get_string(const avro_value_iface_t *iface,
			    const void *vself, const char **str, size_t *size)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	int  rval;
	char  *value;
	check(rval, avro_string_get(self, &value));
	if (str != NULL) {
		*str = (const char *) value;
	}
	if (size != NULL) {
		*size = strlen(value)+1;
	}
	return 0;
}

static int
avro_datum_value_grab_string(const avro_value_iface_t *iface,
			     const void *vself, avro_wrapped_buffer_t *dest)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	int  rval;
	char  *str;
	size_t  sz;
	check(rval, avro_string_get(self, &str));
	sz = strlen(str);

	/* nothing clever, just make a copy */
	/* sz doesn't contain NUL terminator */
	return avro_wrapped_buffer_new_copy(dest, str, sz+1);
}

static int
avro_datum_value_get_enum(const avro_value_iface_t *iface,
			  const void *vself, int *out)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, is_avro_enum(self), "datum instance");
	*out = avro_enum_get(self);
	return 0;
}

static int
avro_datum_value_get_fixed(const avro_value_iface_t *iface,
			   const void *vself, const void **buf, size_t *size)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	int  rval;
	char  *bytes;
	int64_t  sz;
	check(rval, avro_fixed_get(self, &bytes, &sz));
	if (buf != NULL) {
		*buf = (const void *) bytes;
	}
	if (size != NULL) {
		*size = sz;
	}
	return 0;
}

static int
avro_datum_value_grab_fixed(const avro_value_iface_t *iface,
			    const void *vself, avro_wrapped_buffer_t *dest)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	int  rval;
	char  *bytes;
	int64_t  sz;
	check(rval, avro_fixed_get(self, &bytes, &sz));

	/* nothing clever, just make a copy */
	return avro_wrapped_buffer_new_copy(dest, bytes, sz);
}


static int
avro_datum_value_set_boolean(const avro_value_iface_t *iface,
			     void *vself, int val)
{
	AVRO_UNUSED(iface);
	avro_datum_t  self = (avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");
	return avro_boolean_set(self, val);
}

static int
avro_datum_value_set_bytes(const avro_value_iface_t *iface,
			   void *vself, void *buf, size_t size)
{
	AVRO_UNUSED(iface);
	avro_datum_t  self = (avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");
	return avro_bytes_set(self, (const char *) buf, size);
}

static int
avro_datum_value_give_bytes(const avro_value_iface_t *iface,
			    void *vself, avro_wrapped_buffer_t *buf)
{
	/*
	 * We actually can't use avro_givebytes_set, since it can't
	 * handle the extra free_ud parameter.  Ah well, this is
	 * deprecated, so go ahead and make a copy.
	 */

	int rval = avro_datum_value_set_bytes
	    (iface, vself, (void *) buf->buf, buf->size);
	avro_wrapped_buffer_free(buf);
	return rval;
}

static int
avro_datum_value_set_double(const avro_value_iface_t *iface,
			    void *vself, double val)
{
	AVRO_UNUSED(iface);
	avro_datum_t  self = (avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");
	return avro_double_set(self, val);
}

static int
avro_datum_value_set_float(const avro_value_iface_t *iface,
			   void *vself, float val)
{
	AVRO_UNUSED(iface);
	avro_datum_t  self = (avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");
	return avro_float_set(self, val);
}

static int
avro_datum_value_set_int(const avro_value_iface_t *iface,
			 void *vself, int32_t val)
{
	AVRO_UNUSED(iface);
	avro_datum_t  self = (avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");
	return avro_int32_set(self, val);
}

static int
avro_datum_value_set_long(const avro_value_iface_t *iface,
			  void *vself, int64_t val)
{
	AVRO_UNUSED(iface);
	avro_datum_t  self = (avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");
	return avro_int64_set(self, val);
}

static int
avro_datum_value_set_null(const avro_value_iface_t *iface, void *vself)
{
	AVRO_UNUSED(iface);
	avro_datum_t  self = (avro_datum_t) vself;
	check_param(EINVAL, is_avro_null(self), "datum instance");
	return 0;
}

static int
avro_datum_value_set_string(const avro_value_iface_t *iface,
			    void *vself, const char *str)
{
	AVRO_UNUSED(iface);
	avro_datum_t  self = (avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");
	return avro_string_set(self, str);
}

static int
avro_datum_value_set_string_len(const avro_value_iface_t *iface,
				void *vself, const char *str, size_t size)
{
	AVRO_UNUSED(iface);
	AVRO_UNUSED(size);
	avro_datum_t  self = (avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");
	return avro_string_set(self, str);
}

static int
avro_datum_value_give_string_len(const avro_value_iface_t *iface,
				 void *vself, avro_wrapped_buffer_t *buf)
{
	/*
	 * We actually can't use avro_givestring_set, since it can't
	 * handle the extra free_ud parameter.  Ah well, this is
	 * deprecated, so go ahead and make a copy.
	 */

	int rval = avro_datum_value_set_string_len
	    (iface, vself, (char *) buf->buf, buf->size-1);
	avro_wrapped_buffer_free(buf);
	return rval;
}

static int
avro_datum_value_set_enum(const avro_value_iface_t *iface,
			  void *vself, int val)
{
	AVRO_UNUSED(iface);
	avro_datum_t  self = (avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");
	return avro_enum_set(self, val);
}

static int
avro_datum_value_set_fixed(const avro_value_iface_t *iface,
			   void *vself, void *buf, size_t size)
{
	AVRO_UNUSED(iface);
	avro_datum_t  self = (avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");
	return avro_fixed_set(self, (const char *) buf, size);
}

static int
avro_datum_value_give_fixed(const avro_value_iface_t *iface,
			    void *vself, avro_wrapped_buffer_t *buf)
{
	/*
	 * We actually can't use avro_givefixed_set, since it can't
	 * handle the extra free_ud parameter.  Ah well, this is
	 * deprecated, so go ahead and make a copy.
	 */

	int rval = avro_datum_value_set_fixed
	    (iface, vself, (void *) buf->buf, buf->size);
	avro_wrapped_buffer_free(buf);
	return rval;
}


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_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;
}

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;
}

static int
avro_datum_value_get_discriminant(const avro_value_iface_t *iface,
				  const void *vself, int *out)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	if (!is_avro_union(self)) {
		avro_set_error("Can only get discriminant of union");
		return EINVAL;
	}

	*out = avro_union_discriminant(self);
	return 0;
}

static int
avro_datum_value_get_current_branch(const avro_value_iface_t *iface,
				    const void *vself, avro_value_t *branch)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	if (!is_avro_union(self)) {
		avro_set_error("Can only get current branch of union");
		return EINVAL;
	}

	avro_datum_t  child_datum = avro_union_current_branch(self);
	return avro_datum_as_child_value(branch, child_datum);
}


static int
avro_datum_value_append(const avro_value_iface_t *iface,
			void *vself, avro_value_t *child_out, size_t *new_index)
{
	AVRO_UNUSED(iface);
	avro_datum_t  self = (avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	if (!is_avro_array(self)) {
		avro_set_error("Can only append to array");
		return EINVAL;
	}

	int  rval;

	avro_schema_t  array_schema = avro_datum_get_schema(self);
	avro_schema_t  child_schema = avro_schema_array_items(array_schema);
	avro_datum_t  child_datum = avro_datum_from_schema(child_schema);
	if (child_datum == NULL) {
		return ENOMEM;
	}

	rval = avro_array_append_datum(self, child_datum);
	avro_datum_decref(child_datum);
	if (rval != 0) {
		return rval;
	}

	if (new_index != NULL) {
		*new_index = avro_array_size(self) - 1;
	}
	return avro_datum_as_child_value(child_out, child_datum);
}

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_set_branch(const avro_value_iface_t *iface,
			    void *vself, int discriminant,
			    avro_value_t *branch)
{
	AVRO_UNUSED(iface);
	const avro_datum_t  self = (const avro_datum_t) vself;
	check_param(EINVAL, self, "datum instance");

	if (!is_avro_union(self)) {
		avro_set_error("Can only set branch of union");
		return EINVAL;
	}

	int  rval;
	avro_datum_t  child_datum;
	check(rval, avro_union_set_discriminant(self, discriminant, &child_datum));
	return avro_datum_as_child_value(branch, child_datum);
}


avro_value_iface_t  AVRO_DATUM_VALUE_CLASS =
{
	/* "class" methods */
	NULL, /* incref */
	NULL, /* decref */
	/* general "instance" methods */
	avro_datum_value_incref,
	avro_datum_value_decref,
	avro_datum_value_reset,
	avro_datum_value_get_type,
	avro_datum_value_get_schema,
	/* primitive getters */
	avro_datum_value_get_boolean,
	avro_datum_value_get_bytes,
	avro_datum_value_grab_bytes,
	avro_datum_value_get_double,
	avro_datum_value_get_float,
	avro_datum_value_get_int,
	avro_datum_value_get_long,
	avro_datum_value_get_null,
	avro_datum_value_get_string,
	avro_datum_value_grab_string,
	avro_datum_value_get_enum,
	avro_datum_value_get_fixed,
	avro_datum_value_grab_fixed,
	/* primitive setters */
	avro_datum_value_set_boolean,
	avro_datum_value_set_bytes,
	avro_datum_value_give_bytes,
	avro_datum_value_set_double,
	avro_datum_value_set_float,
	avro_datum_value_set_int,
	avro_datum_value_set_long,
	avro_datum_value_set_null,
	avro_datum_value_set_string,
	avro_datum_value_set_string_len,
	avro_datum_value_give_string_len,
	avro_datum_value_set_enum,
	avro_datum_value_set_fixed,
	avro_datum_value_give_fixed,
	/* compound getters */
	avro_datum_value_get_size,
	avro_datum_value_get_by_index,
	avro_datum_value_get_by_name,
	avro_datum_value_get_discriminant,
	avro_datum_value_get_current_branch,
	/* compound setters */
	avro_datum_value_append,
	avro_datum_value_add,
	avro_datum_value_set_branch
};