Make nested flatbuffer lookup consistent. (#4656)
authorsmillius <37065579+smillius@users.noreply.github.com>
Mon, 5 Mar 2018 16:44:14 +0000 (17:44 +0100)
committerWouter van Oortmerssen <aardappel@gmail.com>
Mon, 5 Mar 2018 16:44:14 +0000 (08:44 -0800)
Lookup type of nested flatbuffer field with either raw name or fully qualified name as already done in the parser.
LookupCreateStruct tries both the raw name and the fully qualified one.
Without this, we cannot reference types outside of the current namespace, e.g. in a different module.

src/idl_gen_cpp.cpp

index 30bec09..de19219 100644 (file)
@@ -1687,9 +1687,13 @@ class CppGenerator : public BaseGenerator {
 
       auto nested = field.attributes.Lookup("nested_flatbuffer");
       if (nested) {
-        std::string qualified_name =
-            parser_.current_namespace_->GetFullyQualifiedName(nested->constant);
-        auto nested_root = parser_.LookupStruct(qualified_name);
+        std::string qualified_name = nested->constant;
+        auto nested_root = parser_.LookupStruct(nested->constant);
+        if (nested_root == nullptr) {
+          qualified_name = parser_.current_namespace_->GetFullyQualifiedName(
+              nested->constant);
+          nested_root = parser_.LookupStruct(qualified_name);
+        }
         assert(nested_root);  // Guaranteed to exist by parser.
         (void)nested_root;
         code_.SetValue("CPP_NAME", TranslateNameSpace(qualified_name));