Browse Source

Migrate to new enums approach

- Replace GlobalEnums with specific enum gadgets
- Update tests
Alexey Edelev 5 years ago
parent
commit
35cf1f8fa2

+ 4 - 4
src/generator/CMakeLists.txt

@@ -9,8 +9,8 @@ file(GLOB SOURCES main.cpp
     classgeneratorbase.cpp
     servergenerator.cpp
     protobufclassgenerator.cpp
-    globalenumsgenerator.cpp
-    globalenumssourcegenerator.cpp
+    enumsgenerator.cpp
+    enumssourcegenerator.cpp
     servicegeneratorbase.cpp
     templates.cpp
     clientgenerator.cpp
@@ -26,8 +26,8 @@ file(GLOB HEADERS classgeneratorbase.h
     clientgenerator.h
     clientsourcegenerator.h
     generator.h
-    globalenumsgenerator.h
-    globalenumssourcegenerator.h
+    enumsgenerator.h
+    enumssourcegenerator.h
     protobufclassgenerator.h
     protobufsourcegenerator.h
     servergenerator.h

+ 1 - 1
src/generator/classgeneratorbase.cpp

@@ -173,7 +173,7 @@ std::string ClassGeneratorBase::getTypeName(const FieldDescriptor *field, const
             }
         } else if (visibility == GLOBAL_ENUM) {
             namespaceTypeName = getNamespacesList(enumType, typeNamespace, "");
-            typeName = namespaceTypeName.append(Templates::GlobalEnumClassNameTemplate)
+            typeName = namespaceTypeName.append(enumType->name() + Templates::EnumClassSuffix)
                     .append("::").append(enumType->name());
         } else {
             typeName = namespaceTypeName.append(enumType->name());

+ 1 - 1
src/generator/classgeneratorbase.h

@@ -110,7 +110,7 @@ public:
 
         for (int i = 0; i < message->enum_type_count(); i++) {
             const auto enumDescr = message->enum_type(i);
-            mPrinter->Print({{"enum", enumDescr->name()}}, Templates::EnumTypeUsingTemplate);
+            mPrinter->Print({{"enum", enumDescr->name()}}, Templates::EnumTypeRepeatedTemplate);
         }
         Outdent();
     }

+ 110 - 0
src/generator/enumsgenerator.cpp

@@ -0,0 +1,110 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
+ *
+ * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this
+ * software and associated documentation files (the "Software"), to deal in the Software
+ * without restriction, including without limitation the rights to use, copy, modify,
+ * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
+ * to permit persons to whom the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies
+ * or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+ * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
+ * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "enumsgenerator.h"
+
+#include <google/protobuf/io/zero_copy_stream.h>
+
+using namespace ::QtProtobuf::generator;
+using namespace ::google::protobuf;
+using namespace ::google::protobuf::io;
+using namespace ::google::protobuf::compiler;
+
+EnumsGenerator::EnumsGenerator(const EnumDescriptor *enumDesctiptor, const std::shared_ptr<io::ZeroCopyOutputStream> &out) :
+    ClassGeneratorBase(enumDesctiptor->full_name() + Templates::EnumClassSuffix, out)
+  , mEnumDescriptor(enumDesctiptor)
+{}
+
+
+EnumsGenerator::EnumsGenerator(const EnumDescriptor *enumDesctiptor, const std::shared_ptr<::google::protobuf::io::Printer> &printer) :
+    ClassGeneratorBase(enumDesctiptor->full_name() + Templates::EnumClassSuffix, printer)
+  , mEnumDescriptor(enumDesctiptor)
+{}
+
+void EnumsGenerator::startEnum()
+{
+    printNamespaces(mNamespaces);
+    printEnumClass();
+    printPublic();
+    Indent();
+    mPrinter->Print({{"classname", mClassName}}, Templates::ManualRegistrationDeclaration);
+    Outdent();
+    printPrivate();
+    printConstructor();
+}
+
+void EnumsGenerator::printEnum()
+{
+    printPublic();
+
+    Indent();
+    mPrinter->Print({{"enum", mEnumDescriptor->name()}}, Templates::EnumDefinitionTemplate);
+
+    Indent();
+    for (int j = 0; j < mEnumDescriptor->value_count(); j++) {
+        const auto valueDescr = mEnumDescriptor->value(j);
+        mPrinter->Print({{"enumvalue", utils::upperCaseName(valueDescr->name())},
+                         {"value", std::to_string(valueDescr->number())}}, Templates::EnumFieldTemplate);
+    }
+    Outdent();
+    mPrinter->Print(Templates::SemicolonBlockEnclosureTemplate);
+    mPrinter->Print({{"type", mEnumDescriptor->name()}}, Templates::QEnumTemplate);
+    mPrinter->Print({{"enum", mEnumDescriptor->name()}}, Templates::EnumTypeRepeatedTemplate);
+    Outdent();
+}
+
+void EnumsGenerator::printConstructor()
+{
+    Indent();
+    mPrinter->Print({{"classname", mClassName}}, Templates::ConstructorHeaderTemplate);
+    mPrinter->Print({{"classname", mClassName}}, Templates::DeletedCopyConstructorTemplate);
+    mPrinter->Print({{"classname", mClassName}}, Templates::DeletedMoveConstructorTemplate);
+    Outdent();
+}
+
+void EnumsGenerator::run()
+{
+    startEnum();
+    printEnum();
+    encloseEnum();
+    printMetatype();
+}
+
+void EnumsGenerator::printMetatype()
+{
+    mPrinter->Print({{"classname", mEnumDescriptor->name()}, {"namespaces", mNamespacesColonDelimited + "::" + mClassName}},
+                   Templates::DeclareMetaTypeListTemplate);
+}
+
+void EnumsGenerator::encloseEnum()
+{
+    encloseClass();
+    encloseNamespaces();
+}
+
+void EnumsGenerator::printEnumClass()
+{
+    mPrinter->Print({{"classname", mClassName}}, Templates::NonProtoClassDefinitionTemplate);
+}

+ 58 - 0
src/generator/enumsgenerator.h

@@ -0,0 +1,58 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>, Tatyana Borisova <tanusshhka@mail.ru>
+ *
+ * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this
+ * software and associated documentation files (the "Software"), to deal in the Software
+ * without restriction, including without limitation the rights to use, copy, modify,
+ * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
+ * to permit persons to whom the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies
+ * or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+ * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
+ * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#pragma once
+
+#include "classgeneratorbase.h"
+#include "utils.h"
+
+namespace google {
+namespace protobuf {
+class EnumDescriptor;
+}}
+namespace QtProtobuf {
+namespace generator {
+
+class EnumsGenerator : public ClassGeneratorBase
+{
+    const google::protobuf::EnumDescriptor *mEnumDescriptor;
+public:
+    EnumsGenerator(const google::protobuf::EnumDescriptor *enumDesctiptor, const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out);
+    EnumsGenerator(const google::protobuf::EnumDescriptor *enumDesctiptor, const std::shared_ptr<::google::protobuf::io::Printer> &printer);
+    virtual ~EnumsGenerator() = default;
+
+    void run();
+
+    void startEnum();
+    void printEnum();
+    void encloseEnum();
+    void printMetatype();
+    void printEnumClass();
+    void printConstructor();
+};
+
+} //namespace generator
+} //namespace QtProtobuf
+

+ 92 - 0
src/generator/enumssourcegenerator.cpp

@@ -0,0 +1,92 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) Tatyana Borisova <tanusshhka@mail.ru>
+ *
+ * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this
+ * software and associated documentation files (the "Software"), to deal in the Software
+ * without restriction, including without limitation the rights to use, copy, modify,
+ * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
+ * to permit persons to whom the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies
+ * or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+ * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
+ * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "enumssourcegenerator.h"
+
+#include "generatoroptions.h"
+
+#include <google/protobuf/io/zero_copy_stream.h>
+
+using namespace ::QtProtobuf::generator;
+using namespace ::google::protobuf;
+using namespace ::google::protobuf::io;
+using namespace ::google::protobuf::compiler;
+
+EnumsSourceGenerator::EnumsSourceGenerator(const google::protobuf::EnumDescriptor *enumDesctiptor, const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out) :
+    ClassGeneratorBase(enumDesctiptor->full_name() + Templates::EnumClassSuffix, out)
+  , mEnumDescriptor(enumDesctiptor) {}
+
+
+EnumsSourceGenerator::EnumsSourceGenerator(const google::protobuf::EnumDescriptor *enumDesctiptor, const std::shared_ptr<::google::protobuf::io::Printer> &printer) :
+    ClassGeneratorBase(enumDesctiptor->full_name() + Templates::EnumClassSuffix, printer)
+  , mEnumDescriptor(enumDesctiptor) {}
+
+
+void  EnumsSourceGenerator::printHeaders() {
+    mPrinter->Print("#include \"globalenums.h\"\n\n"
+                    "#include <QProtobufObject>\n\n");
+    if (GeneratorOptions::instance().hasQml()) {
+        mPrinter->Print("#include <QQmlEngine>");
+    }
+}
+
+void EnumsSourceGenerator::run() {
+    printNamespaces(mNamespaces);
+    printRegisterBody();
+    encloseNamespaces();
+}
+
+void EnumsSourceGenerator::printRegisterBody()
+{
+    std::string packageName;
+    for (auto name : mNamespaces) {
+        if (packageName.empty()) {
+            packageName.append(name);
+            continue;
+        }
+        packageName.append(".").append(name);
+    }
+
+    const std::map<std::string, std::string> registrationProperties = {{"classname", mClassName},
+                                                                       {"type", mClassName},
+                                                                       {"namespaces", mNamespacesColonDelimited},
+                                                                       {"package", packageName}};
+
+    mPrinter->Print(registrationProperties, Templates::ManualRegistrationGlobalEnumDefinition);
+    Indent();
+    if (GeneratorOptions::instance().hasQml()) {
+        mPrinter->Print(registrationProperties, Templates::QmlRegisterTypeUncreatableTemplate);
+    }
+
+    const std::map<std::string, std::string> properties = {{"classname", mClassName},
+                                                           {"type", mClassName + "::" + mEnumDescriptor->name()},
+                                                           {"enum", mEnumDescriptor->name() + Templates::ListSuffix},
+                                                           {"namespaces", mNamespacesColonDelimited}};
+    mPrinter->Print(properties, Templates::ComplexGlobalEnumFieldRegistrationTemplate);
+    mPrinter->Print(properties, Templates::RegisterMetaTypeTemplate);
+    mPrinter->Print(properties, Templates::RegisterEnumSerializersTemplate);
+    Outdent();
+    mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
+}

+ 49 - 0
src/generator/enumssourcegenerator.h

@@ -0,0 +1,49 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2019 Tatyana Borisova <tanusshhka@mail.ru>
+ *
+ * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this
+ * software and associated documentation files (the "Software"), to deal in the Software
+ * without restriction, including without limitation the rights to use, copy, modify,
+ * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
+ * to permit persons to whom the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies
+ * or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+ * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
+ * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#pragma once
+
+#include "classgeneratorbase.h"
+#include "utils.h"
+
+namespace QtProtobuf {
+namespace generator {
+
+class EnumsSourceGenerator : public ClassGeneratorBase
+{
+    const google::protobuf::EnumDescriptor *mEnumDescriptor;
+public:
+    EnumsSourceGenerator(const google::protobuf::EnumDescriptor *enumDesctiptor, const std::shared_ptr<google::protobuf::io::ZeroCopyOutputStream> &out);
+    EnumsSourceGenerator(const google::protobuf::EnumDescriptor *enumDesctiptor, const std::shared_ptr<::google::protobuf::io::Printer> &printer);
+    virtual ~EnumsSourceGenerator() = default;
+
+    void run() override;
+    void printHeaders();
+    void printRegisterBody();
+};
+
+} //namespace generator
+} //namespace QtProtobuf
+

+ 31 - 13
src/generator/generator.cpp

@@ -28,8 +28,8 @@
 #include "classgeneratorbase.h"
 #include "protobufclassgenerator.h"
 #include "protobufsourcegenerator.h"
-#include "globalenumsgenerator.h"
-#include "globalenumssourcegenerator.h"
+#include "enumsgenerator.h"
+#include "enumssourcegenerator.h"
 #include "servergenerator.h"
 #include "clientgenerator.h"
 #include "clientsourcegenerator.h"
@@ -122,22 +122,40 @@ bool QtGenerator::GenerateAll(const std::vector<const FileDescriptor *> &files,
 {
     std::string globalEnumsFilename = "globalenums";
 
+    std::shared_ptr<io::ZeroCopyOutputStream> outHeader(generatorContext->Open(globalEnumsFilename + ".h"));
+    std::shared_ptr<io::ZeroCopyOutputStream> outSource(generatorContext->Open(globalEnumsFilename + ".cpp"));
+    std::shared_ptr<::google::protobuf::io::Printer> outHeaderPrinter(new ::google::protobuf::io::Printer(outHeader.get(), '$'));
+    std::shared_ptr<::google::protobuf::io::Printer> outSourcePrinter(new ::google::protobuf::io::Printer(outSource.get(), '$'));
+
     PackagesList packageList;
     for (auto file : files) {
         packageList[file->package()].push_back(file);
     }
 
-    GlobalEnumsGenerator enumGen(packageList,
-                                 std::shared_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(globalEnumsFilename + ".h")));
-    enumGen.printDisclaimer();
-    enumGen.printPreamble();
-    enumGen.run();
-
-    GlobalEnumsSourceGenerator enumSourceGen(packageList,
-                                             std::shared_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(globalEnumsFilename + ".cpp")));
-    enumSourceGen.printDisclaimer();
-    enumSourceGen.printHeaders();
-    enumSourceGen.run();
+    outHeaderPrinter->Print(Templates::DisclaimerTemplate);
+    outHeaderPrinter->Print(Templates::PreambleTemplate);
+    outHeaderPrinter->Print(Templates::DefaultProtobufIncludesTemplate);
+
+    outSourcePrinter->Print({{"include", globalEnumsFilename}}, Templates::InternalIncludeTemplate);
+    outSourcePrinter->Print(Templates::DisclaimerTemplate);
+
+    for (auto file : files) { //TODO: Each should be printed to separate file
+        for(int i = 0; i < file->enum_type_count(); i++) {
+            auto enumType = file->enum_type(i);
+            EnumsGenerator enumGen2(enumType,
+                                    outHeaderPrinter);
+            enumGen2.run();
+        }
+    }
+
+    for (auto file : files) { //TODO: Each should be printed to separate file
+        for(int i = 0; i < file->enum_type_count(); i++) {
+            auto enumType = file->enum_type(i);
+            EnumsSourceGenerator enumSourceGen2(enumType,
+                                    outSourcePrinter);
+            enumSourceGen2.run();
+        }
+    }
 
     return GeneratorBase::GenerateAll(files, parameter, generatorContext, error);
 }

+ 4 - 3
src/generator/generatorbase.cpp

@@ -87,7 +87,7 @@ bool GeneratorBase::GenerateAll(const std::vector<const FileDescriptor *> &files
     bool addGlobalEnumsHeader = false;
     for (auto file : files) {
         if (m_mode == SingleMode) {
-            if (file->message_type_count() > 0) {
+            if (file->message_type_count() > 0 || file->enum_type_count() > 0) {
                 outfHeaderPrinter->Print({{"include", utils::extractFileName(file->name()) + Templates::ProtoFileSuffix}}, Templates::InternalIncludeTemplate);
             }
         } else {
@@ -120,8 +120,9 @@ bool GeneratorBase::GenerateAll(const std::vector<const FileDescriptor *> &files
                 outfHeaderPrinter->Print({{"classname", utils::upperCaseName(message->name())}}, "qRegisterProtobufType<$classname$>();\n");
             });
 
-            if (file->enum_type_count() > 0) {
-                outfHeaderPrinter->Print("GlobalEnums::registerTypes();\n");
+            for (int i = 0; i < file->enum_type_count(); i++) {
+                std::string enumRegistator = file->enum_type(i)->name() + Templates::EnumClassSuffix + "::registerTypes();\n";
+                outfHeaderPrinter->Print(enumRegistator.c_str());
             }
         }
         outfHeaderPrinter->Outdent();

+ 14 - 11
src/generator/singlefilegenerator.cpp

@@ -28,8 +28,8 @@
 #include "classgeneratorbase.h"
 #include "protobufclassgenerator.h"
 #include "protobufsourcegenerator.h"
-#include "globalenumsgenerator.h"
-#include "globalenumssourcegenerator.h"
+#include "enumsgenerator.h"
+#include "enumssourcegenerator.h"
 #include "servergenerator.h"
 #include "clientgenerator.h"
 #include "clientsourcegenerator.h"
@@ -69,7 +69,7 @@ bool SingleFileGenerator::GenerateMessages(const ::google::protobuf::FileDescrip
                                            const std::string &,
                                            ::google::protobuf::compiler::GeneratorContext *generatorContext,
                                            std::string *) const {
-    if (file->message_type_count() <= 0) {
+    if (file->message_type_count() <= 0 && file->enum_type_count() <= 0) {
         return true;
     }
 
@@ -113,14 +113,18 @@ bool SingleFileGenerator::GenerateMessages(const ::google::protobuf::FileDescrip
     PackagesList packageList;
     packageList[file->package()].push_back(file);
 
-    GlobalEnumsGenerator enumGen(packageList,
-                                 outHeaderPrinter);
-    enumGen.run();
-
-    GlobalEnumsSourceGenerator enumSourceGen(packageList,
-                                             outSourcePrinter);
-    enumSourceGen.run();
+    for(int i = 0; i < file->enum_type_count(); i++) {
+        EnumsGenerator enumGen2(file->enum_type(i),
+                                outHeaderPrinter);
+        enumGen2.run();
+    }
 
+    for(int i = 0; i < file->enum_type_count(); i++) {
+        auto enumType = file->enum_type(i);
+        EnumsSourceGenerator enumSourceGen2(enumType,
+                                outSourcePrinter);
+        enumSourceGen2.run();
+    }
 
     std::vector<std::string> namespaces;
     std::string namespacesColonDelimited;
@@ -222,7 +226,6 @@ bool SingleFileGenerator::GenerateServices(const ::google::protobuf::FileDescrip
         }
     }
 
-
     externalIncludes.insert("QAbstractGrpcClient");
     externalIncludes.insert("QGrpcAsyncReply");
 

+ 3 - 1
src/generator/templates.cpp

@@ -69,7 +69,7 @@ const char *Templates::ComplexListTypeUsingTemplate = "using $classname$Repeated
 const char *Templates::MapTypeUsingTemplate = "using $classname$ = QMap<$key$, $value$>;\n";
 const char *Templates::MessageMapTypeUsingTemplate = "using $classname$ = QMap<$key$, QSharedPointer<$value$>>;\n";
 
-const char *Templates::EnumTypeUsingTemplate = "using $enum$Repeated = QList<$enum$>;\n";
+const char *Templates::EnumTypeRepeatedTemplate = "using $enum$Repeated = QList<$enum$>;\n";
 
 const char *Templates::NamespaceTemplate = "\nnamespace $namespace$ {\n";
 const char *Templates::UsingNamespaceTemplate = "using namespace $namespace$;\n";
@@ -301,5 +301,7 @@ const std::unordered_map<::google::protobuf::FieldDescriptor::Type, std::string>
 const char *Templates::ProtoFileSuffix = ".qpb";
 const char *Templates::GrpcFileSuffix = "_grpc";
 
+const char *Templates::EnumClassSuffix = "Gadget";
+
 const std::string Templates::GlobalDeclarationsFilename = std::string("qtprotobuf_global");
 

+ 2 - 1
src/generator/templates.h

@@ -52,7 +52,7 @@ public:
     static const char *ComplexListTypeUsingTemplate;
     static const char *MapTypeUsingTemplate;
     static const char *MessageMapTypeUsingTemplate;
-    static const char *EnumTypeUsingTemplate;
+    static const char *EnumTypeRepeatedTemplate;
     static const char *NamespaceTemplate;
     static const char *UsingNamespaceTemplate;
     static const char *NonProtoClassDefinitionTemplate;
@@ -166,6 +166,7 @@ public:
     static const char *ListSuffix;
     static const char *ProtoFileSuffix;
     static const char *GrpcFileSuffix;
+    static const char *EnumClassSuffix;
 
     static const std::unordered_map<::google::protobuf::FieldDescriptor::Type, std::string> TypeReflection;
     static const std::string GlobalDeclarationsFilename;

+ 11 - 1
src/protobuf/parsemessages.go

@@ -33,7 +33,7 @@ func main() {
 		log.Fatalf("Invalid regexp %s\n", err)
 	}
 	
-    serviceFinder, err := regexp.Compile("^service\\s+([a-zA-Z0-9_]+)")
+	serviceFinder, err := regexp.Compile("^service\\s+([a-zA-Z0-9_]+)")
 	if err != nil {
 		log.Fatalf("Invalid regexp %s\n", err)
 	}
@@ -53,6 +53,11 @@ func main() {
 		}
 	} else {
 		//Singlefile version
+		enumFinder, err := regexp.Compile("^enum\\s+([a-zA-Z0-9_]+)")
+		if err != nil {
+			log.Fatalf("Invalid regexp %s\n", err)
+		}
+
 		basename := strings.TrimSuffix(os.Args[1], filepath.Ext(os.Args[1]))
 		messageFound := false
 		serviceFound := false
@@ -62,6 +67,11 @@ func main() {
 				messageFound = true
 			}
 
+			capture = enumFinder.FindStringSubmatch(scanner.Text())
+			if len(capture) == 2 {
+				messageFound = true
+			}
+
 			capture = serviceFinder.FindStringSubmatch(scanner.Text())
 			if len(capture) == 2 {
 				serviceFound = true

+ 15 - 15
tests/test_protobuf/simpletest.cpp.inc

@@ -271,10 +271,10 @@ TEST_F(SimpleTest, SimpleLocalEnumListTest)
 
 TEST_F(SimpleTest, SimpleExternalEnumMessageTest)
 {
-    using ExternalGlobalEnums = qtprotobufnamespace1::externaltests::GlobalEnums;
+    using ExternalGlobalEnums = qtprotobufnamespace1::externaltests::ExternalTestEnumGadget;
 
     const char *propertyName = "externalEnum";
-    assertMessagePropertyRegistered<SimpleExternalEnumMessage, ExternalGlobalEnums::ExternalTestEnum>(1, "qtprotobufnamespace1::externaltests::GlobalEnums::ExternalTestEnum", propertyName);
+    assertMessagePropertyRegistered<SimpleExternalEnumMessage, ExternalGlobalEnums::ExternalTestEnum>(1, "qtprotobufnamespace1::externaltests::ExternalTestEnumGadget::ExternalTestEnum", propertyName);
 
     SimpleExternalEnumMessage test;
     ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<ExternalGlobalEnums::ExternalTestEnum>(ExternalGlobalEnums::EXTERNAL_TEST_ENUM_VALUE4)));
@@ -284,10 +284,10 @@ TEST_F(SimpleTest, SimpleExternalEnumMessageTest)
 
 TEST_F(SimpleTest, SimpleEnumsTest)
 {
-    EXPECT_GT(GlobalEnums::staticMetaObject.enumeratorCount(), 0);
+    EXPECT_GT(TestEnumGadget::staticMetaObject.enumeratorCount(), 0);
     QMetaEnum testEnum;
-    for (int i = 0; i < GlobalEnums::staticMetaObject.enumeratorCount(); i++) {
-        QMetaEnum tmp = GlobalEnums::staticMetaObject.enumerator(i);
+    for (int i = 0; i < TestEnumGadget::staticMetaObject.enumeratorCount(); i++) {
+        QMetaEnum tmp = TestEnumGadget::staticMetaObject.enumerator(i);
         if (QString(tmp.name()) == QString("TestEnum")) {
             testEnum = tmp;
             break;
@@ -306,8 +306,8 @@ TEST_F(SimpleTest, SimpleEnumsTest)
     ASSERT_EQ(testEnum.value(3), 4);
     ASSERT_EQ(testEnum.value(4), 3);
 
-    for (int i = 0; i < GlobalEnums::staticMetaObject.enumeratorCount(); i++) {
-        QMetaEnum tmp = GlobalEnums::staticMetaObject.enumerator(i);
+    for (int i = 0; i < TestEnumSecondInFileGadget::staticMetaObject.enumeratorCount(); i++) {
+        QMetaEnum tmp = TestEnumSecondInFileGadget::staticMetaObject.enumerator(i);
         if (QString(tmp.name()) == QString("TestEnumSecondInFile")) {
             testEnum = tmp;
             break;
@@ -327,16 +327,16 @@ TEST_F(SimpleTest, SimpleEnumsTest)
 TEST_F(SimpleTest, SimpleFileEnumsTest)
 {
     const char *propertyName = "globalEnumList";
-    assertMessagePropertyRegistered<SimpleFileEnumMessage, GlobalEnums::TestEnumRepeated>(2, "qtprotobufnamespace::tests::GlobalEnums::TestEnumRepeated", propertyName);
+    assertMessagePropertyRegistered<SimpleFileEnumMessage, TestEnumGadget::TestEnumRepeated>(2, "qtprotobufnamespace::tests::TestEnumGadget::TestEnumRepeated", propertyName);
 
-    GlobalEnums::TestEnumRepeated value({GlobalEnums::TEST_ENUM_VALUE1,
-                                     GlobalEnums::TEST_ENUM_VALUE3,
-                                     GlobalEnums::TEST_ENUM_VALUE4,
-                                     GlobalEnums::TEST_ENUM_VALUE2,
-                                     GlobalEnums::TEST_ENUM_VALUE1});
+    TestEnumGadget::TestEnumRepeated value{TestEnumGadget::TEST_ENUM_VALUE1,
+                                     TestEnumGadget::TEST_ENUM_VALUE3,
+                                     TestEnumGadget::TEST_ENUM_VALUE4,
+                                     TestEnumGadget::TEST_ENUM_VALUE2,
+                                     TestEnumGadget::TEST_ENUM_VALUE1};
     SimpleFileEnumMessage test;
-    ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<GlobalEnums::TestEnumRepeated>(value)));
-    ASSERT_TRUE(test.property(propertyName).value<GlobalEnums::TestEnumRepeated>() == value);
+    ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<TestEnumGadget::TestEnumRepeated>(value)));
+    ASSERT_TRUE(test.property(propertyName).value<TestEnumGadget::TestEnumRepeated>() == value);
     ASSERT_TRUE(test.globalEnumList() == value);
 }