Browse Source

Add Q_DECLARE_PROTOBUF_SERIALIZERS macro

- Add Q_DECLARE_PROTOBUF_SERIALIZERS macro to declare protobuf deserializers
- Fix issue in SimpleStringStringMapMessageTest
Alexey Edelev 6 years ago
parent
commit
9329451dcf
3 changed files with 6 additions and 4 deletions
  1. 1 2
      src/generator/templates.cpp
  2. 3 0
      src/protobuf/qprotobufobject.h
  3. 2 2
      tests/simpletest.cpp

+ 1 - 2
src/generator/templates.cpp

@@ -158,8 +158,7 @@ const char *Templates::ClientMethodDefinitionAsyncTemplate = "\nbool $classname$
                                                              "    //TODO: call transport method to serialize this method\n"
                                                              "    return false;\n"
                                                              "}\n";
-const char *Templates::SerializersTemplate = "QByteArray serialize() const { return qtprotobuf::ProtobufObjectPrivate::serialize<$classname$>(this); }\n"
-                                     "void deserialize(const QByteArray &array) { qtprotobuf::ProtobufObjectPrivate::deserialize<$classname$>(this, array); }";
+const char *Templates::SerializersTemplate = "Q_DECLARE_PROTOBUF_SERIALIZERS($classname$)\n";
 const char *Templates::RegisterSerializersTemplate = "qtprotobuf::ProtobufObjectPrivate::registerSerializers<$classname$>();";
 
 const std::unordered_map<::google::protobuf::FieldDescriptor::Type, std::string> Templates::TypeReflection = {

+ 3 - 0
src/protobuf/qprotobufobject.h

@@ -26,3 +26,6 @@
 #pragma once
 
 #include "qprotobufobject_p.h"
+
+#define Q_DECLARE_PROTOBUF_SERIALIZERS(T) QByteArray serialize() const { return qtprotobuf::ProtobufObjectPrivate::serialize<T>(this); } \
+    void deserialize(const QByteArray &array) { qtprotobuf::ProtobufObjectPrivate::deserialize<T>(this, array); }

+ 2 - 2
tests/simpletest.cpp

@@ -651,13 +651,13 @@ TEST_F(SimpleTest, SimpleSInt32StringMapMessageTest)
     ASSERT_STREQ(test.mapField()[44].toStdString().c_str(), "Some 44");
 }
 
-TEST_F(SimpleTest, SimpleStringStringMapMessage)
+TEST_F(SimpleTest, SimpleStringStringMapMessageTest)
 {
     const char* propertyName = "mapField";
     SimpleStringStringMapMessage::registerTypes();
     SimpleStringStringMapMessage test;
     ASSERT_TRUE(QMetaType::isRegistered(qMetaTypeId<SimpleStringStringMapMessage::MapFieldEntry>()));
-    int propertyNumber = SimpleStringStringMapMessage::propertyOrdering.at(1); //See simpletest.proto
+    int propertyNumber = SimpleStringStringMapMessage::propertyOrdering.at(13); //See simpletest.proto
     ASSERT_STREQ(SimpleStringStringMapMessage::staticMetaObject.property(propertyNumber).typeName(), "SimpleStringStringMapMessage::MapFieldEntry");
     ASSERT_EQ(SimpleStringStringMapMessage::staticMetaObject.property(propertyNumber).userType(), qMetaTypeId<SimpleStringStringMapMessage::MapFieldEntry>());
     ASSERT_STREQ(SimpleStringStringMapMessage::staticMetaObject.property(propertyNumber).name(), propertyName);