avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bru...@apache.org
Subject svn commit: r1057661 - in /avro/trunk: CHANGES.txt lang/c/src/avro.h lang/c/src/schema.c lang/c/tests/CMakeLists.txt lang/c/tests/Makefile.am lang/c/tests/test_avro_schema_names.c
Date Tue, 11 Jan 2011 14:32:38 GMT
Author: brucem
Date: Tue Jan 11 14:32:37 2011
New Revision: 1057661

URL: http://svn.apache.org/viewvc?rev=1057661&view=rev
Log:
AVRO-725. Add avro_schema_get_subschema function.

This new function returns the immediate subschema with the given name.
For records, this is the name of the field.  For unions, it's the name
of the branch's schema type.  For arrays and maps, there is a single
special name (respectively "[]" and "{}") that represents the item or
value subschema.

Added:
    avro/trunk/lang/c/tests/test_avro_schema_names.c
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/c/src/avro.h
    avro/trunk/lang/c/src/schema.c
    avro/trunk/lang/c/tests/CMakeLists.txt
    avro/trunk/lang/c/tests/Makefile.am

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1057661&r1=1057660&r2=1057661&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Tue Jan 11 14:32:37 2011
@@ -88,6 +88,9 @@ Avro 1.5.0 (unreleased)
     AVRO-719. Java: Permit MapReduce programs to alter output file
     sync interval.  (Joe Crobak via cutting)
 
+    AVRO-725. C: avro_schema_get_subschema function. (Douglas Creager via
+    brucem)
+
   BUG FIXES
 
     AVRO-675. C: Bytes and fixed setters don't update datum size.

Modified: avro/trunk/lang/c/src/avro.h
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/avro.h?rev=1057661&r1=1057660&r2=1057661&view=diff
==============================================================================
--- avro/trunk/lang/c/src/avro.h (original)
+++ avro/trunk/lang/c/src/avro.h Tue Jan 11 14:32:37 2011
@@ -136,7 +136,10 @@ int avro_schema_to_json(avro_schema_t sc
 
 int avro_schema_to_specific(avro_schema_t schema, const char *prefix);
 
+avro_schema_t avro_schema_get_subschema(const avro_schema_t schema,
+         const char *name);
 const char *avro_schema_name(const avro_schema_t schema);
+const char *avro_schema_type_name(const avro_schema_t schema);
 avro_schema_t avro_schema_copy(avro_schema_t schema);
 int avro_schema_equal(avro_schema_t a, avro_schema_t b);
 

Modified: avro/trunk/lang/c/src/schema.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/schema.c?rev=1057661&r1=1057660&r2=1057661&view=diff
==============================================================================
--- avro/trunk/lang/c/src/schema.c (original)
+++ avro/trunk/lang/c/src/schema.c Tue Jan 11 14:32:37 2011
@@ -1025,6 +1025,64 @@ avro_schema_t avro_schema_copy(avro_sche
 	return new_schema;
 }
 
+avro_schema_t avro_schema_get_subschema(const avro_schema_t schema,
+         const char *name)
+{
+ if (is_avro_record(schema)) {
+   const struct avro_record_schema_t *rschema =
+     avro_schema_to_record(schema);
+   union {
+     st_data_t data;
+     struct avro_record_field_t *field;
+   } field;
+
+   if (st_lookup(rschema->fields_byname,
+           (st_data_t) name, &field.data))
+   {
+     return field.field->type;
+   }
+
+   return NULL;
+ } else if (is_avro_union(schema)) {
+   const struct avro_union_schema_t *uschema =
+     avro_schema_to_union(schema);
+   long i;
+
+   for (i = 0; i < uschema->branches->num_entries; i++) {
+     union {
+       st_data_t data;
+       avro_schema_t schema;
+     } val;
+     st_lookup(uschema->branches, i, &val.data);
+     if (strcmp(avro_schema_type_name(val.schema),
+          name) == 0)
+     {
+       return val.schema;
+     }
+   }
+
+   return NULL;
+ } else if (is_avro_array(schema)) {
+   if (strcmp(name, "[]") == 0) {
+     const struct avro_array_schema_t *aschema =
+       avro_schema_to_array(schema);
+     return aschema->items;
+   }
+
+   return NULL;
+ } else if (is_avro_map(schema)) {
+   if (strcmp(name, "{}") == 0) {
+     const struct avro_map_schema_t *mschema =
+       avro_schema_to_map(schema);
+     return mschema->values;
+   }
+
+   return NULL;
+ }
+
+ return NULL;
+}
+
 const char *avro_schema_name(const avro_schema_t schema)
 {
 	if (is_avro_record(schema)) {
@@ -1037,6 +1095,40 @@ const char *avro_schema_name(const avro_
 	return NULL;
 }
 
+const char *avro_schema_type_name(const avro_schema_t schema)
+{
+ if (is_avro_record(schema)) {
+   return (avro_schema_to_record(schema))->name;
+ } else if (is_avro_enum(schema)) {
+   return (avro_schema_to_enum(schema))->name;
+ } else if (is_avro_fixed(schema)) {
+   return (avro_schema_to_fixed(schema))->name;
+ } else if (is_avro_union(schema)) {
+   return "union";
+ } else if (is_avro_array(schema)) {
+   return "array";
+ } else if (is_avro_map(schema)) {
+   return "map";
+ } else if (is_avro_int32(schema)) {
+   return "int32";
+ } else if (is_avro_int64(schema)) {
+   return "int64";
+ } else if (is_avro_float(schema)) {
+   return "float";
+ } else if (is_avro_double(schema)) {
+   return "double";
+ } else if (is_avro_boolean(schema)) {
+   return "boolean";
+ } else if (is_avro_null(schema)) {
+   return "null";
+ } else if (is_avro_string(schema)) {
+   return "string";
+ } else if (is_avro_bytes(schema)) {
+   return "bytes";
+ }
+ return NULL;
+}
+
 /* simple helper for writing strings */
 static int avro_write_str(avro_writer_t out, const char *str)
 {

Modified: avro/trunk/lang/c/tests/CMakeLists.txt
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/tests/CMakeLists.txt?rev=1057661&r1=1057660&r2=1057661&view=diff
==============================================================================
--- avro/trunk/lang/c/tests/CMakeLists.txt (original)
+++ avro/trunk/lang/c/tests/CMakeLists.txt Tue Jan 11 14:32:37 2011
@@ -26,6 +26,10 @@ add_executable(test_avro_schema test_avr
 target_link_libraries(test_avro_schema avro-static)
 add_test(test_avro_schema ${CMAKE_COMMAND} -E chdir ${AvroC_SOURCE_DIR}/tests ${CMAKE_CURRENT_BINARY_DIR}/test_avro_schema)
 
+add_executable(test_avro_schema_names test_avro_schema_names.c)
+target_link_libraries(test_avro_schema_names avro-static)
+add_test(test_avro_schema_names ${CMAKE_COMMAND} -E chdir ${AvroC_SOURCE_DIR}/tests ${CMAKE_CURRENT_BINARY_DIR}/test_avro_schema_names)
+
 add_executable(test_avro_data test_avro_data.c)
 target_link_libraries(test_avro_data avro-static)
 add_test(test_avro_data ${CMAKE_COMMAND} -E chdir ${AvroC_SOURCE_DIR}/tests ${CMAKE_CURRENT_BINARY_DIR}/test_avro_data)

Modified: avro/trunk/lang/c/tests/Makefile.am
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/tests/Makefile.am?rev=1057661&r1=1057660&r2=1057661&view=diff
==============================================================================
--- avro/trunk/lang/c/tests/Makefile.am (original)
+++ avro/trunk/lang/c/tests/Makefile.am Tue Jan 11 14:32:37 2011
@@ -5,7 +5,7 @@ ACLOCAL_AMFLAGS=-I m4
 
 EXTRA_DIST=schema_tests test_valgrind
 
-check_PROGRAMS=test_avro_schema test_avro_data test_cpp
+check_PROGRAMS=test_avro_schema test_avro_schema_names test_avro_data test_cpp
 
 noinst_PROGRAMS=generate_interop_data test_interop_data
 
@@ -14,6 +14,9 @@ test_LDADD=$(top_builddir)/src/libavro.l
 test_avro_schema_SOURCES=test_avro_schema.c
 test_avro_schema_LDADD=$(test_LDADD)
 
+test_avro_schema_names_SOURCES=test_avro_schema_names.c
+test_avro_schema_names_LDADD=$(test_LDADD)
+
 test_avro_data_SOURCES=test_avro_data.c
 test_avro_data_LDADD=$(test_LDADD)
 

Added: avro/trunk/lang/c/tests/test_avro_schema_names.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/tests/test_avro_schema_names.c?rev=1057661&view=auto
==============================================================================
--- avro/trunk/lang/c/tests/test_avro_schema_names.c (added)
+++ avro/trunk/lang/c/tests/test_avro_schema_names.c Tue Jan 11 14:32:37 2011
@@ -0,0 +1,133 @@
+/*
+ * 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
+ * 
+ * http://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_private.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+int test_cases = 0;
+avro_writer_t avro_stderr;
+
+static void test_helper(const char *json,
+     const char *name,
+     avro_schema_t expected)
+{
+ int rc;
+ avro_schema_t base;
+ avro_schema_error_t serror;
+
+ rc = avro_schema_from_json(json, strlen(json), &base, &serror);
+ if (rc != 0)
+ {
+   fprintf(stderr,
+     "Error parsing Avro schema:\n%s\n",
+     json);
+   exit(EXIT_FAILURE);
+ }
+
+ avro_schema_t actual =
+   avro_schema_get_subschema(base, name);
+
+ if (actual == NULL)
+ {
+   fprintf(stderr,
+     "No subschema named \"%s\" in %s\n",
+     name, avro_schema_type_name(base));
+   exit(EXIT_FAILURE);
+ }
+
+ if (!avro_schema_equal(actual, expected))
+ {
+   fprintf(stderr,
+     "Subschema \"%s\" should be %s, "
+     "is actually %s\n",
+     name,
+     avro_schema_type_name(expected),
+     avro_schema_type_name(actual));
+   exit(EXIT_FAILURE);
+ }
+
+ avro_schema_decref(base);
+ avro_schema_decref(expected);
+}
+
+static void test_array_schema_01()
+{
+ static char *JSON =
+   "{"
+   "  \"type\": \"array\","
+   "  \"items\": \"long\""
+   "}";
+
+ test_helper(JSON, "[]", avro_schema_long());
+}
+
+static void test_map_schema_01()
+{
+ static char *JSON =
+   "{"
+   "  \"type\": \"map\","
+   "  \"values\": \"long\""
+   "}";
+
+ test_helper(JSON, "{}", avro_schema_long());
+}
+
+static void test_record_schema_01()
+{
+ static char *JSON =
+   "{"
+   "  \"type\": \"record\","
+   "  \"name\": \"test\","
+   "  \"fields\": ["
+   "    { \"name\": \"a\", \"type\": \"long\" }"
+   "  ]"
+   "}";
+
+ test_helper(JSON, "a", avro_schema_long());
+}
+
+static void test_union_schema_01()
+{
+ static char *JSON =
+   "["
+   "  \"long\","
+   "  {"
+   "    \"type\": \"record\","
+   "    \"name\": \"test\","
+   "    \"fields\": ["
+   "      { \"name\": \"a\", \"type\": \"long\" }"
+   "    ]"
+   "  }"
+   "]";
+
+ test_helper(JSON, "long", avro_schema_long());
+}
+
+int main(int argc, char *argv[])
+{
+ AVRO_UNUSED(argc);
+ AVRO_UNUSED(argv);
+
+ test_array_schema_01();
+
+ test_map_schema_01();
+
+ test_record_schema_01();
+
+ return EXIT_SUCCESS;
+}



Mime
View raw message