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 "GdbPath"
16 #include "path_segment.h"
17
18 #include "gdb_errors.h"
19 #include "grd_error.h"
20 #include "logger.h"
21
22 namespace OHOS::DistributedDataAip {
PathSegment()23 PathSegment::PathSegment() : sourceVertex_(nullptr), edge_(nullptr), targetVertex_(nullptr)
24 {
25 }
26
PathSegment(std::shared_ptr<Vertex> sourceVertex,std::shared_ptr<Vertex> targetVertex,std::shared_ptr<Edge> edge)27 PathSegment::PathSegment(
28 std::shared_ptr<Vertex> sourceVertex, std::shared_ptr<Vertex> targetVertex, std::shared_ptr<Edge> edge)
29 : sourceVertex_(sourceVertex), edge_(edge), targetVertex_(targetVertex)
30 {
31 }
32
GetSourceVertex() const33 std::shared_ptr<Vertex> PathSegment::GetSourceVertex() const
34 {
35 return sourceVertex_;
36 }
37
SetSourceVertex(std::shared_ptr<Vertex> vertex)38 void PathSegment::SetSourceVertex(std::shared_ptr<Vertex> vertex)
39 {
40 sourceVertex_ = vertex;
41 }
42
GetEdge() const43 std::shared_ptr<Edge> PathSegment::GetEdge() const
44 {
45 return edge_;
46 }
47
SetEdge(std::shared_ptr<Edge> edge)48 void PathSegment::SetEdge(std::shared_ptr<Edge> edge)
49 {
50 this->edge_ = edge;
51 }
52
GetTargetVertex() const53 std::shared_ptr<Vertex> PathSegment::GetTargetVertex() const
54 {
55 return targetVertex_;
56 }
57
SetTargetVertex(std::shared_ptr<Vertex> vertex)58 void PathSegment::SetTargetVertex(std::shared_ptr<Vertex> vertex)
59 {
60 targetVertex_ = vertex;
61 }
62
Parse(const nlohmann::json & json,int32_t & errCode)63 std::shared_ptr<PathSegment> PathSegment::Parse(const nlohmann::json &json, int32_t &errCode)
64 {
65 if (!json.contains(PathSegment::SOURCE_VERTEX) || !json.at(PathSegment::SOURCE_VERTEX).is_object() ||
66 !json.contains(PathSegment::EDGE) || !json.at(PathSegment::EDGE).is_object() ||
67 !json.contains(PathSegment::TARGET_VERTEX) || !json.at(PathSegment::TARGET_VERTEX).is_object()) {
68 LOG_ERROR("pathSegment format error. jsonStr=%{public}s", json.dump().c_str());
69 errCode = E_PARSE_JSON_FAILED;
70 return nullptr;
71 }
72 errCode = E_OK;
73 std::shared_ptr<PathSegment> segment = std::make_shared<PathSegment>();
74 auto sourceVertex = Vertex::Parse(json.at(PathSegment::SOURCE_VERTEX), errCode);
75 if (errCode != E_OK) {
76 return nullptr;
77 }
78 segment->SetSourceVertex(sourceVertex);
79
80 auto edge = Edge::Parse(json.at(PathSegment::EDGE), errCode);
81 if (errCode != E_OK) {
82 return nullptr;
83 }
84 segment->SetEdge(edge);
85
86 auto targetVertex = Vertex::Parse(json.at(PathSegment::TARGET_VERTEX), errCode);
87 if (errCode != E_OK) {
88 return nullptr;
89 }
90 segment->SetTargetVertex(targetVertex);
91
92 return segment;
93 }
94 } // namespace OHOS::DistributedDataAip
95