Skip to content

Commit e331f2c

Browse files
authored
RSDK-9299 - refactor get_resource_name to be a non-proto method (#329)
1 parent f49b1f6 commit e331f2c

File tree

12 files changed

+74
-65
lines changed

12 files changed

+74
-65
lines changed

src/viam/sdk/common/utils.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <unordered_map>
55
#include <vector>
66

7+
#include <boost/algorithm/string.hpp>
78
#include <boost/blank.hpp>
89
#include <boost/log/core.hpp>
910
#include <boost/log/expressions.hpp>
@@ -183,6 +184,26 @@ bool from_dm_from_extra(const ProtoStruct& extra) {
183184
}
184185
return false;
185186
}
187+
std::pair<std::string, std::string> long_name_to_remote_and_short(const std::string& long_name) {
188+
std::vector<std::string> name_parts;
189+
// boost::split causes a clang-tidy false positive, see
190+
// https://bugs.llvm.org/show_bug.cgi?id=41141
191+
//
192+
// NOLINTNEXTLINE
193+
boost::split(name_parts, long_name, boost::is_any_of(":"));
194+
auto name = name_parts.back();
195+
name_parts.pop_back();
196+
auto remote_name = name_parts.empty()
197+
? ""
198+
: std::accumulate(std::next(name_parts.begin()),
199+
name_parts.end(),
200+
*name_parts.begin(),
201+
[](const std::string& a, const std::string& b) {
202+
return a + ":" + b;
203+
});
204+
205+
return {std::move(remote_name), std::move(name)};
206+
}
186207

187208
} // namespace sdk
188209
} // namespace viam

src/viam/sdk/common/utils.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ class ClientContext {
6161
grpc::ClientContext wrapped_context_;
6262
};
6363

64+
/// @brief Given a fully qualified resource name, returns remote name (or "" if no remote name
65+
/// exists) and short name
66+
std::pair<std::string, std::string> long_name_to_remote_and_short(const std::string& long_name);
67+
6468
/// @brief Returns a new `ProtoStruct` with a random key for server-side debug logging
6569
ProtoStruct debug_map();
6670

src/viam/sdk/components/component.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,17 @@
22

33
#include <string>
44

5-
#include <google/protobuf/struct.pb.h>
6-
7-
#include <viam/api/common/v1/common.pb.h>
8-
95
#include <viam/sdk/common/utils.hpp>
10-
#include <viam/sdk/resource/resource.hpp>
116

127
namespace viam {
138
namespace sdk {
149

15-
using viam::common::v1::ResourceName;
16-
17-
Component::Component() : Resource("component"){};
10+
Component::Component() : Resource("component") {}
1811

19-
Component::Component(std::string name) : Resource(std::move(name)){};
12+
Component::Component(std::string name) : Resource(std::move(name)) {}
2013

21-
ResourceName Component::get_resource_name(std::string name) const {
22-
auto r = this->Resource::get_resource_name(name);
23-
*r.mutable_type() = kComponent;
24-
return r;
14+
Name Component::get_resource_name() const {
15+
return Resource::get_resource_name(kComponent);
2516
}
2617

2718
} // namespace sdk

src/viam/sdk/components/component.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
#include <string>
44

5-
#include <google/protobuf/struct.pb.h>
6-
7-
#include <viam/api/common/v1/common.pb.h>
8-
95
#include <viam/sdk/resource/resource.hpp>
106

117
namespace viam {
@@ -14,7 +10,7 @@ namespace sdk {
1410
class Component : public Resource {
1511
public:
1612
Component();
17-
viam::common::v1::ResourceName get_resource_name(std::string name) const override;
13+
Name get_resource_name() const override;
1814

1915
protected:
2016
explicit Component(std::string name);

src/viam/sdk/registry/registry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ Registry::registered_models() {
162162
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
163163
Status ModelRegistration::create_status(const std::shared_ptr<Resource>& resource) const {
164164
Status status;
165-
*status.mutable_name() = resource->get_resource_name(resource->name());
165+
*status.mutable_name() = resource->get_resource_name().to_proto();
166166
*status.mutable_status() = google::protobuf::Struct();
167167
return status;
168168
}

src/viam/sdk/resource/resource.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include <viam/sdk/resource/resource.hpp>
22

3-
#include <grpcpp/support/status.h>
4-
53
#include <viam/sdk/common/proto_value.hpp>
64
#include <viam/sdk/common/utils.hpp>
75
#include <viam/sdk/registry/registry.hpp>
@@ -10,23 +8,23 @@
108
namespace viam {
119
namespace sdk {
1210

13-
using common::v1::ResourceName;
14-
1511
Resource::~Resource() = default;
1612
Resource::Resource(std::string name) : name_(std::move(name)) {}
1713

1814
std::string Resource::name() const {
1915
return name_;
2016
}
2117

22-
ResourceName Resource::get_resource_name(std::string name) const {
23-
ResourceName r;
24-
*r.mutable_namespace_() = kRDK;
25-
*r.mutable_type() = kResource;
26-
*r.mutable_subtype() = this->api().resource_subtype();
27-
*r.mutable_name() = std::move(name);
18+
Name Resource::get_resource_name(const std::string& type) const {
19+
auto api_ = api();
20+
api_.set_resource_type(type);
21+
22+
auto name_parts = long_name_to_remote_and_short(name_);
23+
return {api_, name_parts.first, name_parts.second};
24+
}
2825

29-
return r;
26+
Name Resource::get_resource_name() const {
27+
return get_resource_name(kResource);
3028
}
3129

3230
} // namespace sdk

src/viam/sdk/resource/resource.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
#include <unordered_map>
44

5-
#include <grpcpp/impl/service_type.h>
6-
#include <grpcpp/support/status.h>
7-
85
#include <viam/sdk/common/proto_value.hpp>
96
#include <viam/sdk/config/resource.hpp>
107
#include <viam/sdk/resource/resource_api.hpp>
@@ -22,14 +19,17 @@ class Resource {
2219
/// @brief Returns the `API` associated with a particular resource.
2320
virtual API api() const = 0;
2421

25-
/// @brief Returns a `ResourceName` for a particular resource name.
26-
virtual viam::common::v1::ResourceName get_resource_name(std::string name) const;
22+
/// @brief Returns the `Name` for a particular resource.
23+
virtual Name get_resource_name() const;
2724

2825
/// @brief Return the resource's name.
2926
virtual std::string name() const;
3027

3128
private:
3229
std::string name_;
30+
31+
protected:
32+
Name get_resource_name(const std::string& type) const;
3333
};
3434

3535
template <>

src/viam/sdk/resource/resource_api.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ std::string APIType::to_string() const {
3232
}
3333

3434
APIType::APIType(std::string namespace_, std::string resource_type)
35-
: namespace_(std::move(namespace_)), resource_type_(std::move(resource_type)){};
35+
: namespace_(std::move(namespace_)), resource_type_(std::move(resource_type)) {}
3636

3737
std::string API::to_string() const {
3838
return APIType::to_string() + ":" + resource_subtype_;
@@ -126,21 +126,10 @@ viam::common::v1::ResourceName Name::to_proto() const {
126126
}
127127

128128
Name Name::from_proto(const viam::common::v1::ResourceName& proto) {
129-
const API api(proto.namespace_(), proto.type(), proto.subtype());
130-
std::vector<std::string> name_parts;
131-
boost::split(name_parts, proto.name(), boost::is_any_of(":"));
132-
auto name = name_parts.back();
133-
name_parts.pop_back();
134-
auto remote_name = name_parts.empty()
135-
? ""
136-
: std::accumulate(std::next(name_parts.begin()),
137-
name_parts.end(),
138-
*name_parts.begin(),
139-
[](const std::string& a, const std::string& b) {
140-
return a + ":" + b;
141-
});
142-
143-
return Name({proto.namespace_(), proto.type(), proto.subtype()}, remote_name, name);
129+
auto name_parts = long_name_to_remote_and_short(proto.name());
130+
131+
return Name(
132+
{proto.namespace_(), proto.type(), proto.subtype()}, name_parts.first, name_parts.second);
144133
};
145134

146135
Name Name::from_string(std::string name) {

src/viam/sdk/robot/service.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ std::vector<Status> RobotService_::generate_status_(
8282
const std::shared_ptr<const ModelRegistration> registration = kv.second;
8383
if (registration->api().resource_subtype() == resource->api().resource_subtype()) {
8484
bool resource_present = false;
85-
const ResourceName name = resource->get_resource_name(resource->name());
85+
const ResourceName name = resource->get_resource_name().to_proto();
8686
for (const auto& resource_name : resource_names) {
8787
if (name.SerializeAsString() == resource_name.SerializeAsString()) {
8888
resource_present = true;
@@ -195,7 +195,7 @@ ::grpc::Status RobotService_::StopAll(::grpc::ServerContext*,
195195

196196
for (const auto& r : resource_manager()->resources()) {
197197
const std::shared_ptr<Resource> resource = r.second;
198-
const ResourceName rn = resource->get_resource_name(resource->name());
198+
const ResourceName rn = resource->get_resource_name().to_proto();
199199
const std::string rn_ = rn.SerializeAsString();
200200
if (extra.find(rn_) != extra.end()) {
201201
try {

src/viam/sdk/services/service.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22

33
#include <string>
44

5-
#include <viam/api/common/v1/common.pb.h>
6-
75
#include <viam/sdk/common/utils.hpp>
8-
#include <viam/sdk/resource/resource.hpp>
96

107
namespace viam {
118
namespace sdk {
129

13-
common::v1::ResourceName Service::get_resource_name(std::string name) const {
14-
auto r = this->Resource::get_resource_name(name);
15-
*r.mutable_type() = kService;
16-
return r;
10+
Name Service::get_resource_name() const {
11+
return Resource::get_resource_name(kService);
1712
}
1813

19-
Service::Service() : Resource("service"){};
14+
Service::Service() : Resource("service") {}
2015

2116
} // namespace sdk
2217
} // namespace viam

0 commit comments

Comments
 (0)