Author: martinkl
Date: Thu Jan 21 10:59:16 2016
New Revision: 1725902

URL: http://svn.apache.org/viewvc?rev=1725902&view=rev
Log:
AVRO-1691. C: Allow schemas consisting only of a primitive type. Contributed by 
Magnus Edenhill.

Added:
    avro/branches/branch-1.8/lang/c/tests/test_avro_1691.c
Modified:
    avro/branches/branch-1.8/CHANGES.txt
    avro/branches/branch-1.8/lang/c/src/schema.c
    avro/branches/branch-1.8/lang/c/tests/CMakeLists.txt

Modified: avro/branches/branch-1.8/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/avro/branches/branch-1.8/CHANGES.txt?rev=1725902&r1=1725901&r2=1725902&view=diff
==============================================================================
--- avro/branches/branch-1.8/CHANGES.txt (original)
+++ avro/branches/branch-1.8/CHANGES.txt Thu Jan 21 10:59:16 2016
@@ -258,6 +258,9 @@ Avro 1.8.0 (15 December 2015)
     AVRO-1769. C: Use operating system's libjansson instead of bundling
     it with Avro. (Magnus Edenhill via martinkl)
 
+    AVRO-1691. C: Allow schemas consisting only of a primitive type.
+    (Magnus Edenhill via martinkl)
+
 Avro 1.7.7 (23 July 2014)
 
   NEW FEATURES

Modified: avro/branches/branch-1.8/lang/c/src/schema.c
URL: 
http://svn.apache.org/viewvc/avro/branches/branch-1.8/lang/c/src/schema.c?rev=1725902&r1=1725901&r2=1725902&view=diff
==============================================================================
--- avro/branches/branch-1.8/lang/c/src/schema.c (original)
+++ avro/branches/branch-1.8/lang/c/src/schema.c Thu Jan 21 10:59:16 2016
@@ -1136,7 +1136,7 @@ avro_schema_from_json(const char *jsonte
        AVRO_UNUSED(len);
        AVRO_UNUSED(e);
 
-       root = json_loads(jsontext, 0, &json_error);
+       root = json_loads(jsontext, JSON_DECODE_ANY, &json_error);
        if (!root) {
                avro_set_error("Error parsing JSON: %s", json_error.text);
                return EINVAL;
@@ -1155,7 +1155,7 @@ avro_schema_from_json_length(const char
        json_t  *root;
        json_error_t  json_error;
 
-       root = json_loadb(jsontext, length, 0, &json_error);
+       root = json_loadb(jsontext, length, JSON_DECODE_ANY, &json_error);
        if (!root) {
                avro_set_error("Error parsing JSON: %s", json_error.text);
                return EINVAL;

Modified: avro/branches/branch-1.8/lang/c/tests/CMakeLists.txt
URL: 
http://svn.apache.org/viewvc/avro/branches/branch-1.8/lang/c/tests/CMakeLists.txt?rev=1725902&r1=1725901&r2=1725902&view=diff
==============================================================================
--- avro/branches/branch-1.8/lang/c/tests/CMakeLists.txt (original)
+++ avro/branches/branch-1.8/lang/c/tests/CMakeLists.txt Thu Jan 21 10:59:16 
2016
@@ -63,3 +63,4 @@ add_avro_test(test_avro_data)
 add_avro_test(test_refcount)
 add_avro_test(test_cpp test_cpp.cpp)
 add_avro_test(test_avro_1379)
+add_avro_test(test_avro_1691)

Added: avro/branches/branch-1.8/lang/c/tests/test_avro_1691.c
URL: 
http://svn.apache.org/viewvc/avro/branches/branch-1.8/lang/c/tests/test_avro_1691.c?rev=1725902&view=auto
==============================================================================
--- avro/branches/branch-1.8/lang/c/tests/test_avro_1691.c (added)
+++ avro/branches/branch-1.8/lang/c/tests/test_avro_1691.c Thu Jan 21 10:59:16 
2016
@@ -0,0 +1,94 @@
+/*
+ * 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.
+ */
+
+/**
+ * AVRO-1691 test case - support of string-field JSON schemas.
+ */
+#include "avro.h"
+#include "avro_private.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+static const char *json_schemas[] = {
+        /* These two schemas are functionally equivalent. */
+        "{ \"type\": \"string\" }", /* Object wrapped */
+        "\"string\"",               /* JSON string field */
+        NULL
+};
+
+
+
+
+int main(void)
+{
+        int pass;
+
+        for (pass = 0 ; json_schemas[pass] ; pass++) {
+                int rval = 0;
+                size_t len;
+                static char  buf[4096];
+                avro_writer_t  writer;
+                avro_file_writer_t file_writer;
+                avro_file_reader_t file_reader;
+                avro_schema_t  schema = NULL;
+                avro_schema_error_t  error = NULL;
+                char outpath[64];
+                const char *json_schema = json_schemas[pass];
+
+                printf("pass %d with schema %s\n", pass, json_schema);
+                check(rval, avro_schema_from_json(json_schema, 
strlen(json_schema),
+                                                  &schema, &error));
+
+                avro_value_iface_t  *iface = 
avro_generic_class_from_schema(schema);
+
+                avro_value_t  val;
+                avro_generic_value_new(iface, &val);
+
+                avro_value_t  out;
+                avro_generic_value_new(iface, &out);
+
+                /* create the val */
+                avro_value_reset(&val);
+                avro_value_set_string(&val, "test-1691");
+
+                /* Write value to file */
+                snprintf(outpath, sizeof(outpath), "test-1691-%d.avro", pass);
+
+                /* create the writers */
+                writer = avro_writer_memory(buf, sizeof(buf));
+                check(rval, avro_file_writer_create(outpath, schema, 
&file_writer));
+
+                check(rval, avro_value_write(writer, &val));
+
+                len = avro_writer_tell(writer);
+                check(rval, avro_file_writer_append_encoded(file_writer, buf, 
len));
+                check(rval, avro_file_writer_close(file_writer));
+
+                /* Read the value back */
+                check(rval, avro_file_reader(outpath, &file_reader));
+                check(rval, avro_file_reader_read_value(file_reader, &out));
+                if (!avro_value_equal(&val, &out)) {
+                        fprintf(stderr, "fail!\n");
+                        exit(EXIT_FAILURE);
+                }
+                fprintf(stderr, "pass %d: ok: schema %s\n", pass, json_schema);
+                check(rval, avro_file_reader_close(file_reader));
+                remove(outpath);
+        }
+
+       exit(EXIT_SUCCESS);
+}


Reply via email to