Fix policy-value to include stringfied value
authorSangwan Kwon <sangwan.kwon@samsung.com>
Wed, 20 Nov 2019 09:44:21 +0000 (18:44 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Fri, 22 Nov 2019 01:52:17 +0000 (10:52 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/sdk/policy-value.hpp
src/vist/sdk/tests/sdk.cpp

index 75f895c..a8d2003 100644 (file)
 
 #pragma once
 
-#include <vist/archive.hpp>
+#include <vist/stringfy.hpp>
 
 #include <string>
 
 namespace vist {
 namespace policy {
 
-// TODO: Support various value type
 struct PolicyValue final {
-       enum class Type {
-               Integer,
-               String,
-               None
-       };
-
-       explicit PolicyValue(int value) : type(Type::Integer)
-       {
-               this->buffer << value;
-       }
-
-       explicit PolicyValue(const std::string& value) : type(Type::String)
-       {
-               this->buffer << value;
-       }
-
        explicit PolicyValue() noexcept = default;
+       explicit PolicyValue(int value) : stringfied(Stringfy::Dump(value)) {}
+       explicit PolicyValue(const std::string& value) : stringfied(Stringfy::Dump(value)) {}
        ~PolicyValue() = default;
 
        PolicyValue(const PolicyValue&) = default;
@@ -50,32 +35,28 @@ struct PolicyValue final {
        PolicyValue(PolicyValue&&) noexcept = default;
        PolicyValue& operator=(PolicyValue&&) noexcept = default;
 
-       inline Type getType() const noexcept
+       inline std::string dump() const noexcept
        {
-               return this->type;
+               return this->stringfied;
        }
 
-       operator int() const
+       inline Stringfy::Type getType() const
        {
-               auto clone = this->buffer;
-               int out;
-               clone >> out;
+               return Stringfy::GetType(this->stringfied);
+       }
 
-               return out;
+       operator int() const
+       {
+               return Stringfy::Restore(this->stringfied);
        }
 
        operator std::string() const
        {
-               auto clone = this->buffer;
-               std::string out;
-               clone >> out;
-
-               return out;
+               return Stringfy::Restore(this->stringfied);
        }
 
 private:
-       Archive buffer;
-       Type type = Type::None;
+       std::string stringfied;
 };
 
 } // namespace policy
index 2c8942b..9b2ceb7 100644 (file)
@@ -25,6 +25,7 @@ namespace {
        int g_value = -1;
 } // anonymous namespace
 
+using namespace vist;
 using namespace vist::policy;
 
 class TestPolicyModel : public PolicyModel {
@@ -50,15 +51,24 @@ public:
 TEST(PolicySDKTests, policy_value_int)
 {
        PolicyValue value(1);
-       EXPECT_EQ(PolicyValue::Type::Integer, value.getType());
-       EXPECT_EQ((int)value, 1);
+       EXPECT_EQ(Stringfy::Type::Integer, value.getType());
+       EXPECT_EQ(static_cast<int>(value), 1);
 }
 
 TEST(PolicySDKTests, policy_value_string)
 {
-       PolicyValue value(std::string("text"));
-       EXPECT_EQ(PolicyValue::Type::String, value.getType());
-       EXPECT_EQ((std::string)value, "text");
+       PolicyValue value("TEXT");
+       EXPECT_EQ(Stringfy::Type::String, value.getType());
+       EXPECT_EQ(static_cast<std::string>(value), "TEXT");
+}
+
+TEST(PolicySDKTests, policy_value_dump)
+{
+       PolicyValue intValue(1);
+       EXPECT_EQ("I/1", intValue.dump());
+
+       PolicyValue strValue("TEXT");
+       EXPECT_EQ("S/TEXT", strValue.dump());
 }
 
 TEST(PolicySDKTests, policy_model)