-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathresource.cpp
176 lines (145 loc) · 5.72 KB
/
resource.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <viam/sdk/config/resource.hpp>
#include <numeric>
#include <string>
#include <unordered_map>
#include <boost/algorithm/string.hpp>
#include <viam/api/app/v1/robot.pb.h>
#include <viam/api/robot/v1/robot.pb.h>
#include <viam/api/service/discovery/v1/discovery.pb.h>
#include <viam/sdk/common/exception.hpp>
#include <viam/sdk/common/private/repeated_ptr_convert.hpp>
#include <viam/sdk/common/proto_value.hpp>
#include <viam/sdk/common/utils.hpp>
#include <viam/sdk/referenceframe/frame.hpp>
#include <viam/sdk/resource/resource.hpp>
namespace viam {
namespace sdk {
ResourceConfig::ResourceConfig(std::string type,
std::string name,
std::string namespace_,
ProtoStruct attributes,
std::string api,
Model model,
LinkConfig frame,
sdk::log_level lvl)
: api_({kRDK, type, ""}),
frame_(std::move(frame)),
model_(std::move(model)),
name_(std::move(name)),
namespace__(std::move(namespace_)),
type_(std::move(type)),
attributes_(std::move(attributes)),
log_level_(lvl) {
if (api.find(':') != std::string::npos) {
api_ = API::from_string(std::move(api));
}
fix_api();
}
ResourceConfig::ResourceConfig(std::string type) : api_({kRDK, type, ""}), type_(std::move(type)) {}
Name ResourceConfig::resource_name() {
this->fix_api();
std::vector<std::string> remotes;
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
boost::split(remotes, this->name_, boost::is_any_of(":"));
if (remotes.size() > 1) {
const std::string str_name(remotes.back());
remotes.pop_back();
const std::string remote =
std::accumulate(remotes.begin(), remotes.end(), std::string(":"));
return Name(this->api_, remote, str_name);
}
return Name(this->api_, "", remotes.at(0));
}
const API& ResourceConfig::api() const {
return api_;
}
const LinkConfig& ResourceConfig::frame() const {
return frame_;
}
const Model& ResourceConfig::model() const {
return model_;
}
const std::string& ResourceConfig::name() const {
return name_;
}
const std::string& ResourceConfig::namespace_() const {
return namespace__;
}
const std::string& ResourceConfig::type() const {
return type_;
}
log_level ResourceConfig::get_log_level() const {
return log_level_;
}
const std::vector<std::string>& viam::sdk::ResourceConfig::depends_on() const {
return depends_on_;
}
const std::vector<ResourceLevelServiceConfig>& viam::sdk::ResourceConfig::service_config() const {
return service_config_;
}
const ProtoStruct& ResourceConfig::attributes() const {
return attributes_;
}
void ResourceConfig::fix_api() {
if (this->api_.type_namespace().empty() && this->namespace__.empty()) {
this->namespace__ = kRDK;
this->api_.namespace_ = kRDK;
} else if (this->api_.type_namespace().empty()) {
api_.namespace_ = namespace__;
} else {
this->namespace__ = this->api_.type_namespace();
}
if (this->api_.resource_type().empty()) {
api_.resource_type_ = kComponent;
}
if (this->api_.resource_subtype().empty()) {
api_.resource_subtype_ = this->type_;
} else if (this->type_.empty()) {
this->type_ = this->api_.resource_subtype();
}
// This shouldn't be able to happen except with directly instantiated
// config structs
if (this->api_.type_namespace() != this->namespace__ ||
this->api_.resource_subtype() != this->type_) {
throw Exception("component namespace and/or type do not match component api field");
}
}
namespace proto_convert_details {
void to_proto_impl<ResourceLevelServiceConfig>::operator()(
const ResourceLevelServiceConfig& self, app::v1::ResourceLevelServiceConfig* proto) const {
*proto->mutable_type() = self.type;
*proto->mutable_attributes() = to_proto(self.attributes);
}
void to_proto_impl<ResourceConfig>::operator()(const ResourceConfig& self,
app::v1::ComponentConfig* proto) const {
*proto->mutable_service_configs() = impl::to_repeated_field(self.service_config());
*proto->mutable_name() = self.name();
*proto->mutable_namespace_() = self.namespace_();
*proto->mutable_type() = self.type();
*proto->mutable_api() = self.api().to_string();
*proto->mutable_log_configuration()->mutable_level() = to_string(self.get_log_level());
*proto->mutable_model() = self.model().to_string();
*proto->mutable_attributes() = to_proto(self.attributes());
*proto->mutable_depends_on() = ::google::protobuf::RepeatedPtrField<std::string>(
self.depends_on().begin(), self.depends_on().end());
*proto->mutable_frame() = to_proto(self.frame());
}
ResourceConfig from_proto_impl<app::v1::ComponentConfig>::operator()(
const app::v1::ComponentConfig* proto) const {
return ResourceConfig(proto->type(),
proto->name(),
proto->namespace_(),
from_proto(proto->attributes()),
proto->api(),
Model::from_str(proto->model()),
proto->has_frame() ? from_proto(proto->frame()) : LinkConfig{},
level_from_string(proto->log_configuration().level()));
}
std::vector<ResourceConfig>
from_proto_impl<service::discovery::v1::DiscoverResourcesResponse>::operator()(
const service::discovery::v1::DiscoverResourcesResponse* proto) const {
return impl::from_repeated_field(proto->discoveries());
}
} // namespace proto_convert_details
} // namespace sdk
} // namespace viam