• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 PANDA_TOOLING_INSPECTOR_JSON_PROPERTY_H
17 #define PANDA_TOOLING_INSPECTOR_JSON_PROPERTY_H
18 
19 #include "utils/json_parser.h"
20 #include "utils/logger.h"
21 
22 #include <string>
23 #include <type_traits>
24 
25 namespace panda::tooling::inspector {
26 template <typename PropertyType, typename Result, typename... Key>
GetProperty(Result & result,const JsonObject & object,const JsonObject::Key & key,const Key &...keys)27 bool GetProperty(Result &result, const JsonObject &object, const JsonObject::Key &key, const Key &... keys)
28 {
29     if constexpr (sizeof...(keys) > 0) {  // NOLINT(bugprone-suspicious-semicolon,readability-braces-around-statements)
30         auto *ptr = object.GetValue<JsonObject::JsonObjPointer>(key);
31         return ptr && GetProperty<PropertyType>(result, **ptr, keys...);
32     }
33 
34     auto *ptr = object.GetValue<PropertyType>(key);
35     if (!ptr) {
36         return false;
37     }
38 
39     if constexpr (std::is_scalar_v<PropertyType>) {  // NOLINT(readability-braces-around-statements)
40         result = *ptr;
41         // NOLINTNEXTLINE(readability-braces-around-statements,readability-misleading-indentation)
42     } else if constexpr (std::is_same_v<PropertyType, JsonObject::JsonObjPointer>) {
43         result = ptr->get();
44     } else {  // NOLINT(readability-misleading-indentation)
45         result = ptr;
46     }
47     return true;
48 }
49 
50 template <typename PropertyType, typename Result, typename... Key>
GetPropertyOrLog(Result & result,const JsonObject & object,const JsonObject::Key & key,const Key &...keys)51 bool GetPropertyOrLog(Result &result, const JsonObject &object, const JsonObject::Key &key, const Key &... keys)
52 {
53     using namespace std::literals::string_literals;
54 
55     if (GetProperty<PropertyType>(result, object, key, keys...)) {
56         return true;
57     }
58 
59     LOG(INFO, DEBUGGER) << "No '" << (key + ... + ("."s + keys)) << "' property";
60     return false;
61 }
62 }  // namespace panda::tooling::inspector
63 
64 #endif  // PANDA_TOOLING_INSPECTOR_JSON_PROPERTY_H
65