Browse Source

Add section pair document header member

Patrick-Christopher Mattulat 2 years ago
parent
commit
34a498b0ce

+ 3 - 1
include/ls-std/io/section-pair/model/SectionPairDocument.hpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-15
-* Changed:         2023-02-15
+* Changed:         2023-02-16
 *
 * */
 
@@ -27,10 +27,12 @@ namespace ls::std::io
       void add(const section_pair_document_section_list_element &_section);
       [[nodiscard]] section_pair_document_section_list_element get(size_t _index);
       [[nodiscard]] size_t getAmountOfSections();
+      [[nodiscard]] ::std::string getHeader();
       [[nodiscard]] section_pair_document_section_list getSectionList();
 
     private:
 
+      const ::std::string header = "# section-pair document";
       ls::std::io::section_pair_document_section_list sections{};
 
       void _checkSectionExistence(const ls::std::io::section_pair_identifier &_sectionId);

+ 6 - 1
source/ls-std/io/section-pair/model/SectionPairDocument.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-15
-* Changed:         2023-02-15
+* Changed:         2023-02-16
 *
 * */
 
@@ -50,6 +50,11 @@ size_t ls::std::io::SectionPairDocument::getAmountOfSections()
   return this->sections.size();
 }
 
+::std::string ls::std::io::SectionPairDocument::getHeader()
+{
+  return this->header;
+}
+
 ls::std::io::section_pair_document_section_list ls::std::io::SectionPairDocument::getSectionList()
 {
   return this->sections;

+ 6 - 2
source/ls-std/io/section-pair/serialization/SerializableSectionPairDocument.cpp

@@ -26,9 +26,11 @@ ls::std::io::SerializableSectionPairDocument::~SerializableSectionPairDocument()
 
 ls::std::core::type::byte_field ls::std::io::SerializableSectionPairDocument::marshal()
 {
-  ls::std::core::type::byte_field serializedDocument{};
+  ::std::shared_ptr<ls::std::io::SectionPairDocument> document = ::std::dynamic_pointer_cast<ls::std::io::SectionPairDocument>(this->value);
+  ::std::string newLine = ls::std::io::NewLine::get();
+  ls::std::core::type::byte_field serializedDocument = document->getHeader() + newLine + newLine;
 
-  for (const auto &_section : ::std::dynamic_pointer_cast<ls::std::io::SectionPairDocument>(this->value)->getSectionList())
+  for (const auto &_section : document->getSectionList())
   {
     serializedDocument += _section->marshal();
   }
@@ -39,6 +41,8 @@ ls::std::core::type::byte_field ls::std::io::SerializableSectionPairDocument::ma
 void ls::std::io::SerializableSectionPairDocument::unmarshal(const ls::std::core::type::byte_field &_data)
 {
   ls::std::core::type::byte_field serializedDocument = _data;
+  size_t headerSize = ::std::dynamic_pointer_cast<ls::std::io::SectionPairDocument>(this->value)->getHeader().size() + 2 * ls::std::io::NewLine::get().size();
+  serializedDocument = serializedDocument.substr(headerSize);
   ls::std::core::type::byte_field serializedSection{};
 
   do

+ 7 - 1
test/cases/io/section-pair/model/SectionPairDocumentTest.cpp

@@ -3,7 +3,7 @@
 * Company:         Lynar Studios
 * E-Mail:          webmaster@lynarstudios.com
 * Created:         2023-02-15
-* Changed:         2023-02-15
+* Changed:         2023-02-16
 *
 * */
 
@@ -95,6 +95,12 @@ namespace
     ASSERT_EQ(1, document->getAmountOfSections());
   }
 
+  TEST_F(SectionPairDocumentTest, getHeader)
+  {
+    shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();
+    ASSERT_STREQ("# section-pair document", document->getHeader().c_str());
+  }
+
   TEST_F(SectionPairDocumentTest, getSectionList)
   {
     shared_ptr<SectionPairDocument> document = make_shared<SectionPairDocument>();

+ 1 - 1
test/classes/io/section-pair/SectionPairDocumentProvider.cpp

@@ -47,8 +47,8 @@ test::io::SectionPairDocumentProvider::~SectionPairDocumentProvider() = default;
 
 ls::std::core::type::byte_field test::io::SectionPairDocumentProvider::createSerializedDocument()
 {
-  ls::std::core::type::byte_field serializedDocument{};
   ::std::string newLine = ls::std::io::NewLine::get();
+  ls::std::core::type::byte_field serializedDocument = "# section-pair document" + newLine + newLine;
 
   // general section