Browse Source

Add package-based generation for multi-file generator

- Update tests
Alexey Edelev 5 years ago
parent
commit
98e18d993b

+ 11 - 1
src/generator/classgeneratorbase.cpp

@@ -412,9 +412,19 @@ void ClassGeneratorBase::printInclude(const google::protobuf::Descriptor *messag
             printInclude(message, field->message_type()->field(1), existingIncludes);
             printInclude(message, field->message_type()->field(1), existingIncludes);
             includeTemplate = Templates::ExternalIncludeTemplate;
             includeTemplate = Templates::ExternalIncludeTemplate;
         } else {
         } else {
+            std::string outFileBasename = "";
+            std::string fieldPackage = field->message_type()->file()->package();
+            if (fieldPackage != message->file()->package()) {
+                std::vector<std::string> packages;
+                utils::split(fieldPackage, packages, '.');
+                for (auto package : packages) {
+                    outFileBasename += package + "/";
+                }
+            }
+
             std::string typeName = field->message_type()->name();
             std::string typeName = field->message_type()->name();
             utils::tolower(typeName);
             utils::tolower(typeName);
-            newInclude = typeName;
+            newInclude = outFileBasename + typeName;
             includeTemplate = Templates::InternalIncludeTemplate;
             includeTemplate = Templates::InternalIncludeTemplate;
         }
         }
         break;
         break;

+ 2 - 0
src/generator/generator.cpp

@@ -84,6 +84,7 @@ bool QtGenerator::Generate(const FileDescriptor *file,
 
 
         std::string baseFilename(message->name());
         std::string baseFilename(message->name());
         utils::tolower(baseFilename);
         utils::tolower(baseFilename);
+        baseFilename = generateBaseName(file, baseFilename);
 
 
         std::string filename = baseFilename + ".h";
         std::string filename = baseFilename + ".h";
         ProtobufClassGenerator classGen(message,
         ProtobufClassGenerator classGen(message,
@@ -100,6 +101,7 @@ bool QtGenerator::Generate(const FileDescriptor *file,
         const ServiceDescriptor *service = file->service(i);
         const ServiceDescriptor *service = file->service(i);
         std::string baseFilename(service->name());
         std::string baseFilename(service->name());
         utils::tolower(baseFilename);
         utils::tolower(baseFilename);
+        baseFilename = generateBaseName(file, baseFilename);
 
 
         std::string fullFilename = baseFilename + "server.h";
         std::string fullFilename = baseFilename + "server.h";
         ServerGenerator serverGen(service,
         ServerGenerator serverGen(service,

+ 17 - 0
src/generator/generatorbase.cpp

@@ -70,3 +70,20 @@ bool GeneratorBase::GenerateAll(const std::vector<const FileDescriptor *> &files
 {
 {
     return CodeGenerator::GenerateAll(files, parameter, generatorContext, error);
     return CodeGenerator::GenerateAll(files, parameter, generatorContext, error);
 }
 }
+
+std::string GeneratorBase::generateBaseName(const ::google::protobuf::FileDescriptor *file, std::string name)
+{
+    std::vector<std::string> packages;
+    utils::split(file->package(), packages, '.');
+    std::string outFileBasename = "";
+    for (auto package : packages) {
+        outFileBasename += package + "/";
+    }
+    if (name != "") {
+        outFileBasename += name;
+    } else {
+        outFileBasename += utils::extractFileName(file->name());
+    }
+
+    return outFileBasename;
+}

+ 2 - 1
src/generator/generatorbase.h

@@ -30,6 +30,7 @@
 #include <string>
 #include <string>
 #include <memory>
 #include <memory>
 #include <functional>
 #include <functional>
+#include <google/protobuf/descriptor.h>
 
 
 namespace google { namespace protobuf {
 namespace google { namespace protobuf {
 class FileDescriptor;
 class FileDescriptor;
@@ -64,7 +65,7 @@ public:
 
 
 protected:
 protected:
     void iterateNonNestedFileds(const ::google::protobuf::FileDescriptor *file, std::function<void(const ::google::protobuf::Descriptor *)> callback) const;
     void iterateNonNestedFileds(const ::google::protobuf::FileDescriptor *file, std::function<void(const ::google::protobuf::Descriptor *)> callback) const;
-
+    static std::string generateBaseName(const ::google::protobuf::FileDescriptor *file, std::string name = "");
 private:
 private:
     Mode m_mode;
     Mode m_mode;
 };
 };

+ 0 - 13
src/generator/singlefilegenerator.cpp

@@ -48,19 +48,6 @@ using namespace ::QtProtobuf::generator;
 using namespace ::google::protobuf;
 using namespace ::google::protobuf;
 using namespace ::google::protobuf::compiler;
 using namespace ::google::protobuf::compiler;
 
 
-std::string generateBaseName(const FileDescriptor *file)
-{
-    std::vector<std::string> packages;
-    utils::split(file->package(), packages, '.');
-    std::string outFileBasename = "";
-    for (auto package : packages) {
-        outFileBasename += package + "/";
-    }
-    outFileBasename += utils::extractFileName(file->name());
-
-    return outFileBasename;
-}
-
 SingleFileGenerator::SingleFileGenerator() : GeneratorBase(GeneratorBase::SingleMode)
 SingleFileGenerator::SingleFileGenerator() : GeneratorBase(GeneratorBase::SingleMode)
 {}
 {}
 
 

+ 5 - 2
src/protobuf/parsemessages.go

@@ -59,14 +59,17 @@ func main() {
 
 
 	if multi {
 	if multi {
 		for scanner.Scan() {
 		for scanner.Scan() {
+			if fullpath == "" {
+				log.Fatalf("Package is not specified correctly in %s file\n", os.Args[1])
+			}
 			capture := messageFinder.FindStringSubmatch(scanner.Text())
 			capture := messageFinder.FindStringSubmatch(scanner.Text())
 			if len(capture) == 2 {
 			if len(capture) == 2 {
-				fmt.Printf("%s.h;", strings.ToLower(capture[1]))
+				fmt.Printf("%s%s.h;", fullpath, strings.ToLower(capture[1]))
 			}
 			}
 
 
 			capture = serviceFinder.FindStringSubmatch(scanner.Text())
 			capture = serviceFinder.FindStringSubmatch(scanner.Text())
 			if len(capture) == 2 {
 			if len(capture) == 2 {
-				fmt.Printf("%sclient.h;", strings.ToLower(capture[1]))
+				fmt.Printf("%s%sclient.h;", fullpath, strings.ToLower(capture[1]))
 			}
 			}
 		}
 		}
 	} else {
 	} else {

+ 0 - 1
tests/test_protobuf/CMakeLists.txt

@@ -11,7 +11,6 @@ file(GLOB SOURCES
     jsonserializationtest.cpp)
     jsonserializationtest.cpp)
 
 
 add_test_target(TARGET ${TARGET}
 add_test_target(TARGET ${TARGET}
-    EXCLUDE_HEADERS nestedsimpleintmessage.h
     SOURCES ${SOURCES}
     SOURCES ${SOURCES}
     QML)
     QML)
 add_target_windeployqt(TARGET ${TARGET}
 add_target_windeployqt(TARGET ${TARGET}

+ 1 - 1
tests/test_protobuf_multifile/CMakeLists.txt

@@ -9,7 +9,7 @@ file(GLOB PROTO_FILES ABSOLUTE ${CMAKE_CURRENT_SOURCE_DIR}/../test_protobuf/prot
 
 
 add_test_target(TARGET ${TARGET}
 add_test_target(TARGET ${TARGET}
     PROTO_FILES ${PROTO_FILES}
     PROTO_FILES ${PROTO_FILES}
-    EXCLUDE_HEADERS nestedsimpleintmessage.h
+    EXCLUDE_HEADERS qtprotobufnamespace/tests/nested/nestedsimpleintmessage.h
     SOURCES ${SOURCES}
     SOURCES ${SOURCES}
     QML
     QML
     MULTI)
     MULTI)

+ 63 - 63
tests/test_protobuf_multifile/simpletest.cpp

@@ -23,69 +23,69 @@
  * DEALINGS IN THE SOFTWARE.
  * DEALINGS IN THE SOFTWARE.
  */
  */
 
 
-#include "stepchildenummessage.h"
-#include "simpleboolmessage.h"
-#include "simpleintmessage.h"
-#include "simplesintmessage.h"
-#include "simpleuintmessage.h"
-#include "simpleint64message.h"
-#include "simplesint64message.h"
-#include "simpleuint64message.h"
-#include "simplefixedint32message.h"
-#include "simplefixedint64message.h"
-#include "simplesfixedint32message.h"
-#include "simplesfixedint64message.h"
-#include "simplestringmessage.h"
-#include "simplefloatmessage.h"
-#include "simpledoublemessage.h"
-#include "simpleenummessage.h"
-#include "simpleenumlistmessage.h"
-#include "simplefileenummessage.h"
-#include "simpleexternalenummessage.h"
-#include "externalcomplexmessage.h"
-#include "complexmessage.h"
-#include "simplebytesmessage.h"
-#include "repeatedintmessage.h"
-#include "repeatedbytesmessage.h"
-#include "repeateddoublemessage.h"
-#include "repeatedcomplexmessage.h"
-#include "repeatedfloatmessage.h"
-#include "repeatedsintmessage.h"
-#include "repeatedstringmessage.h"
-#include "repeateduintmessage.h"
-#include "repeatedint64message.h"
-#include "repeatedsint64message.h"
-#include "repeateduint64message.h"
-#include "repeatedfixedintmessage.h"
-#include "repeatedsfixedintmessage.h"
-#include "repeatedfixedint64message.h"
-#include "repeatedsfixedint64message.h"
-#include "repeatedexternalcomplexmessage.h"
-#include "simplesint32stringmapmessage.h"
-#include "simplestringstringmapmessage.h"
-#include "emptymessage.h"
-#include "message_uderscore_name.h"
-#include "messageuderscorename.h"
-#include "messageunderscorefield.h"
-#include "priormessageunderscorefield.h"
-#include "followingmessageunderscorefield.h"
-#include "combinedmessageunderscorefield.h"
-#include "testmessagesequence.h"
-#include "testmessagesequence2.h"
-#include "cyclingseconddependency.h"
-#include "cyclingfirstdependency.h"
-#include "messageuppercasereserved.h"
-#include "messagereserved.h"
-#include "messageenumreserved.h"
-#include "repeatedfieldsequence.h"
-#include "repeatedfieldsequence2.h"
-#include "mapfieldsequence.h"
-#include "mapfieldsequence2.h"
-#include "lowercasesequence.h"
-#include "lowercasesequence2.h"
-#include "lowercasemessagename.h"
-#include "lowercasefieldmessagename.h"
-#include "messageuppercase.h"
+#include "qtprotobufnamespace/tests/stepchildenummessage.h"
+#include "qtprotobufnamespace/tests/simpleboolmessage.h"
+#include "qtprotobufnamespace/tests/simpleintmessage.h"
+#include "qtprotobufnamespace/tests/simplesintmessage.h"
+#include "qtprotobufnamespace/tests/simpleuintmessage.h"
+#include "qtprotobufnamespace/tests/simpleint64message.h"
+#include "qtprotobufnamespace/tests/simplesint64message.h"
+#include "qtprotobufnamespace/tests/simpleuint64message.h"
+#include "qtprotobufnamespace/tests/simplefixedint32message.h"
+#include "qtprotobufnamespace/tests/simplefixedint64message.h"
+#include "qtprotobufnamespace/tests/simplesfixedint32message.h"
+#include "qtprotobufnamespace/tests/simplesfixedint64message.h"
+#include "qtprotobufnamespace/tests/simplestringmessage.h"
+#include "qtprotobufnamespace/tests/simplefloatmessage.h"
+#include "qtprotobufnamespace/tests/simpledoublemessage.h"
+#include "qtprotobufnamespace/tests/simpleenummessage.h"
+#include "qtprotobufnamespace/tests/simpleenumlistmessage.h"
+#include "qtprotobufnamespace/tests/simplefileenummessage.h"
+#include "qtprotobufnamespace/tests/simpleexternalenummessage.h"
+#include "qtprotobufnamespace1/externaltests/externalcomplexmessage.h"
+#include "qtprotobufnamespace/tests/complexmessage.h"
+#include "qtprotobufnamespace/tests/simplebytesmessage.h"
+#include "qtprotobufnamespace/tests/repeatedintmessage.h"
+#include "qtprotobufnamespace/tests/repeatedbytesmessage.h"
+#include "qtprotobufnamespace/tests/repeateddoublemessage.h"
+#include "qtprotobufnamespace/tests/repeatedcomplexmessage.h"
+#include "qtprotobufnamespace/tests/repeatedfloatmessage.h"
+#include "qtprotobufnamespace/tests/repeatedsintmessage.h"
+#include "qtprotobufnamespace/tests/repeatedstringmessage.h"
+#include "qtprotobufnamespace/tests/repeateduintmessage.h"
+#include "qtprotobufnamespace/tests/repeatedint64message.h"
+#include "qtprotobufnamespace/tests/repeatedsint64message.h"
+#include "qtprotobufnamespace/tests/repeateduint64message.h"
+#include "qtprotobufnamespace/tests/repeatedfixedintmessage.h"
+#include "qtprotobufnamespace/tests/repeatedsfixedintmessage.h"
+#include "qtprotobufnamespace/tests/repeatedfixedint64message.h"
+#include "qtprotobufnamespace/tests/repeatedsfixedint64message.h"
+#include "qtprotobufnamespace/tests/repeatedexternalcomplexmessage.h"
+#include "qtprotobufnamespace/tests/simplesint32stringmapmessage.h"
+#include "qtprotobufnamespace/tests/simplestringstringmapmessage.h"
+#include "qtprotobufnamespace/tests/emptymessage.h"
+#include "qtprotobufnamespace/tests/message_uderscore_name.h"
+#include "qtprotobufnamespace/tests/messageuderscorename.h"
+#include "qtprotobufnamespace/tests/messageunderscorefield.h"
+#include "qtprotobufnamespace/tests/priormessageunderscorefield.h"
+#include "qtprotobufnamespace/tests/followingmessageunderscorefield.h"
+#include "qtprotobufnamespace/tests/combinedmessageunderscorefield.h"
+#include "qtprotobufnamespace/tests/sequence/testmessagesequence.h"
+#include "qtprotobufnamespace/tests/sequence/testmessagesequence2.h"
+#include "qtprotobufnamespace/tests/sequence/cyclingseconddependency.h"
+#include "qtprotobufnamespace/tests/sequence/cyclingfirstdependency.h"
+#include "qtprotobufnamespace/tests/messageuppercasereserved.h"
+#include "qtprotobufnamespace/tests/messagereserved.h"
+#include "qtprotobufnamespace/tests/messageenumreserved.h"
+#include "qtprotobufnamespace/tests/sequence/repeatedfieldsequence.h"
+#include "qtprotobufnamespace/tests/sequence/repeatedfieldsequence2.h"
+#include "qtprotobufnamespace/tests/sequence/mapfieldsequence.h"
+#include "qtprotobufnamespace/tests/sequence/mapfieldsequence2.h"
+#include "qtprotobufnamespace/tests/sequence/lowercasesequence.h"
+#include "qtprotobufnamespace/tests/sequence/lowercasesequence2.h"
+#include "qtprotobufnamespace/tests/lowercasemessagename.h"
+#include "qtprotobufnamespace/tests/lowercasefieldmessagename.h"
+#include "qtprotobufnamespace/tests/messageuppercase.h"
 
 
 #include "globalenums.h"
 #include "globalenums.h"