avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dcrea...@apache.org
Subject svn commit: r1070941 - in /avro/trunk: CHANGES.txt lang/c/src/avro.h lang/c/tests/test_avro_data.c
Date Tue, 15 Feb 2011 15:40:18 GMT
Author: dcreager
Date: Tue Feb 15 15:40:18 2011
New Revision: 1070941

URL: http://svn.apache.org/viewvc?rev=1070941&view=rev
Log:
AVRO-744. C: Helper macros for extracting and setting record field values

This patch adds two helper macros that can be used to extract or set the
value of a record field using fewer function calls.  It wraps a call to
avro_record_get, which extracts the avro_datum_t instance for the field,
and a call to avro_XXX_get or avro_XXX_set on the field datum.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/c/src/avro.h
    avro/trunk/lang/c/tests/test_avro_data.c

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1070941&r1=1070940&r2=1070941&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Tue Feb 15 15:40:18 2011
@@ -157,6 +157,9 @@ Avro 1.5.0 (unreleased)
     AVRO-749. Don't install Jansson build artifacts. (Douglas Creager via
     brucem)
 
+    AVRO-744. C: Helper macros for extracting and setting record field
+    values (dcreager)
+
   BUG FIXES
 
     AVRO-743. Java: Performance Regression and memory pressure with 

Modified: avro/trunk/lang/c/src/avro.h
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/avro.h?rev=1070941&r1=1070940&r2=1070941&view=diff
==============================================================================
--- avro/trunk/lang/c/src/avro.h (original)
+++ avro/trunk/lang/c/src/avro.h Tue Feb 15 15:40:18 2011
@@ -274,6 +274,21 @@ const char *avro_enum_get_name(const avr
 int avro_fixed_get(avro_datum_t datum, char **bytes, int64_t * size);
 int avro_record_get(const avro_datum_t record, const char *field_name,
 		    avro_datum_t * value);
+
+/*
+ * A helper macro that extracts the value of the given field of a
+ * record.
+ */
+
+#define avro_record_get_field_value(rc, rec, typ, fname, ...)	\
+	do {							\
+		avro_datum_t  field = NULL;			\
+		(rc) = avro_record_get((rec), (fname), &field);	\
+		if (rc) break;					\
+		(rc) = avro_##typ##_get(field, __VA_ARGS__);	\
+	} while (0)
+
+
 int avro_map_get(const avro_datum_t datum, const char *key,
 		 avro_datum_t * value);
 /*
@@ -322,6 +337,19 @@ int avro_givefixed_set(avro_datum_t datu
 
 int avro_record_set(avro_datum_t record, const char *field_name,
 		    avro_datum_t value);
+
+/*
+ * A helper macro that sets the value of the given field of a record.
+ */
+
+#define avro_record_set_field_value(rc, rec, typ, fname, ...)	\
+	do {							\
+		avro_datum_t  field = NULL;			\
+		(rc) = avro_record_get((rec), (fname), &field);	\
+		if (rc) break;					\
+		(rc) = avro_##typ##_set(field, __VA_ARGS__);	\
+	} while (0)
+
 int avro_map_set(avro_datum_t map, const char *key,
 		 avro_datum_t value);
 int avro_array_append_datum(avro_datum_t array_datum,

Modified: avro/trunk/lang/c/tests/test_avro_data.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/tests/test_avro_data.c?rev=1070941&r1=1070940&r2=1070941&view=diff
==============================================================================
--- avro/trunk/lang/c/tests/test_avro_data.c (original)
+++ avro/trunk/lang/c/tests/test_avro_data.c Tue Feb 15 15:40:18 2011
@@ -325,6 +325,16 @@ static int test_record(void)
 	test_json(datum, schema,
 		  "{\"name\": \"Joseph Campbell\", \"age\": 83}");
 
+	int  rc;
+	avro_record_set_field_value(rc, datum, int32, "age", 104);
+
+	int32_t  age = 0;
+	avro_record_get_field_value(rc, datum, int32, "age", &age);
+	if (age != 104) {
+		fprintf(stderr, "Incorrect age value\n");
+		exit(EXIT_FAILURE);
+	}
+
 	avro_datum_decref(name_datum);
 	avro_datum_decref(age_datum);
 	avro_datum_decref(datum);



Mime
View raw message