Browse Source

Add key value reader

- add KVReader class to read a .kv file
- add tests for KVReader class
- add test resources for KVReader tests
- fix KVParser class to respect windows new line
size
Patrick-Christopher Mattulat 3 years ago
parent
commit
821f2f0a58

+ 4 - 2
CMakeLists.txt

@@ -95,7 +95,8 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/serialization/json/event/SerializableJSONEvent.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/io/kv/KVPair.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/io/kv/KVDocument.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/io/kv/KVParser.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/io/kv/KVParser.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/ls_std/io/kv/KVReader.cpp)
 
 if (${LS_STD_BUILD_WITH_TESTS})
     set(TEST_FILES
@@ -153,7 +154,8 @@ if (${LS_STD_BUILD_WITH_TESTS})
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/json/event/SerializableJSONEventTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KVPairTest.cpp
             ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KVDocumentTest.cpp
-            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KVParserTest.cpp)
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KVParserTest.cpp
+            ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/kv/KVReaderTest.cpp)
 endif ()
 
 ##########################################################

+ 46 - 0
include/ls_std/io/kv/KVReader.hpp

@@ -0,0 +1,46 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-12-25
+ * Changed:         2020-12-25
+ *
+ * */
+
+#ifndef LS_STD_KV_READER_HPP
+#define LS_STD_KV_READER_HPP
+
+#include <ls_std/base/Class.hpp>
+#include <ls_std/io/IReader.hpp>
+#include <ls_std/io/kv/KVDocument.hpp>
+#include <ls_std/io/File.hpp>
+#include <memory>
+
+namespace ls_std {
+  class KVReader : public ls_std::Class, public ls_std::IReader {
+    public:
+
+      explicit KVReader(const std::shared_ptr<ls_std::KVDocument>& _document, const std::string& _absolutePath);
+      ~KVReader() override = default;
+
+      // implementation
+
+      ls_std::byte_field read() override;
+
+      // additional functionality
+
+      std::shared_ptr<ls_std::KVDocument> getDocument();
+      void setDocument(const std::shared_ptr<ls_std::KVDocument>& _document);
+      void setFile(const ls_std::File& _kvFile);
+
+    private:
+
+      std::shared_ptr<ls_std::KVDocument> document {};
+      ls_std::File kvFile;
+
+      void _assignDocument(const std::shared_ptr<ls_std::KVDocument>& _document);
+      void _assignFile(ls_std::File _kvFile);
+  };
+}
+
+#endif

+ 1 - 0
include/ls_std/ls_std.hpp

@@ -56,6 +56,7 @@
 #include "io/kv/KVPair.hpp"
 #include "io/kv/KVDocument.hpp"
 #include "io/kv/KVParser.hpp"
+#include "io/kv/KVReader.hpp"
 
 #include "logic/State.hpp"
 #include "logic/StateConnection.hpp"

+ 1 - 1
source/ls_std/io/kv/KVParser.cpp

@@ -92,5 +92,5 @@ void ls_std::KVParser::_readLineWithUnixLineBreak(ls_std::KVParseData& _parseDat
 void ls_std::KVParser::_readLineWithWindowsLineBreak(ls_std::KVParseData &_parseData)
 {
   size_t newLinePosition = _parseData.line.toString().find(ls_std::NewLine::getWindowsNewLine());
-  _parseData.line = _parseData.line.toString().substr(0, newLinePosition);
+  _parseData.line = _parseData.line.toString().substr(0, newLinePosition + 1);
 }

+ 61 - 0
source/ls_std/io/kv/KVReader.cpp

@@ -0,0 +1,61 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-12-25
+ * Changed:         2020-12-25
+ *
+ * */
+
+#include <ls_std/io/kv/KVReader.hpp>
+#include <ls_std/exception/IllegalArgumentException.hpp>
+#include <ls_std/io/FileReader.hpp>
+#include <ls_std/io/kv/KVParser.hpp>
+
+ls_std::KVReader::KVReader(const std::shared_ptr<ls_std::KVDocument> &_document, const std::string &_absolutePath) : ls_std::Class("KVReader"),
+kvFile(ls_std::File {""})
+{
+  this->_assignDocument(_document);
+  this->_assignFile(ls_std::File {_absolutePath});
+}
+
+ls_std::byte_field ls_std::KVReader::read()
+{
+  ls_std::byte_field data = ls_std::FileReader {this->kvFile}.read();
+  ls_std::KVParser {this->document}.parse(data);
+
+  return data;
+}
+
+std::shared_ptr<ls_std::KVDocument> ls_std::KVReader::getDocument()
+{
+  return this->document;
+}
+
+void ls_std::KVReader::setDocument(const std::shared_ptr<ls_std::KVDocument> &_document)
+{
+  this->_assignDocument(_document);
+}
+
+void ls_std::KVReader::setFile(const ls_std::File &_kvFile)
+{
+  this->_assignFile(_kvFile);
+}
+
+void ls_std::KVReader::_assignDocument(const std::shared_ptr<ls_std::KVDocument> &_document)
+{
+  if(_document == nullptr) {
+    throw ls_std::IllegalArgumentException {};
+  }
+
+  this->document = _document;
+}
+
+void ls_std::KVReader::_assignFile(ls_std::File _kvFile)
+{
+  if(!_kvFile.exists()) {
+    throw ls_std::IllegalArgumentException {};
+  }
+
+  this->kvFile = _kvFile;
+}

+ 62 - 0
test/cases/io/kv/KVReaderTest.cpp

@@ -0,0 +1,62 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-12-25
+ * Changed:         2020-12-25
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include <ls_std/ls_std.hpp>
+#include <TestHelper.hpp>
+
+namespace {
+  class KVReaderTest : public ::testing::Test {
+    protected:
+
+      KVReaderTest() = default;
+      ~KVReaderTest() override = default;
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  TEST_F(KVReaderTest, getDocument)
+  {
+    std::string kvPath = TestHelper::getResourcesFolderLocation() + "server_settings.kv";
+    ls_std::KVReader reader {std::make_shared<ls_std::KVDocument>(), kvPath};
+
+    ASSERT_TRUE(reader.getDocument() != nullptr);
+  }
+
+  TEST_F(KVReaderTest, read)
+  {
+    std::string kvPath = TestHelper::getResourcesFolderLocation() + "server_settings.kv";
+    std::shared_ptr<ls_std::KVDocument> document = std::make_shared<ls_std::KVDocument>();
+    ls_std::KVReader reader {document, kvPath};
+
+    reader.read();
+    ASSERT_EQ(3, document->getPairs().size());
+    ASSERT_TRUE(document->hasPair("port"));
+    ASSERT_TRUE(document->hasPair("host"));
+    ASSERT_TRUE(document->hasPair("service-name"));
+
+    ASSERT_STREQ("8080", document->getPairs().at("port").getValue().c_str());
+    ASSERT_STREQ("localhost", document->getPairs().at("host").getValue().c_str());
+    ASSERT_STREQ("deamon", document->getPairs().at("service-name").getValue().c_str());
+  }
+
+  TEST_F(KVReaderTest, setDocument)
+  {
+    std::string kvPath = TestHelper::getResourcesFolderLocation() + "server_settings.kv";
+    std::shared_ptr<ls_std::KVDocument> document1 = std::make_shared<ls_std::KVDocument>();
+    std::shared_ptr<ls_std::KVDocument> document2 = std::make_shared<ls_std::KVDocument>();
+
+    ls_std::KVReader reader {document1, kvPath};
+    ASSERT_TRUE(reader.getDocument() == document1);
+
+    reader.setDocument(document2);
+    ASSERT_TRUE(reader.getDocument() == document2);
+  }
+}

+ 5 - 0
test/resources/server_settings.kv

@@ -0,0 +1,5 @@
+# This is an example key value file for server settings
+
+port=8080;
+host=localhost;
+service-name=deamon;