Browse Source

Add common service generator

- Add common service generator
- Make server generator derived of service generator
- Implement tests
Alexey Edelev 6 years ago
parent
commit
60362885f3

+ 2 - 1
CMakeLists.txt

@@ -16,7 +16,8 @@ add_executable(${PROJECT_NAME} src/generator/main.cpp
     src/generator/classgeneratorbase.cpp
     src/generator/servergenerator.cpp
     src/generator/protobufclassgenerator.cpp
-    src/generator/globalenumsgenerator.cpp)
+    src/generator/globalenumsgenerator.cpp
+    src/generator/servicegeneratorbase.cpp)
 
 if (WIN32)
     #Needs to set path to protobuf libraries

+ 1 - 1
src/generator/generator.cpp

@@ -123,7 +123,7 @@ bool QtGenerator::Generate(const FileDescriptor *file,
         std::string baseFilename(service->name());
         utils::tolower(baseFilename);
 
-        std::string headeFilename = baseFilename + ".h";
+        std::string headeFilename = baseFilename + "server.h";
         ServerGenerator serverGen(service,
                                   std::move(std::unique_ptr<io::ZeroCopyOutputStream>(generatorContext->Open(headeFilename))));
         serverGen.run();

+ 3 - 0
src/generator/protobufclassgenerator.cpp

@@ -143,6 +143,9 @@ void ProtobufClassGenerator::printComparisonOperators()
 void ProtobufClassGenerator::printIncludes(std::set<std::string> listModel)
 {
     assert(mMessage != nullptr);
+
+    mPrinter.Print(DefaultProtobufIncludesTemplate);
+
     PropertyMap properties;
     std::set<std::string> existingIncludes;
     std::string newInclude;

+ 2 - 24
src/generator/servergenerator.cpp

@@ -40,30 +40,8 @@ using namespace ::qtprotobuf::generator;
 using namespace ::google::protobuf;
 using namespace ::google::protobuf::compiler;
 
-//TODO: need to decompose common and non-common logic and fields of ClassGeneratorBase 
 ServerGenerator::ServerGenerator(const ServiceDescriptor *service, std::unique_ptr<io::ZeroCopyOutputStream> out) :
-    ClassGeneratorBase(service->full_name(), std::move(out))
-  , mService(service)
+    ServiceGeneratorBase(service, std::move(out))
 {
+    mClassName += "Server";
 }
-
-void ServerGenerator::printIncludes(const ServiceDescriptor *service)
-{
-    std::unordered_set<std::string> includeSet;
-    for(int i = 0; i < service->method_count(); i++) {
-        const MethodDescriptor* method = service->method(i);
-        std::string inputTypeName = method->input_type()->name();
-        std::string outputTypeName = method->output_type()->name();
-        utils::tolower(inputTypeName);
-        utils::tolower(outputTypeName);
-        includeSet.insert(inputTypeName);
-        includeSet.insert(outputTypeName);
-    }
-
-    for(auto type : includeSet) {
-        mPrinter.Print({{"type_lower", type}}, InternalIncludeTemplate);
-    }
-}
-
-
-

+ 4 - 7
src/generator/servergenerator.h

@@ -25,7 +25,7 @@
 
 #pragma once
 
-#include "classgeneratorbase.h"
+#include "servicegeneratorbase.h"
 #include <string>
 #include <memory>
 #include <google/protobuf/io/printer.h>
@@ -38,7 +38,7 @@ class Message;
 namespace qtprotobuf {
 namespace generator {
 
-class ServerGenerator : public ClassGeneratorBase
+class ServerGenerator : public ServiceGeneratorBase
 {
     const google::protobuf::ServiceDescriptor *mService;
 public:
@@ -47,15 +47,12 @@ public:
 
     void run() {
         printPreamble();
-        printIncludes(mService);
+        printIncludes();
         printNamespaces();
-        mPrinter.Print({{"classname", mClassName}}, NonProtoClassDefinitionTemplate);
+        printClassName();
         encloseClass();
         encloseNamespaces();
     }
-
-private:
-    void printIncludes(const google::protobuf::ServiceDescriptor *service);
 };
 
 } //namespace generator

+ 72 - 0
src/generator/servicegeneratorbase.cpp

@@ -0,0 +1,72 @@
+/*
+ * 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.
+ */
+
+#include "servicegeneratorbase.h"
+
+#include <google/protobuf/stubs/logging.h>
+#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/io/printer.h>
+#include <google/protobuf/io/zero_copy_stream.h>
+#include <google/protobuf/descriptor.h>
+
+#include <unordered_set>
+
+#include "utils.h"
+#include "templates.h"
+
+using namespace ::qtprotobuf::generator;
+using namespace ::google::protobuf;
+using namespace ::google::protobuf::compiler;
+using namespace qtprotobuf::generator;
+
+ServiceGeneratorBase::ServiceGeneratorBase(const ::google::protobuf::ServiceDescriptor* service,
+                                           std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out) :
+    ClassGeneratorBase(service->full_name(), std::move(out))
+  , mService(service)
+{
+}
+
+void ServiceGeneratorBase::printIncludes()
+{
+    std::unordered_set<std::string> includeSet;
+    for(int i = 0; i < mService->method_count(); i++) {
+        const MethodDescriptor* method = mService->method(i);
+        std::string inputTypeName = method->input_type()->name();
+        std::string outputTypeName = method->output_type()->name();
+        utils::tolower(inputTypeName);
+        utils::tolower(outputTypeName);
+        includeSet.insert(inputTypeName);
+        includeSet.insert(outputTypeName);
+    }
+
+    for(auto type : includeSet) {
+        mPrinter.Print({{"type_lower", type}}, InternalIncludeTemplate);
+    }
+}
+
+void ServiceGeneratorBase::printClassName()
+{
+    mPrinter.Print({{"classname", mClassName}}, NonProtoClassDefinitionTemplate);
+}

+ 56 - 0
src/generator/servicegeneratorbase.h

@@ -0,0 +1,56 @@
+/*
+ * 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 <string>
+#include <memory>
+#include <google/protobuf/io/printer.h>
+
+namespace google { namespace protobuf {
+class ServiceDescriptor;
+class Message;
+}}
+
+namespace qtprotobuf {
+namespace generator {
+
+class ServiceGeneratorBase : public ClassGeneratorBase
+{
+protected:
+    const ::google::protobuf::ServiceDescriptor* mService;
+
+public:
+    ServiceGeneratorBase(const ::google::protobuf::ServiceDescriptor* service,
+                         std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out);
+    void run() = 0;
+
+    void printIncludes();
+    void printClassName();
+};
+
+} //namespace generator
+} //namespace qtprotobuf

+ 5 - 3
src/generator/templates.h

@@ -32,11 +32,13 @@
 namespace qtprotobuf {
 namespace generator {
 
+static const char *DefaultProtobufIncludesTemplate = "#include <QMetaType>\n"
+                                                     "#include <protobufobject.h>\n"
+                                                     "#include <unordered_map>\n\n";
+
 static const char *PreambleTemplate = "/* This file is autogenerated. DO NOT CHANGE. All changes will be lost */\n\n"
                                       "#pragma once\n\n"
-                                      "#include <QObject>\n"
-                                      "#include <protobufobject.h>\n"
-                                      "#include <unordered_map>\n\n";
+                                      "#include <QObject>\n";
 
 static const char *InternalIncludeTemplate =  "#include \"$type_lower$.h\"\n";
 static const char *ExternalIncludeTemplate = "#include <$type$>\n";

+ 2 - 1
tests/CMakeLists.txt

@@ -52,7 +52,8 @@ file(GLOB HEADERS ${TESTS_OUT_DIR})
 file(GLOB SOURCES main.cpp
     simpletest.cpp
     serializationtest.cpp
-    deserializationtest.cpp)
+    deserializationtest.cpp
+    servertest.cpp)
 
 file(GLOB PROTOS proto/*.proto)
 

+ 42 - 0
tests/servertest.cpp

@@ -0,0 +1,42 @@
+/*
+ * 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 "servertest.h"
+
+#include "testserviceserver.h"
+
+using namespace qtprotobufnamespace::tests;
+using namespace qtprotobuf::tests;
+
+using namespace qtprotobuf;
+
+ServerTest::ServerTest()
+{
+}
+
+TEST_F(ServerTest, CheckMethodsGeneration)
+{
+    TestServiceServer testServer;
+}

+ 40 - 0
tests/servertest.h

@@ -0,0 +1,40 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <gtest/gtest.h>
+
+namespace qtprotobuf {
+namespace tests {
+
+class ServerTest : public ::testing::Test
+{
+public:
+    ServerTest();
+};
+
+}
+}