Sfoglia il codice sorgente

Add conversion of google.protobuf.Timestamp

- Add conversion from google.protobuf.Timestamp to QDateTime and back
- Update tests

Fixes: #85
Alexey Edelev 4 anni fa
parent
commit
617fc65dde

+ 5 - 0
src/generator/messagedeclarationprinter.cpp

@@ -122,6 +122,11 @@ void MessageDeclarationPrinter::printConstructors()
     for (int i = 0; i <= mDescriptor->field_count(); i++) {
         printConstructor(i);
     }
+
+    if (mDescriptor->full_name() == std::string("google.protobuf.Timestamp")) {
+        mPrinter->Print("Timestamp(const QDateTime &datetime, QObject *parent = nullptr);\n"
+                        "operator QDateTime() const;\n");
+    }
 }
 
 void MessageDeclarationPrinter::printConstructor(int fieldCount)

+ 11 - 0
src/generator/messagedefinitionprinter.cpp

@@ -114,6 +114,17 @@ void MessageDefinitionPrinter::printConstructors() {
         printInitializationList(i);
         mPrinter->Print(Templates::ConstructorContentTemplate);
     }
+
+    if (mDescriptor->full_name() == std::string("google.protobuf.Timestamp")) {
+        mPrinter->Print("Timestamp::Timestamp(const QDateTime &datetime, QObject *parent) : QObject(parent)\n"
+                        ", m_seconds(datetime.toMSecsSinceEpoch() / 1000)\n"
+                        ", m_nanos((datetime.toMSecsSinceEpoch() % 1000) * 1000)\n"
+                        "{}\n"
+                        "Timestamp::operator QDateTime() const\n"
+                        "{\n"
+                        "    return QDateTime::fromMSecsSinceEpoch(m_seconds * 1000 + m_nanos / 1000);\n"
+                        "}\n");
+    }
 }
 
 void MessageDefinitionPrinter::printConstructor(int fieldCount)

+ 4 - 1
src/generator/singlefilegenerator.cpp

@@ -98,7 +98,7 @@ bool SingleFileGenerator::GenerateMessages(const ::google::protobuf::FileDescrip
     externalIncludes.insert("QString");
 
     bool hasQtTypes = false;
-    common::iterateMessages(file, [&externalIncludes, &hasQtTypes](const ::google::protobuf::Descriptor *message){
+    common::iterateMessages(file, [&externalIncludes, &hasQtTypes](const ::google::protobuf::Descriptor *message) {
         for (int i = 0; i < message->field_count(); i++) {
             auto field = message->field(i);
             if (field->type() == ::google::protobuf::FieldDescriptor::TYPE_MESSAGE
@@ -108,6 +108,9 @@ bool SingleFileGenerator::GenerateMessages(const ::google::protobuf::FileDescrip
                 hasQtTypes = true;
             }
         }
+        if (message->full_name() == "google.protobuf.Timestamp") {
+            externalIncludes.insert("QDateTime");
+        }
     });
 
     if (hasQtTypes) {

+ 6 - 7
tests/test_wellknowntypes/simpletest.cpp

@@ -270,13 +270,11 @@ TEST_F(WellknowntypesTest, TimestampMessageSerializationTest) {
     qtprotobufnamespace::wellknowntypes::tests::TimestampMessage msg;
 
     QDateTime deserializedDateTime;
-    QDateTime originalDateTime;
+    QDateTime originalDateTime = QDateTime::currentDateTime();
 
-    qint64 secsSinceEpoch = QDateTime::currentSecsSinceEpoch();
-    originalDateTime.setSecsSinceEpoch(secsSinceEpoch);
-    msg.setTestField({secsSinceEpoch, 0, nullptr});
-    ASSERT_EQ(msg.testField().seconds(), secsSinceEpoch);
-    ASSERT_EQ(msg.testField().nanos(), 0);
+    msg.setTestField({originalDateTime, nullptr});
+    ASSERT_EQ(msg.testField().seconds(), originalDateTime.toMSecsSinceEpoch() / 1000);
+    ASSERT_EQ(msg.testField().nanos(), (originalDateTime.toMSecsSinceEpoch() % 1000) * 1000);
 
     QByteArray val = msg.serialize(serializer.get());
     msg.setTestField({0, 0, nullptr});
@@ -285,8 +283,9 @@ TEST_F(WellknowntypesTest, TimestampMessageSerializationTest) {
     ASSERT_EQ(msg.testField().nanos(), 0);
 
     msg.deserialize(serializer.get(), val);
-    deserializedDateTime.setSecsSinceEpoch(msg.testField().seconds());
+    deserializedDateTime = msg.testField();
 
+    ASSERT_EQ(deserializedDateTime.toMSecsSinceEpoch(), originalDateTime.toMSecsSinceEpoch());
     ASSERT_TRUE(deserializedDateTime == originalDateTime);
 }
 }