1 /*
2  * Copyright (c) 2022 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 HISYSEVENT_JSON_DECORATOR_H
17 #define HISYSEVENT_JSON_DECORATOR_H
18 
19 #include "hisysevent_record.h"
20 #include "json/json.h"
21 
22 #include <functional>
23 #include <unordered_map>
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 enum Validity {
28     KEY_INVALID,
29     VALUE_INVALID,
30     KV_BOTH_VALID
31 };
32 
33 using BaseInfoHandler = std::function<bool(const Json::Value&)>;
34 using ExtensiveInfoHander = std::function<bool(const Json::Value&, const Json::Value&)>;
35 using DecorateMarks = std::unordered_map<std::string, Validity>;
36 
37 class HiSysEventJsonDecorator {
38 public:
39     HiSysEventJsonDecorator();
40     std::string DecorateEventJsonStr(const HiSysEventRecord& record);
41 
42 private:
43     bool CheckAttrDecorationNeed(const Json::Value& eventJson, const std::string& key,
44         const Json::Value& standard);
45     Validity CheckAttrValidity(const Json::Value& eventJson, const std::string& key,
46         const Json::Value& standard);
47     Validity CheckLevelValidity(const Json::Value& baseInfo);
48     bool CheckEventDecorationNeed(const Json::Value& eventJson, BaseInfoHandler baseJsonInfoHandler,
49         ExtensiveInfoHander extensiveJsonInfoHandler);
50     std::string Decorate(Validity validity, std::string& key, std::string& value);
51     std::string DecorateJsonStr(const std::string& standard, DecorateMarks marks);
52     bool JudgeDataType(const std::string& dataType, const Json::Value& eventJson);
53 
54 private:
55     Json::Value jsonRoot_;
56     DecorateMarks decoratedMarks_;
57     bool isJsonRootValid_ = false;
58 };
59 } // namespace HiviewDFX
60 } // namespace OHOS
61 
62 #endif // HISYSEVENT_JSON_DECORATOR_H