• 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 "GdbPath"
16 #include "path.h"
17 
18 #include "gdb_errors.h"
19 #include "path_segment.h"
20 #include "grd_error.h"
21 #include "logger.h"
22 
23 namespace OHOS::DistributedDataAip {
Path(std::shared_ptr<Vertex> start,std::shared_ptr<Vertex> end)24 Path::Path(std::shared_ptr<Vertex> start, std::shared_ptr<Vertex> end) : start_(start), end_(end)
25 {
26     pathLen_ = 0;
27 }
28 
Path(std::shared_ptr<Vertex> start,std::shared_ptr<Vertex> end,uint32_t pathLen,std::vector<std::shared_ptr<PathSegment>> segments)29 Path::Path(std::shared_ptr<Vertex> start, std::shared_ptr<Vertex> end, uint32_t pathLen,
30     std::vector<std::shared_ptr<PathSegment>> segments)
31     : pathLen_(pathLen), start_(start),  end_(end), segments_(std::move(segments))
32 {
33 }
34 
Path()35 Path::Path() : pathLen_(0), start_(nullptr), end_(nullptr), segments_()
36 {
37 }
38 
GetPathLength() const39 uint32_t Path::GetPathLength() const
40 {
41     return pathLen_;
42 }
43 
SetPathLength(uint32_t pathLen)44 void Path::SetPathLength(uint32_t pathLen)
45 {
46     this->pathLen_ = pathLen;
47 }
48 
GetStart() const49 std::shared_ptr<Vertex> Path::GetStart() const
50 {
51     return start_;
52 }
53 
SetStart(std::shared_ptr<Vertex> start)54 void Path::SetStart(std::shared_ptr<Vertex> start)
55 {
56     this->start_ = start;
57 }
58 
GetEnd() const59 std::shared_ptr<Vertex> Path::GetEnd() const
60 {
61     return end_;
62 }
63 
SetEnd(std::shared_ptr<Vertex> end)64 void Path::SetEnd(std::shared_ptr<Vertex> end)
65 {
66     this->end_ = end;
67 }
68 
GetSegments() const69 const std::vector<std::shared_ptr<PathSegment>> &Path::GetSegments() const
70 {
71     return segments_;
72 }
73 
Parse(const nlohmann::json & json,int32_t & errCode)74 std::shared_ptr<Path> Path::Parse(const nlohmann::json &json, int32_t &errCode)
75 {
76     if (!json.contains(Path::PATHLEN) || !json.contains(Path::START) || !json.at(Path::START).is_object() ||
77         !json.contains(Path::END) || !json.at(Path::END).is_object() || !json.contains(Path::SEGMENTS) ||
78         !json.at(Path::SEGMENTS).is_array() || !json.at(Path::PATHLEN).is_number_unsigned()) {
79         LOG_ERROR("path format error. jsonStr=%{public}s", json.dump().c_str());
80         errCode = E_PARSE_JSON_FAILED;
81         return nullptr;
82     }
83     errCode = E_OK;
84 
85     auto pathLen = json.at(Path::PATHLEN).get<uint32_t>();
86     auto start = Vertex::Parse(json.at(Path::START), errCode);
87     if (errCode != E_OK) {
88         return nullptr;
89     }
90 
91     auto end = Vertex::Parse(json.at(Path::END), errCode);
92     if (errCode != E_OK) {
93         return nullptr;
94     }
95     auto pathSegments = std::vector<std::shared_ptr<PathSegment>>();
96     for (const auto &item : json.at(Path::SEGMENTS)) {
97         if (!item.is_object()) {
98             LOG_ERROR("pathItem format error. jsonStr=%{public}s", json.dump().c_str());
99             return nullptr;
100         }
101         auto pathSegment = PathSegment::Parse(item, errCode);
102         if (errCode != E_OK) {
103             return nullptr;
104         }
105         pathSegments.push_back(pathSegment);
106     }
107     errCode = E_OK;
108     return std::make_shared<Path>(start, end, pathLen, pathSegments);
109     ;
110 }
111 } // namespace OHOS::DistributedDataAip
112