• 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 "GdbEdge"
16 #include <utility>
17 
18 #include "gdb_errors.h"
19 #include "full_result.h"
20 #include "logger.h"
21 
22 namespace OHOS::DistributedDataAip {
Edge()23 Edge::Edge() : Vertex(), sourceId_("0"), targetId_("0")
24 {
25 }
26 
Edge(std::string id,std::string label,std::string sourceId,std::string targetId)27 Edge::Edge(std::string id, std::string label, std::string sourceId, std::string targetId)
28     : Vertex(std::move(id), std::move(label)), sourceId_(std::move(sourceId)), targetId_(std::move(targetId))
29 {
30 }
31 
Edge(const std::shared_ptr<Vertex> & element,std::string sourceId,std::string targetId)32 Edge::Edge(const std::shared_ptr<Vertex> &element, std::string sourceId, std::string targetId)
33     : sourceId_(std::move(sourceId)), targetId_(std::move(targetId))
34 {
35     if (element != nullptr) {
36         id_ = element->GetId();
37         label_ = element->GetLabel();
38         properties_ = element->GetProperties();
39     }
40 }
41 
GetSourceId() const42 std::string Edge::GetSourceId() const
43 {
44     return sourceId_;
45 }
46 
SetSourceId(std::string sourceId)47 void Edge::SetSourceId(std::string sourceId)
48 {
49     sourceId_ = std::move(sourceId);
50 }
51 
GetTargetId() const52 std::string Edge::GetTargetId() const
53 {
54     return targetId_;
55 }
56 
SetTargetId(std::string targetId)57 void Edge::SetTargetId(std::string targetId)
58 {
59     targetId_ = std::move(targetId);
60 }
61 
GetIdFromJson(const std::string & key,const nlohmann::json & json,int32_t & errCode)62 std::string Edge::GetIdFromJson(const std::string &key, const nlohmann::json &json, int32_t &errCode)
63 {
64     if (key.empty() || (!json.at(key).is_string() && !json.at(key).is_number())) {
65         LOG_ERROR("edge start or end id is not number or string. jsonStr=%{public}s", json.dump().c_str());
66         errCode = E_PARSE_JSON_FAILED;
67         return "";
68     }
69     errCode = E_OK;
70     if (json.at(key).is_number()) {
71         auto sourceId = json.at(key).get<int32_t>();
72         return std::to_string(sourceId);
73     }
74     if (json.at(key).is_string()) {
75         return json.at(key).get<std::string>();
76     }
77     errCode = E_PARSE_JSON_FAILED;
78     return "";
79 }
80 
Parse(const nlohmann::json & json,int32_t & errCode)81 std::shared_ptr<Edge> Edge::Parse(const nlohmann::json &json, int32_t &errCode)
82 {
83     if (!json.contains(Edge::SOURCEID) || !json.contains(Edge::TARGETID)) {
84         LOG_ERROR("edge format error. jsonStr=%{public}s", json.dump().c_str());
85         errCode = E_PARSE_JSON_FAILED;
86         return nullptr;
87     }
88     errCode = E_OK;
89     std::shared_ptr<Vertex> element = Vertex::Parse(json, errCode);
90     if (errCode != E_OK || element == nullptr) {
91         LOG_ERROR("parse edge element failed. jsonStr=%{public}s", json.dump().c_str());
92         return nullptr;
93     }
94     auto sourceId = Edge::GetIdFromJson(Edge::SOURCEID, json, errCode);
95     if (errCode != E_OK) {
96         return nullptr;
97     }
98     auto targetId = Edge::GetIdFromJson(Edge::TARGETID, json, errCode);
99     if (errCode != E_OK) {
100         return nullptr;
101     }
102     return std::make_shared<Edge>(element, sourceId, targetId);
103 }
104 } // namespace OHOS::DistributedDataAip
105