Fixed GenerateText not handling vectors of unions.
authorWouter van Oortmerssen <aardappel@gmail.com>
Mon, 16 Sep 2019 21:43:35 +0000 (14:43 -0700)
committerWouter van Oortmerssen <aardappel@gmail.com>
Mon, 16 Sep 2019 21:44:14 +0000 (14:44 -0700)
Change-Id: Ie82abaf178495c4692e7d10be6b4a13f2fa1bee6

src/idl_gen_text.cpp
tests/test.cpp

index 9825dce..8be4230 100644 (file)
@@ -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<typename T>
-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<EnumVal const *> 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<typename T, typename Container>
 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<const void *>(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<soffset_t>(i), opts, _text)) {
         return false;
       }
     }
@@ -113,43 +116,51 @@ bool PrintContainer(const Container &c, size_t size, Type type, int indent,
 
 template<typename T>
 bool PrintVector(const Vector<T> &v, Type type, int indent,
-                 const IDLOptions &opts, std::string *_text) {
-  return PrintContainer<T, Vector<T>>(v, v.size(), type, indent, opts, _text);
+                 const uint8_t *prev_val, const IDLOptions &opts,
+                 std::string *_text) {
+  return PrintContainer<T, Vector<T>>(v, v.size(), type, indent, prev_val, opts,
+                                      _text);
 }
 
 // Print an array a sequence of JSON values, comma separated, wrapped in "[]".
 template<typename T>
 bool PrintArray(const Array<T, 0xFFFF> &a, size_t size, Type type, int indent,
                 const IDLOptions &opts, std::string *_text) {
-  return PrintContainer<T, Array<T, 0xFFFF>>(a, size, type, indent, opts,
-                                             _text);
+  return PrintContainer<T, Array<T, 0xFFFF>>(a, size, type, indent, nullptr,
+                                             opts, _text);
 }
 
 // Specialization of Print above for pointer types.
 template<>
 bool Print<const void *>(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<const void *>(val, *union_type, indent, nullptr, opts,
-                                 _text);
-    case BASE_TYPE_STRUCT:
-      if (!GenStruct(*type.struct_def, reinterpret_cast<const Table *>(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<const Vector<uint8_t> *>(prev_val +
+                                               ReadScalar<uoffset_t>(prev_val));
+        union_type_byte = type_vec->Get(static_cast<uoffset_t>(vector_index));
+      }
+      auto enum_val = type.enum_def->ReverseLookup(union_type_byte, true);
+      if (enum_val) {
+        return Print<const void *>(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<const Table *>(val),
+                       indent, opts, _text);
     case BASE_TYPE_STRING: {
       auto s = reinterpret_cast<const String *>(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 *>(const void *val, Type type, int indent,
           case BASE_TYPE_ ## ENUM: \
             if (!PrintVector<CTYPE>( \
                   *reinterpret_cast<const Vector<CTYPE> *>(val), \
-                  vec_type, indent, opts, _text)) { \
+                  vec_type, indent, prev_val, opts, _text)) { \
               return false; \
             } \
             break;
@@ -169,7 +180,7 @@ bool Print<const void *>(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 *>(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<typename T> static T GetFieldDefault(const FieldDef &fd) {
@@ -215,7 +227,7 @@ static bool GenField(const FieldDef &fd, const Table *table, bool fixed,
       fixed ? reinterpret_cast<const Struct *>(table)->GetField<T>(
                   fd.value.offset)
             : table->GetField<T>(fd.value.offset, GetFieldDefault<T>(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<const void *>(fd.value.offset)
               : table->GetPointer<const void *>(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<uint8_t>(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<const uint8_t *>(table) + fd.value.offset;
+      } else {
+        prev_val = table->GetAddressOf(fd.value.offset);
       }
     }
   }
index a3474cb..dfa0d6a 100644 (file)
@@ -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 }"