From 970ef45f0082e31692a85dad764dd4942e5932a3 Mon Sep 17 00:00:00 2001
From: Sergey Yakubov <sergey.yakubov@desy.de>
Date: Thu, 12 Apr 2018 09:40:18 +0200
Subject: [PATCH] refactor jason parser

---
 3d_party/rapidjson/include/rapidjson/reader.h |  6 +-
 common/cpp/include/common/error.h             |  8 +-
 common/cpp/include/common/io_error.h          | 80 +++++++++----------
 common/cpp/include/json_parser/json_parser.h  | 22 ++++-
 common/cpp/src/data_structs/data_structs.cpp  |  4 +-
 common/cpp/src/json_parser/json_parser.cpp    |  1 -
 .../json_parser/test_json_parser.cpp          | 27 ++++---
 .../api/include/producer/producer_error.h     | 20 ++---
 receiver/src/receiver_config.cpp              |  2 +-
 receiver/src/receiver_error.h                 |  8 +-
 .../parse_config_file/parse_config_file.cpp   |  2 +-
 11 files changed, 98 insertions(+), 82 deletions(-)

diff --git a/3d_party/rapidjson/include/rapidjson/reader.h b/3d_party/rapidjson/include/rapidjson/reader.h
index 9b28b2685..206b26141 100644
--- a/3d_party/rapidjson/include/rapidjson/reader.h
+++ b/3d_party/rapidjson/include/rapidjson/reader.h
@@ -1223,7 +1223,8 @@ class GenericReader {
                     }
                     i = i * 10 + static_cast<unsigned>(s.TakePush() - '0');
                     significandDigit++;
-                } else
+                }
+            else
                 while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) {
                     if (RAPIDJSON_UNLIKELY(i >= 429496729)) { // 2^32 - 1 = 4294967295
                         if (RAPIDJSON_LIKELY(i != 429496729 || s.Peek() > '5')) {
@@ -1264,7 +1265,8 @@ class GenericReader {
                         }
                     i64 = i64 * 10 + static_cast<unsigned>(s.TakePush() - '0');
                     significandDigit++;
-                } else
+                }
+            else
                 while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) {
                     if (RAPIDJSON_UNLIKELY(i64 >= RAPIDJSON_UINT64_C2(0x19999999, 0x99999999))) // 2^64 - 1 = 18446744073709551615
                         if (RAPIDJSON_LIKELY(i64 != RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || s.Peek() > '5')) {
diff --git a/common/cpp/include/common/error.h b/common/cpp/include/common/error.h
index 71c32b6e8..e4602afd5 100644
--- a/common/cpp/include/common/error.h
+++ b/common/cpp/include/common/error.h
@@ -146,11 +146,11 @@ inline Error TextErrorWithType(const std::string& error, ErrorType error_type) {
 
 namespace ErrorTemplates {
 auto const kMemoryAllocationError = SimpleErrorTemplate {
-                                        "kMemoryAllocationError", ErrorType::kMemoryAllocationError
-                                    };
+    "kMemoryAllocationError", ErrorType::kMemoryAllocationError
+};
 auto const kEndOfFile = SimpleErrorTemplate {
-                            "End of file", ErrorType::kEndOfFile
-                        };
+    "End of file", ErrorType::kEndOfFile
+};
 
 }
 
diff --git a/common/cpp/include/common/io_error.h b/common/cpp/include/common/io_error.h
index db3606b3b..40c9d4dd7 100644
--- a/common/cpp/include/common/io_error.h
+++ b/common/cpp/include/common/io_error.h
@@ -72,68 +72,68 @@ static inline std::ostream& operator<<(std::ostream& os, const IOErrorTemplate&
 
 namespace IOErrorTemplates {
 auto const kUnknownIOError = IOErrorTemplate {
-                                 "Unknown Error", IOErrorType::kUnknownIOError
-                             };
+    "Unknown Error", IOErrorType::kUnknownIOError
+};
 
 auto const kFileNotFound = IOErrorTemplate {
-                               "No such file or directory", IOErrorType::kFileNotFound
-                           };
+    "No such file or directory", IOErrorType::kFileNotFound
+};
 auto const kReadError = IOErrorTemplate {
-                            "Read error", IOErrorType::kReadError
-                        };
+    "Read error", IOErrorType::kReadError
+};
 auto const kBadFileNumber = IOErrorTemplate {
-                                "Bad file number", IOErrorType::kBadFileNumber
-                            };
+    "Bad file number", IOErrorType::kBadFileNumber
+};
 auto const kResourceTemporarilyUnavailable = IOErrorTemplate {
-                                                 "Resource temporarily unavailable", IOErrorType::kResourceTemporarilyUnavailable
-                                             };
+    "Resource temporarily unavailable", IOErrorType::kResourceTemporarilyUnavailable
+};
 auto const kPermissionDenied = IOErrorTemplate {
-                                   "Permission denied", IOErrorType::kPermissionDenied
-                               };
+    "Permission denied", IOErrorType::kPermissionDenied
+};
 auto const kUnsupportedAddressFamily = IOErrorTemplate {
-                                           "Unsupported address family", IOErrorType::kUnsupportedAddressFamily
-                                       };
+    "Unsupported address family", IOErrorType::kUnsupportedAddressFamily
+};
 auto const kInvalidAddressFormat = IOErrorTemplate {
-                                       "Invalid address format", IOErrorType::kInvalidAddressFormat
-                                   };
+    "Invalid address format", IOErrorType::kInvalidAddressFormat
+};
 auto const kAddressAlreadyInUse = IOErrorTemplate {
-                                      "Address already in use", IOErrorType::kAddressAlreadyInUse
-                                  };
+    "Address already in use", IOErrorType::kAddressAlreadyInUse
+};
 auto const kConnectionRefused = IOErrorTemplate {
-                                    "Connection refused", IOErrorType::kConnectionRefused
-                                };
+    "Connection refused", IOErrorType::kConnectionRefused
+};
 auto const kConnectionResetByPeer = IOErrorTemplate {
-                                        "kConnectionResetByPeer", IOErrorType::kConnectionResetByPeer
-                                    };
+    "kConnectionResetByPeer", IOErrorType::kConnectionResetByPeer
+};
 auto const kTimeout = IOErrorTemplate {
-                          "kTimeout", IOErrorType::kTimeout
-                      };
+    "kTimeout", IOErrorType::kTimeout
+};
 auto const kFileAlreadyExists = IOErrorTemplate {
-                                    "kFileAlreadyExists", IOErrorType::kFileAlreadyExists
-                                };
+    "kFileAlreadyExists", IOErrorType::kFileAlreadyExists
+};
 auto const kNoSpaceLeft = IOErrorTemplate {
-                              "kNoSpaceLeft", IOErrorType::kNoSpaceLeft
-                          };
+    "kNoSpaceLeft", IOErrorType::kNoSpaceLeft
+};
 auto const kSocketOperationOnNonSocket = IOErrorTemplate {
-                                             "kSocketOperationOnNonSocket", IOErrorType::kSocketOperationOnNonSocket
-                                         };
+    "kSocketOperationOnNonSocket", IOErrorType::kSocketOperationOnNonSocket
+};
 auto const kInvalidMemoryAddress = IOErrorTemplate {
-                                       "kInvalidMemoryAddress", IOErrorType::kInvalidMemoryAddress
-                                   };
+    "kInvalidMemoryAddress", IOErrorType::kInvalidMemoryAddress
+};
 auto const kUnableToResolveHostname = IOErrorTemplate {
-                                          "kUnableToResolveHostname", IOErrorType::kUnableToResolveHostname
-                                      };
+    "kUnableToResolveHostname", IOErrorType::kUnableToResolveHostname
+};
 auto const kSocketOperationUnknownAtLevel =  IOErrorTemplate {
-                                                 "kSocketOperationUnknownAtLevel", IOErrorType::kSocketOperationUnknownAtLevel
-                                             };
+    "kSocketOperationUnknownAtLevel", IOErrorType::kSocketOperationUnknownAtLevel
+};
 
 auto const kSocketOperationValueOutOfBound =  IOErrorTemplate {
-                                                  "kSocketOperationValueOutOfBound", IOErrorType::kSocketOperationValueOutOfBound
-                                              };
+    "kSocketOperationValueOutOfBound", IOErrorType::kSocketOperationValueOutOfBound
+};
 
 auto const kAddressNotValid =  IOErrorTemplate {
-                                   "Address not valid", IOErrorType::kAddressNotValid
-                               };
+    "Address not valid", IOErrorType::kAddressNotValid
+};
 
 }
 
diff --git a/common/cpp/include/json_parser/json_parser.h b/common/cpp/include/json_parser/json_parser.h
index 65302b7ef..ab8e49ab7 100644
--- a/common/cpp/include/json_parser/json_parser.h
+++ b/common/cpp/include/json_parser/json_parser.h
@@ -15,15 +15,16 @@ class RapidJson;
 
 class JsonParser {
   public:
-    JsonParser(const std::string& json, const std::unique_ptr<IO>* io); // nullptr as second parameter will use default IO
-    JsonParser(const std::string& json);
-    ~JsonParser();
-    JsonParser(JsonParser&& other);
     Error GetUInt64(const std::string& name, uint64_t* val) const noexcept;
     Error GetString(const std::string& name, std::string* val) const noexcept;
     Error GetArrayUInt64(const std::string& name, std::vector<uint64_t>* val) const noexcept;
     Error GetArrayString(const std::string& name, std::vector<std::string>* val) const noexcept;
     JsonParser Embedded(const std::string& name) const noexcept;
+    ~JsonParser();
+  protected:
+    JsonParser(const std::string& json, const std::unique_ptr<IO>* io); // nullptr as second parameter will use default IO
+    JsonParser(const std::string& json);
+    JsonParser(JsonParser&& other);
   private:
     std::unique_ptr<IO> default_io_;
     std::unique_ptr<RapidJson>  rapid_json_;
@@ -31,6 +32,19 @@ class JsonParser {
 
 };
 
+
+class JsonStringParser : public JsonParser {
+  public:
+    JsonStringParser(const std::string& json): JsonParser(json) {};
+};
+
+
+class JsonFileParser : public JsonParser {
+  public:
+    JsonFileParser(const std::string& json, const std::unique_ptr<IO>* io = nullptr): JsonParser(json, io) {};
+};
+
+
 }
 
 
diff --git a/common/cpp/src/data_structs/data_structs.cpp b/common/cpp/src/data_structs/data_structs.cpp
index 4625c052f..5887f4e69 100644
--- a/common/cpp/src/data_structs/data_structs.cpp
+++ b/common/cpp/src/data_structs/data_structs.cpp
@@ -16,7 +16,7 @@ std::string FileInfo::Json() const {
 }
 
 
-bool TimeFromJson(const JsonParser& parser, const std::string name, std::chrono::system_clock::time_point* val) {
+bool TimeFromJson(const JsonStringParser& parser, const std::string name, std::chrono::system_clock::time_point* val) {
     uint64_t nanoseconds_from_epoch;
     if (parser.GetUInt64(name, &nanoseconds_from_epoch)) {
         return false;
@@ -34,7 +34,7 @@ bool TimeFromJson(const JsonParser& parser, const std::string name, std::chrono:
 bool FileInfo::SetFromJson(const std::string& json_string) {
     auto old = *this;
 
-    JsonParser parser(json_string);
+    JsonStringParser parser(json_string);
 
     if (parser.GetUInt64("_id", &id) ||
             parser.GetUInt64("size", &size) ||
diff --git a/common/cpp/src/json_parser/json_parser.cpp b/common/cpp/src/json_parser/json_parser.cpp
index e0e1c680b..c38f855f0 100644
--- a/common/cpp/src/json_parser/json_parser.cpp
+++ b/common/cpp/src/json_parser/json_parser.cpp
@@ -4,7 +4,6 @@
 
 namespace hidra2 {
 
-
 JsonParser::~JsonParser() {
 
 }
diff --git a/common/cpp/unittests/json_parser/test_json_parser.cpp b/common/cpp/unittests/json_parser/test_json_parser.cpp
index 3f97389e7..5db4292ae 100644
--- a/common/cpp/unittests/json_parser/test_json_parser.cpp
+++ b/common/cpp/unittests/json_parser/test_json_parser.cpp
@@ -20,7 +20,8 @@ using ::testing::SetArgPointee;
 using ::testing::HasSubstr;
 using ::testing::ElementsAre;
 
-using hidra2::JsonParser;
+using hidra2::JsonFileParser;
+using hidra2::JsonStringParser;
 using hidra2::RapidJson;
 using hidra2::MockIO;
 using hidra2::IO;
@@ -31,7 +32,7 @@ namespace {
 TEST(ParseString, SimpleConvertToJson) {
     std::string json = R"({"_id":2,"foo":"foo","bar":1})";
 
-    JsonParser parser{json};
+    JsonStringParser parser{json};
 
     uint64_t id, bar;
     std::string foo;
@@ -52,7 +53,7 @@ TEST(ParseString, SimpleConvertToJson) {
 TEST(ParseString, EmbeddedConvertToJson) {
     std::string json = R"({"id":{"test":2}})";
 
-    JsonParser parser{json};
+    JsonStringParser parser{json};
 
     uint64_t id;
     auto err1 = parser.Embedded("id").GetUInt64("test", &id);
@@ -64,7 +65,7 @@ TEST(ParseString, EmbeddedConvertToJson) {
 TEST(ParseString, DoubleEmbeddedConvertToJson) {
     std::string json = R"({"id":{"test":{"test2":2}}})";
 
-    JsonParser parser{json};
+    JsonStringParser parser{json};
 
     uint64_t id;
     auto err1 = parser.Embedded("id").Embedded("test").GetUInt64("test2", &id);
@@ -76,7 +77,7 @@ TEST(ParseString, DoubleEmbeddedConvertToJson) {
 TEST(ParseString, ErrorOnWrongEmbeddedKey) {
     std::string json = R"({"id1":{"test":2}})";
 
-    JsonParser parser{json};
+    JsonStringParser parser{json};
 
     uint64_t id;
     auto err = parser.Embedded("id").GetUInt64("test", &id);
@@ -88,7 +89,7 @@ TEST(ParseString, ErrorOnWrongEmbeddedKey) {
 TEST(ParseString, ErrorOnWrongEmbeddedSubKey) {
     std::string json = R"({"id1":{"test1":2}})";
 
-    JsonParser parser{json};
+    JsonStringParser parser{json};
 
     uint64_t id;
     auto err = parser.Embedded("id").GetUInt64("test", &id);
@@ -100,7 +101,7 @@ TEST(ParseString, ErrorOnWrongEmbeddedSubKey) {
 TEST(ParseString, ErrorOnWrongKey) {
     std::string json = R"({"_id":"2"})";
 
-    JsonParser parser{json};
+    JsonStringParser parser{json};
 
     uint64_t id;
     auto err = parser.GetUInt64("_id1", &id);
@@ -112,7 +113,7 @@ TEST(ParseString, ErrorOnWrongKey) {
 TEST(ParseString, ErrorOnWrongType) {
     std::string json = R"({"_id":"2"})";
 
-    JsonParser parser{json};
+    JsonStringParser parser{json};
 
     uint64_t id;
     auto err = parser.GetUInt64("_id", &id);
@@ -125,7 +126,7 @@ TEST(ParseString, ErrorOnWrongType) {
 TEST(ParseString, ErrorOnWrongDocument) {
     std::string json = R"({"_id":2)";
 
-    JsonParser parser{json};
+    JsonStringParser parser{json};
 
     uint64_t id;
     auto err = parser.GetUInt64("_id", &id);
@@ -139,7 +140,7 @@ TEST(ParseString, ErrorOnWrongDocument) {
 TEST(ParseString, IntArrayConvertToJson) {
     std::string json = R"({"array":[1,2,3]})";
 
-    JsonParser parser{json};
+    JsonStringParser parser{json};
 
     std::vector<uint64_t> vec;
     auto err = parser.GetArrayUInt64("array", &vec);
@@ -151,7 +152,7 @@ TEST(ParseString, IntArrayConvertToJson) {
 TEST(ParseString, IntArrayErrorConvertToJson) {
     std::string json = R"({"array":[1,2,"3"]})";
 
-    JsonParser parser{json};
+    JsonStringParser parser{json};
 
     std::vector<uint64_t> vec;
     auto err = parser.GetArrayUInt64("array", &vec);
@@ -164,7 +165,7 @@ TEST(ParseString, IntArrayErrorConvertToJson) {
 TEST(ParseString, StringArrayConvertToJson) {
     std::string json = R"({"array":["s1","s2","s3"]})";
 
-    JsonParser parser{json};
+    JsonStringParser parser{json};
 
     std::vector<std::string> vec;
     auto err = parser.GetArrayString("array", &vec);
@@ -179,7 +180,7 @@ class ParseFileTests : public Test {
     std::unique_ptr<IO> io_ptr = std::unique_ptr<IO> {
         &mock_io
     };
-    JsonParser parser{"filename", &io_ptr};
+    JsonFileParser parser{"filename", &io_ptr};
 
     void SetUp() override {
     }
diff --git a/producer/api/include/producer/producer_error.h b/producer/api/include/producer/producer_error.h
index decf91083..cdcf69a74 100644
--- a/producer/api/include/producer/producer_error.h
+++ b/producer/api/include/producer/producer_error.h
@@ -53,23 +53,23 @@ class ProducerErrorTemplate : public SimpleErrorTemplate {
 
 namespace ProducerErrorTemplates {
 auto const kAlreadyConnected = ProducerErrorTemplate {
-                                   "Already connected", ProducerErrorType::kAlreadyConnected
-                               };
+    "Already connected", ProducerErrorType::kAlreadyConnected
+};
 auto const kConnectionNotReady = ProducerErrorTemplate {
-                                     "Connection not ready", ProducerErrorType::kConnectionNotReady
-                                 };
+    "Connection not ready", ProducerErrorType::kConnectionNotReady
+};
 
 auto const kFileTooLarge = ProducerErrorTemplate {
-                               "File too large", ProducerErrorType::kFileTooLarge
-                           };
+    "File too large", ProducerErrorType::kFileTooLarge
+};
 
 auto const kFileIdAlreadyInUse = ProducerErrorTemplate {
-                                     "File already in use", ProducerErrorType::kFileIdAlreadyInUse
-                                 };
+    "File already in use", ProducerErrorType::kFileIdAlreadyInUse
+};
 
 auto const kUnknownServerError = ProducerErrorTemplate {
-                                     "Unknown server error", ProducerErrorType::kUnknownServerError
-                                 };
+    "Unknown server error", ProducerErrorType::kUnknownServerError
+};
 
 
 };
diff --git a/receiver/src/receiver_config.cpp b/receiver/src/receiver_config.cpp
index e6652cb97..d6d115411 100644
--- a/receiver/src/receiver_config.cpp
+++ b/receiver/src/receiver_config.cpp
@@ -12,7 +12,7 @@ ReceiverConfigFactory::ReceiverConfigFactory() : io__{GenerateDefaultIO()} {
 }
 
 Error ReceiverConfigFactory::SetConfigFromFile(std::string file_name) {
-    JsonParser parser(file_name, &io__);
+    JsonFileParser parser(file_name, &io__);
     return parser.GetString("influxdb_uri", &config.influxdb_uri);
 }
 
diff --git a/receiver/src/receiver_error.h b/receiver/src/receiver_error.h
index 63da34f6f..42eb039d8 100644
--- a/receiver/src/receiver_error.h
+++ b/receiver/src/receiver_error.h
@@ -50,11 +50,11 @@ class ReceiverErrorTemplate : public SimpleErrorTemplate {
 
 namespace ReceiverErrorTemplates {
 auto const kInvalidOpCode = ReceiverErrorTemplate {
-                                "Invalid Opcode", ReceiverErrorType::kInvalidOpCode
-                            };
+    "Invalid Opcode", ReceiverErrorType::kInvalidOpCode
+};
 auto const kBadRequest = ReceiverErrorTemplate {
-                             "Bad request", ReceiverErrorType::kBadRequest
-                         };
+    "Bad request", ReceiverErrorType::kBadRequest
+};
 
 };
 }
diff --git a/tests/automatic/json_parser/parse_config_file/parse_config_file.cpp b/tests/automatic/json_parser/parse_config_file/parse_config_file.cpp
index fc8962669..4fb391b75 100644
--- a/tests/automatic/json_parser/parse_config_file/parse_config_file.cpp
+++ b/tests/automatic/json_parser/parse_config_file/parse_config_file.cpp
@@ -40,7 +40,7 @@ void AssertSettings(const Settings& settings) {
 
 Settings Parse(const std::string& fname, Error* err) {
 
-    hidra2::JsonParser parser(fname, nullptr);
+    hidra2::JsonFileParser parser(fname);
 
     Settings settings;
 
-- 
GitLab