• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #define LOG_TAG "GdbElement"
16 #include <utility>
17 
18 #include "gdb_errors.h"
19 #include "full_result.h"
20 #include "logger.h"
21 
22 namespace OHOS::DistributedDataAip {
Vertex()23 Vertex::Vertex() : id_("0"), properties_()
24 {
25 }
26 
Vertex(std::string id,std::string label)27 Vertex::Vertex(std::string id, std::string label) : id_(std::move(id)), label_(std::move(label))
28 {
29     labels_.emplace_back(label_);
30 }
31 
Vertex(std::string id,std::string label,const std::unordered_map<std::string,PropType> & properties)32 Vertex::Vertex(std::string id, std::string label,
33     const std::unordered_map<std::string, PropType> &properties)
34     : id_(std::move(id)), label_(std::move(label)), properties_(properties)
35 {
36     labels_.emplace_back(label_);
37 }
38 
GetId() const39 std::string Vertex::GetId() const
40 {
41     return id_;
42 }
43 
SetId(std::string id)44 void Vertex::SetId(std::string id)
45 {
46     id_ = std::move(id);
47 }
48 
GetLabel() const49 const std::string &Vertex::GetLabel() const
50 {
51     return label_;
52 }
53 
GetLabels() const54 const std::vector<std::string> &Vertex::GetLabels() const
55 {
56     return labels_;
57 }
58 
SetLabel(const std::string & label)59 void Vertex::SetLabel(const std::string &label)
60 {
61     label_ = label;
62     labels_.emplace_back(label);
63 }
64 
GetProperties() const65 const std::unordered_map<std::string, PropType> &Vertex::GetProperties() const
66 {
67     return properties_;
68 }
69 
SetProperty(const std::string & key,PropType value)70 void Vertex::SetProperty(const std::string &key, PropType value)
71 {
72     properties_[key] = std::move(value);
73 }
74 
Parse(const nlohmann::json & json,int32_t & errCode)75 std::shared_ptr<Vertex> Vertex::Parse(const nlohmann::json &json, int32_t &errCode)
76 {
77     std::shared_ptr<Vertex> element = std::make_shared<Vertex>();
78     if (!json.contains(Vertex::LABEL) || !json.contains(Vertex::ID) ||
79         !json.contains(Vertex::PROPERTIES) || !json.at(Vertex::PROPERTIES).is_object()) {
80         LOG_ERROR("element format error.");
81         errCode = E_PARSE_JSON_FAILED;
82         return nullptr;
83     }
84 
85     if (json.at(Vertex::ID).is_number()) {
86         auto id = json.at(Vertex::ID).get<int32_t>();
87         element->SetId(std::to_string(id));
88     } else if (json.at(Vertex::ID).is_string()) {
89         auto id = json.at(Vertex::ID).get<std::string>();
90         element->SetId(id);
91     } else {
92         LOG_ERROR("element id is not number or string.");
93         errCode = E_PARSE_JSON_FAILED;
94         return nullptr;
95     }
96     if (!json.at(Vertex::LABEL).is_string()) {
97         LOG_ERROR("element label is not string.");
98         errCode = E_PARSE_JSON_FAILED;
99         return nullptr;
100     }
101     element->SetLabel(json.at(Vertex::LABEL).get<std::string>());
102     for (const auto &[key, value] : json.at(Vertex::PROPERTIES).items()) {
103         if (value.is_string()) {
104             element->SetProperty(key, value.get<std::string>());
105         } else if (value.is_number_integer()) {
106             element->SetProperty(key, value.get<int64_t>());
107         } else if (value.is_number_float()) {
108             element->SetProperty(key, value.get<double>());
109         } else if (value.is_boolean()) {
110             element->SetProperty(key, value.get<bool>());
111         } else if (value.is_null()) {
112             element->SetProperty(key, nullptr);
113         } else {
114             LOG_ERROR("element property value type is not supported.");
115             errCode = E_PARSE_JSON_FAILED;
116             return nullptr;
117         }
118     }
119     errCode = E_OK;
120     return element;
121 }
122 } // namespace OHOS::DistributedDataAip
123