Browse Source

Merge pull request #354 from holmes1412/master

json: update Json in tools
xiehan 6 months ago
parent
commit
eeb280c88d
4 changed files with 94 additions and 21 deletions
  1. 1 1
      WORKSPACE
  2. 64 17
      tools/templates/config/Json.cc
  3. 28 2
      tools/templates/config/Json.h
  4. 1 1
      workflow

+ 1 - 1
WORKSPACE

@@ -18,7 +18,7 @@ rules_proto_toolchains()
 
 git_repository(
     name = "workflow",
-    commit = "273134f0ffadbb40a59ce9155a1fde299cd9d68b",
+    commit = "ba98ed0945f8888f73350ddfe5d9681306103702",
     remote = "https://github.com/sogou/workflow.git")
 
 new_git_repository(

+ 64 - 17
tools/templates/config/Json.cc

@@ -75,23 +75,22 @@ Json::Json(std::nullptr_t null)
 {
 }
 
-Json::Json(double val)
-    : node_(json_value_create(JSON_VALUE_NUMBER, val)), parent_(nullptr),
-      allocated_(true)
-{
-}
-
-Json::Json(int val)
-    : node_(json_value_create(JSON_VALUE_NUMBER, static_cast<double>(val))),
+Json::Json(bool val)
+    : node_(val ? json_value_create(JSON_VALUE_TRUE)
+                : json_value_create(JSON_VALUE_FALSE)),
       parent_(nullptr), allocated_(true)
 {
 }
 
-Json::Json(bool val)
-    : node_(val ? json_value_create(JSON_VALUE_TRUE)
-                : json_value_create(JSON_VALUE_FALSE)),
+Json::Json(const std::vector<std::string>& val)
+    : node_(json_value_create(JSON_VALUE_ARRAY)),
       parent_(nullptr), allocated_(true)
 {
+    json_array_t* arr = json_value_array(node_);
+    for (const auto& str : val)
+    {
+        json_array_append(arr, JSON_VALUE_STRING, str.c_str());
+    }
 }
 
 // for parse
@@ -378,6 +377,11 @@ bool Json::can_obj_push_back()
 
 void Json::push_back(const std::string& key, bool val)
 {
+    if (is_placeholder())
+    {
+        *this = Json::Object{{key, val}};
+        return;
+    }
     if (!can_obj_push_back())
     {
         return;
@@ -389,6 +393,11 @@ void Json::push_back(const std::string& key, bool val)
 
 void Json::push_back(const std::string& key, std::nullptr_t val)
 {
+    if (is_placeholder())
+    {
+        *this = Json::Object{{key, val}};
+        return;
+    }
     if (!can_obj_push_back())
     {
         return;
@@ -404,6 +413,11 @@ void Json::push_back(const std::string& key, const std::string& val)
 
 void Json::push_back(const std::string& key, const char* val)
 {
+    if (is_placeholder())
+    {
+        *this = Json::Object{{key, val}};
+        return;
+    }
     if (!can_obj_push_back())
     {
         return;
@@ -414,6 +428,11 @@ void Json::push_back(const std::string& key, const char* val)
 
 void Json::push_back(const std::string& key, const std::vector<std::string>& val)
 {
+    if (is_placeholder())
+    {
+        *this = Json::Object{{key, val}};
+        return;
+    }
     if (!can_obj_push_back())
     {
         return;
@@ -430,6 +449,11 @@ void Json::push_back(const std::string& key, const std::vector<std::string>& val
 
 void Json::push_back(const std::string& key, const Json& val)
 {
+    if (is_placeholder())
+    {
+        *this = Json::Object{{key, val}};
+        return;
+    }
     if (!can_obj_push_back())
     {
         return;
@@ -552,15 +576,18 @@ void Json::normal_push_back(const std::string& key,
 {
     json_object_t* obj = json_value_object(parent_);
     const json_value_t* find = json_object_find(key.c_str(), obj);
+    const json_value_t* v;
     if (find == nullptr)
     {
-        json_object_append(obj, key.c_str(), JSON_VALUE_NULL);
-        return;
+        v = json_object_append(obj, key.c_str(), JSON_VALUE_ARRAY);
+    }
+    else
+    {
+        v = json_object_insert_before(find, obj, key.c_str(),
+                                      JSON_VALUE_ARRAY);
+        json_value_t* remove_val = json_object_remove(find, obj);
+        json_value_destroy(remove_val);
     }
-    const json_value_t *v = json_object_insert_before(find, obj, key.c_str(),
-                                                      JSON_VALUE_ARRAY);
-    json_value_t* remove_val = json_object_remove(find, obj);
-    json_value_destroy(remove_val);
     json_array_t* arr = json_value_array(v);
     for (const auto& str : val)
         json_array_append(arr, JSON_VALUE_STRING, str.c_str());
@@ -599,6 +626,11 @@ Json Json::copy() const
 
 void Json::push_back(bool val)
 {
+    if (is_placeholder())
+    {
+        *this = Json::Array{{val}};
+        return;
+    }
     if (!can_arr_push_back())
     {
         return;
@@ -610,6 +642,11 @@ void Json::push_back(bool val)
 
 void Json::push_back(std::nullptr_t val)
 {
+    if (is_placeholder())
+    {
+        *this = Json::Array{{val}};
+        return;
+    }
     if (!can_arr_push_back())
     {
         return;
@@ -632,6 +669,11 @@ void Json::push_back(const std::vector<std::string>& val)
 
 void Json::push_back(const char* val)
 {
+    if (is_placeholder())
+    {
+        *this = Json::Array{{val}};
+        return;
+    }
     if (!can_arr_push_back())
     {
         return;
@@ -642,6 +684,11 @@ void Json::push_back(const char* val)
 
 void Json::push_back(const Json& val)
 {
+    if (is_placeholder())
+    {
+        *this = Json::Array{{val}};
+        return;
+    }
     if (!can_arr_push_back())
     {
         return;

+ 28 - 2
tools/templates/config/Json.h

@@ -204,6 +204,11 @@ public:
                                                   bool>::type = true>
     void push_back(const std::string& key, const T& val)
     {
+        if (is_placeholder())
+        {
+            *this = Json::Object{{key, val}};
+            return;
+        }
         if (!can_obj_push_back())
         {
             return;
@@ -219,6 +224,11 @@ public:
                                       bool>::type = true>
     void push_back(const std::string& key, const T& val)
     {
+        if (is_placeholder())
+        {
+            *this = Json::Object{{key, val}};
+            return;
+        }
         if (!can_obj_push_back())
         {
             return;
@@ -345,6 +355,11 @@ public:
                                                   bool>::type = true>
     void push_back(T val)
     {
+        if (is_placeholder())
+        {
+            *this = Json::Array{{val}};
+            return;
+        }
         if (!can_arr_push_back())
         {
             return;
@@ -359,6 +374,11 @@ public:
                                       bool>::type = true>
     void push_back(const T& val)
     {
+        if (is_placeholder())
+        {
+            *this = Json::Array{{val}};
+            return;
+        }
         if (!can_arr_push_back())
         {
             return;
@@ -709,9 +729,15 @@ public:
     Json(const std::string& str);
     Json(const char* str);
     Json(std::nullptr_t null);
-    Json(double val);
-    Json(int val);
+    template <typename T, typename std::enable_if<detail::is_number<T>::value,
+                                                  bool>::type = true>
+    Json(T val)
+        : node_(json_value_create(JSON_VALUE_NUMBER, static_cast<double>(val))),
+          parent_(nullptr), allocated_(true)
+    {
+    }
     Json(bool val);
+    Json(const std::vector<std::string>& val);
 
     // For parse
     Json(const std::string& str, bool parse_flag);

+ 1 - 1
workflow

@@ -1 +1 @@
-Subproject commit 273134f0ffadbb40a59ce9155a1fde299cd9d68b
+Subproject commit ba98ed0945f8888f73350ddfe5d9681306103702