1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 
16 #ifndef JSON_COMMON_H
17 #define JSON_COMMON_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <set>
22 #include <vector>
23 
24 #include "rd_json_object.h"
25 
26 namespace DocumentDB {
27 class JsonCommon {
28 public:
29     static ValueObject GetValueInSameLevel(JsonObject &node, const std::string &field);
30     static ValueObject GetValueInSameLevel(JsonObject &node, const std::string &field, bool &isFieldExist);
31     static bool CheckJsonField(JsonObject &node);
32     static bool CheckProjectionField(JsonObject &node, int &errCode);
33     static int ParseNode(JsonObject &Node, std::vector<std::string> singlePath,
34         std::vector<std::vector<std::string>> &resultPath, bool isFirstLevel);
35     static std::vector<std::vector<std::string>> ParsePath(const JsonObject &node, int &errCode);
36     static std::vector<ValueObject> GetLeafValue(const JsonObject &node);
37     static bool isValueEqual(const ValueObject &srcValue, const ValueObject &targetValue);
38     static int Append(const JsonObject &src, const JsonObject &add, bool isReplace);
39     static bool IsJsonNodeMatch(const JsonObject &src, const JsonObject &target, int &errCode);
40     static bool ConvertToInt(const std::string &str, int &value);
41 
42 private:
43     static bool JsonEqualJudge(const JsonFieldPath &itemPath, const JsonObject &src, const JsonObject &item,
44         bool &isCollapse, int &isMatchFlag);
45     static bool CheckNode(JsonObject &Node);
46     static bool CheckProjectionNode(JsonObject &Node, bool isFirstLevel, int &errCode);
47     static void CheckLeafNode(const JsonObject &Node, std::vector<ValueObject> &leafValue);
48     static bool IsArrayMatch(const JsonObject &src, const JsonObject &target, int &isAlreadyMatched);
49     static bool IsObjectItemMatch(const JsonObject &srcItem, const JsonObject &item, int &isAlreadyMatched,
50         bool &isCollapse, int &isMatchFlag);
51 };
52 } // namespace DocumentDB
53 #endif // JSON_COMMON_H