Skip to content

Commit 6f57eba

Browse files
authored
RSDK-11291 - make link config optional (#462)
1 parent 5a8049b commit 6f57eba

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/viam/sdk/config/resource.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@
2020
namespace viam {
2121
namespace sdk {
2222

23+
ResourceConfig::ResourceConfig(std::string type,
24+
std::string name,
25+
std::string namespace_,
26+
ProtoStruct attributes,
27+
std::string api,
28+
Model model,
29+
log_level lvl)
30+
: api_({kRDK, type, ""}),
31+
frame_(boost::none),
32+
model_(std::move(model)),
33+
name_(std::move(name)),
34+
namespace__(std::move(namespace_)),
35+
type_(std::move(type)),
36+
attributes_(std::move(attributes)),
37+
log_level_(lvl) {
38+
if (api.find(':') != std::string::npos) {
39+
api_ = API::from_string(std::move(api));
40+
}
41+
fix_api();
42+
}
43+
2344
ResourceConfig::ResourceConfig(std::string type,
2445
std::string name,
2546
std::string namespace_,
@@ -63,7 +84,7 @@ const API& ResourceConfig::api() const {
6384
return api_;
6485
}
6586

66-
const LinkConfig& ResourceConfig::frame() const {
87+
const boost::optional<LinkConfig>& ResourceConfig::frame() const {
6788
return frame_;
6889
}
6990

@@ -150,7 +171,9 @@ void to_proto_impl<ResourceConfig>::operator()(const ResourceConfig& self,
150171
*proto->mutable_depends_on() = ::google::protobuf::RepeatedPtrField<std::string>(
151172
self.depends_on().begin(), self.depends_on().end());
152173

153-
*proto->mutable_frame() = to_proto(self.frame());
174+
if (self.frame()) {
175+
*proto->mutable_frame() = to_proto(self.frame().get());
176+
}
154177
}
155178

156179
ResourceConfig from_proto_impl<app::v1::ComponentConfig>::operator()(

src/viam/sdk/config/resource.hpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <string>
44
#include <unordered_map>
55

6+
#include <boost/optional.hpp>
7+
68
#include <viam/sdk/common/proto_convert.hpp>
79
#include <viam/sdk/common/proto_value.hpp>
810
#include <viam/sdk/log/logging.hpp>
@@ -40,6 +42,14 @@ struct ResourceLevelServiceConfig {
4042

4143
class ResourceConfig {
4244
public:
45+
ResourceConfig(std::string type,
46+
std::string name,
47+
std::string namespace_,
48+
ProtoStruct attributes,
49+
std::string api,
50+
Model model,
51+
log_level lvl = sdk::log_level::info);
52+
4353
ResourceConfig(std::string type,
4454
std::string name,
4555
std::string namespace_,
@@ -55,7 +65,7 @@ class ResourceConfig {
5565

5666
const API& api() const;
5767

58-
const LinkConfig& frame() const;
68+
const boost::optional<LinkConfig>& frame() const;
5969

6070
const Model& model() const;
6171

@@ -76,7 +86,7 @@ class ResourceConfig {
7686

7787
API api_;
7888

79-
LinkConfig frame_;
89+
boost::optional<LinkConfig> frame_;
8090

8191
Model model_;
8292

src/viam/sdk/tests/test_resource.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ BOOST_AUTO_TEST_CASE(test_linkconfig) {
180180
BOOST_AUTO_TEST_CASE(test_resource) {
181181
ResourceConfig resource1("type");
182182
BOOST_CHECK_EQUAL(resource1.api().to_string(), "rdk:type:");
183-
BOOST_CHECK_EQUAL(resource1.frame().get_parent(), "");
183+
BOOST_CHECK(!resource1.frame());
184184
BOOST_CHECK_EQUAL(resource1.model().to_string(), "rdk:builtin:builtin");
185185
BOOST_CHECK_EQUAL(resource1.name(), "");
186186
BOOST_CHECK_EQUAL(resource1.namespace_(), "");
@@ -242,7 +242,7 @@ BOOST_AUTO_TEST_CASE(test_resource) {
242242
BOOST_CHECK_EQUAL(resource2.type(), "type");
243243
BOOST_CHECK_EQUAL(resource2.model().to_string(), "ns:mf:model1");
244244
BOOST_CHECK_EQUAL(resource2.api().to_string(), "ns:component:type");
245-
BOOST_CHECK_EQUAL(resource2.frame().get_parent(), "parent");
245+
BOOST_CHECK_EQUAL(resource2.frame()->get_parent(), "parent");
246246
std::string key;
247247
Value value;
248248
for (const auto& key_and_value : resource2.attributes()) {

0 commit comments

Comments
 (0)