|
| 1 | +/* |
| 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 |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 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. |
| 16 | + */ |
| 17 | + |
| 18 | +#include <stdio.h> |
| 19 | +#include <string.h> |
| 20 | +#include <avro.h> |
| 21 | + |
| 22 | +#define check_exit(call) \ |
| 23 | + do { \ |
| 24 | + int __rc = call; \ |
| 25 | + if (__rc != 0) { \ |
| 26 | + fprintf(stderr, "Unexpected error:\n %s\n %s\n", \ |
| 27 | + avro_strerror(), #call); \ |
| 28 | + exit(EXIT_FAILURE); \ |
| 29 | + } \ |
| 30 | + } while (0) |
| 31 | + |
| 32 | +void check_json_encoding(const avro_value_t * val, const char * expected) { |
| 33 | + char * actual; |
| 34 | + |
| 35 | + check_exit(avro_value_to_json(val, 1, &actual)); |
| 36 | + if (strcmp(expected, actual) != 0) { |
| 37 | + fprintf(stderr, "json encoding mismatch\n expected: %s\n actual: %s\n", expected, actual); |
| 38 | + exit(EXIT_FAILURE); |
| 39 | + } |
| 40 | + free(actual); |
| 41 | +} |
| 42 | + |
| 43 | +int main(int argc, char **argv) |
| 44 | +{ |
| 45 | + const char *json = |
| 46 | + "{" |
| 47 | + " \"type\": \"record\"," |
| 48 | + " \"name\": \"r\"," |
| 49 | + " \"fields\": [" |
| 50 | + " { \"name\": \"field\", \"type\": [" |
| 51 | + " \"null\"," |
| 52 | + " \"int\"," |
| 53 | + " {" |
| 54 | + " \"type\": \"record\"," |
| 55 | + " \"name\": \"r1\"," |
| 56 | + " \"fields\": []" |
| 57 | + " }," |
| 58 | + " {" |
| 59 | + " \"type\": \"record\"," |
| 60 | + " \"name\": \"r2\"," |
| 61 | + " \"namespace\": \"space\"," |
| 62 | + " \"fields\": []" |
| 63 | + " }" |
| 64 | + " ]}" |
| 65 | + " ]" |
| 66 | + "}"; |
| 67 | + |
| 68 | + avro_schema_t schema = NULL; |
| 69 | + avro_schema_error_t error; |
| 70 | + |
| 71 | + (void) argc; |
| 72 | + (void) argv; |
| 73 | + |
| 74 | + check_exit(avro_schema_from_json(json, strlen(json), &schema, &error)); |
| 75 | + |
| 76 | + avro_value_iface_t *iface = avro_generic_class_from_schema(schema); |
| 77 | + avro_value_t val; |
| 78 | + check_exit(avro_generic_value_new(iface, &val)); |
| 79 | + |
| 80 | +#define TEST_AVRO_4135 (1) |
| 81 | + #if TEST_AVRO_4135 |
| 82 | + { |
| 83 | + avro_value_t field; |
| 84 | + avro_value_t branch; |
| 85 | + char *json_encoding; |
| 86 | + |
| 87 | + avro_value_get_by_index(&val, 0, &field, NULL); |
| 88 | + check_exit(avro_value_set_branch(&field, 0, &branch)); |
| 89 | + avro_value_set_null(&branch); |
| 90 | + check_json_encoding(&val, "{\"field\": null}"); |
| 91 | + |
| 92 | + check_exit(avro_value_set_branch(&field, 1, &branch)); |
| 93 | + avro_value_set_int(&branch, 42); |
| 94 | + check_exit(avro_value_to_json(&val, 1, &json_encoding)); |
| 95 | + check_json_encoding(&val, "{\"field\": {\"int\": 42}}"); |
| 96 | + |
| 97 | + check_exit(avro_value_set_branch(&field, 2, &branch)); |
| 98 | + check_exit(avro_value_to_json(&val, 1, &json_encoding)); |
| 99 | + check_json_encoding(&val, "{\"field\": {\"r1\": {}}}"); |
| 100 | + |
| 101 | + check_exit(avro_value_set_branch(&field, 3, &branch)); |
| 102 | + check_exit(avro_value_to_json(&val, 3, &json_encoding)); |
| 103 | + check_json_encoding(&val, "{\"field\": {\"space.r2\": {}}}"); |
| 104 | + } |
| 105 | + #endif |
| 106 | + |
| 107 | + avro_value_decref(&val); |
| 108 | + avro_schema_decref(schema); |
| 109 | + return 0; |
| 110 | +} |
0 commit comments