From f9277e691dd10e42a999bba683db5df7d49226d6 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 16 Sep 2019 14:43:35 -0700 Subject: [PATCH] Fixed GenerateText not handling vectors of unions. Change-Id: Ie82abaf178495c4692e7d10be6b4a13f2fa1bee6 --- src/idl_gen_text.cpp | 91 +++++++++++++++++++++++++------------------- tests/test.cpp | 34 +++++++++++++++++ 2 files changed, 86 insertions(+), 39 deletions(-) diff --git a/src/idl_gen_text.cpp b/src/idl_gen_text.cpp index 9825dcedb..8be4230ed 100644 --- a/src/idl_gen_text.cpp +++ b/src/idl_gen_text.cpp @@ -47,8 +47,9 @@ void OutputIdentifier(const std::string &name, const IDLOptions &opts, // for a single FlatBuffer value into JSON format. // The general case for scalars: template -bool Print(T val, Type type, int /*indent*/, Type * /*union_type*/, - const IDLOptions &opts, std::string *_text) { +bool Print(T val, Type type, int /*indent*/, const uint8_t * /*prev_val*/, + soffset_t /*vector_index*/, const IDLOptions &opts, + std::string *_text) { std::string &text = *_text; if (type.enum_def && opts.output_enum_identifiers) { std::vector enum_values; @@ -83,7 +84,8 @@ bool Print(T val, Type type, int /*indent*/, Type * /*union_type*/, // Print a vector or an array of JSON values, comma seperated, wrapped in "[]". template bool PrintContainer(const Container &c, size_t size, Type type, int indent, - const IDLOptions &opts, std::string *_text) { + const uint8_t *prev_val, const IDLOptions &opts, + std::string *_text) { std::string &text = *_text; text += "["; text += NewLine(opts); @@ -96,11 +98,12 @@ bool PrintContainer(const Container &c, size_t size, Type type, int indent, if (IsStruct(type)) { if (!Print(reinterpret_cast(c.Data() + i * type.struct_def->bytesize), - type, indent + Indent(opts), nullptr, opts, _text)) { + type, indent + Indent(opts), nullptr, -1, opts, _text)) { return false; } } else { - if (!Print(c[i], type, indent + Indent(opts), nullptr, opts, _text)) { + if (!Print(c[i], type, indent + Indent(opts), prev_val, + static_cast(i), opts, _text)) { return false; } } @@ -113,43 +116,51 @@ bool PrintContainer(const Container &c, size_t size, Type type, int indent, template bool PrintVector(const Vector &v, Type type, int indent, - const IDLOptions &opts, std::string *_text) { - return PrintContainer>(v, v.size(), type, indent, opts, _text); + const uint8_t *prev_val, const IDLOptions &opts, + std::string *_text) { + return PrintContainer>(v, v.size(), type, indent, prev_val, opts, + _text); } // Print an array a sequence of JSON values, comma separated, wrapped in "[]". template bool PrintArray(const Array &a, size_t size, Type type, int indent, const IDLOptions &opts, std::string *_text) { - return PrintContainer>(a, size, type, indent, opts, - _text); + return PrintContainer>(a, size, type, indent, nullptr, + opts, _text); } // Specialization of Print above for pointer types. template<> bool Print(const void *val, Type type, int indent, - Type *union_type, const IDLOptions &opts, - std::string *_text) { + const uint8_t *prev_val, soffset_t vector_index, + const IDLOptions &opts, std::string *_text) { switch (type.base_type) { - case BASE_TYPE_UNION: + case BASE_TYPE_UNION: { // If this assert hits, you have an corrupt buffer, a union type field // was not present or was out of range. - FLATBUFFERS_ASSERT(union_type); - return Print(val, *union_type, indent, nullptr, opts, - _text); - case BASE_TYPE_STRUCT: - if (!GenStruct(*type.struct_def, reinterpret_cast(val), - indent, opts, _text)) { + FLATBUFFERS_ASSERT(prev_val); + auto union_type_byte = *prev_val; // Always a uint8_t. + if (vector_index >= 0) { + auto type_vec = reinterpret_cast *>(prev_val + + ReadScalar(prev_val)); + union_type_byte = type_vec->Get(static_cast(vector_index)); + } + auto enum_val = type.enum_def->ReverseLookup(union_type_byte, true); + if (enum_val) { + return Print(val, enum_val->union_type, indent, nullptr, + -1, opts, _text); + } else { return false; } - break; + } + case BASE_TYPE_STRUCT: + return GenStruct(*type.struct_def, reinterpret_cast(val), + indent, opts, _text); case BASE_TYPE_STRING: { auto s = reinterpret_cast(val); - if (!EscapeString(s->c_str(), s->size(), _text, opts.allow_non_utf8, - opts.natural_utf8)) { - return false; - } - break; + return EscapeString(s->c_str(), s->size(), _text, opts.allow_non_utf8, + opts.natural_utf8); } case BASE_TYPE_VECTOR: { const auto vec_type = type.VectorType(); @@ -161,7 +172,7 @@ bool Print(const void *val, Type type, int indent, case BASE_TYPE_ ## ENUM: \ if (!PrintVector( \ *reinterpret_cast *>(val), \ - vec_type, indent, opts, _text)) { \ + vec_type, indent, prev_val, opts, _text)) { \ return false; \ } \ break; @@ -169,7 +180,7 @@ bool Print(const void *val, Type type, int indent, #undef FLATBUFFERS_TD } // clang-format on - break; + return true; } case BASE_TYPE_ARRAY: { const auto vec_type = type.VectorType(); @@ -192,11 +203,12 @@ bool Print(const void *val, Type type, int indent, case BASE_TYPE_ARRAY: FLATBUFFERS_ASSERT(0); } // clang-format on - break; + return true; } - default: FLATBUFFERS_ASSERT(0); + default: + FLATBUFFERS_ASSERT(0); + return false; } - return true; } template static T GetFieldDefault(const FieldDef &fd) { @@ -215,7 +227,7 @@ static bool GenField(const FieldDef &fd, const Table *table, bool fixed, fixed ? reinterpret_cast(table)->GetField( fd.value.offset) : table->GetField(fd.value.offset, GetFieldDefault(fd)), - fd.value.type, indent, nullptr, opts, _text); + fd.value.type, indent, nullptr, -1, opts, _text); } static bool GenStruct(const StructDef &struct_def, const Table *table, @@ -223,8 +235,8 @@ static bool GenStruct(const StructDef &struct_def, const Table *table, // Generate text for non-scalar field. static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed, - int indent, Type *union_type, const IDLOptions &opts, - std::string *_text) { + int indent, const uint8_t *prev_val, + const IDLOptions &opts, std::string *_text) { const void *val = nullptr; if (fixed) { // The only non-scalar fields in structs are structs or arrays. @@ -245,7 +257,7 @@ static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed, ? table->GetStruct(fd.value.offset) : table->GetPointer(fd.value.offset); } - return Print(val, fd.value.type, indent, union_type, opts, _text); + return Print(val, fd.value.type, indent, prev_val, -1, opts, _text); } // Generate text for a struct or table, values separated by commas, indented, @@ -255,7 +267,7 @@ static bool GenStruct(const StructDef &struct_def, const Table *table, std::string &text = *_text; text += "{"; int fieldout = 0; - Type *union_type = nullptr; + const uint8_t *prev_val = nullptr; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { FieldDef &fd = **it; @@ -294,16 +306,17 @@ static bool GenStruct(const StructDef &struct_def, const Table *table, FLATBUFFERS_GEN_TYPE_ARRAY(FLATBUFFERS_TD) #undef FLATBUFFERS_TD if (!GenFieldOffset(fd, table, struct_def.fixed, indent + Indent(opts), - union_type, opts, _text)) { + prev_val, opts, _text)) { return false; } break; // clang-format on } - if (fd.value.type.base_type == BASE_TYPE_UTYPE) { - auto enum_val = fd.value.type.enum_def->ReverseLookup( - table->GetField(fd.value.offset, 0), true); - union_type = enum_val ? &enum_val->union_type : nullptr; + // Track prev val for use with union types. + if (struct_def.fixed) { + prev_val = reinterpret_cast(table) + fd.value.offset; + } else { + prev_val = table->GetAddressOf(fd.value.offset); } } } diff --git a/tests/test.cpp b/tests/test.cpp index a3474cb5f..dfa0d6ab1 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -2410,6 +2410,7 @@ void UnionVectorTest() { TestMovie(repacked_movie); + // Generate text using mini-reflection. auto s = flatbuffers::FlatBufferToString(fbb.GetBufferPointer(), MovieTypeTable()); TEST_EQ_STR( @@ -2451,6 +2452,39 @@ void UnionVectorTest() { " ]\n" "}"); + // Generate text using parsed schema. + std::string jsongen; + auto result = GenerateText(parser, fbb.GetBufferPointer(), &jsongen); + TEST_EQ(result, true); + TEST_EQ_STR( + jsongen.c_str(), + "{\n" + " main_character_type: \"Rapunzel\",\n" + " main_character: {\n" + " hair_length: 6\n" + " },\n" + " characters_type: [\n" + " \"Belle\",\n" + " \"MuLan\",\n" + " \"BookFan\",\n" + " \"Other\",\n" + " \"Unused\"\n" + " ],\n" + " characters: [\n" + " {\n" + " books_read: 7\n" + " },\n" + " {\n" + " sword_attack_damage: 5\n" + " },\n" + " {\n" + " books_read: 2\n" + " },\n" + " \"Other\",\n" + " \"Unused\"\n" + " ]\n" + "}\n"); + flatbuffers::Parser parser2(idl_opts); TEST_EQ(parser2.Parse("struct Bool { b:bool; }" "union Any { Bool }"