Skip to content

RSDK-11031: Add accessors to Model and ModelFamily #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/viam/sdk/resource/resource_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,28 @@ const API& RPCSubtype::api() const {
ModelFamily::ModelFamily(std::string namespace_, std::string family)
: namespace_(std::move(namespace_)), family_(std::move(family)) {}

const std::string& ModelFamily::get_namespace() const {
return namespace_;
}

const std::string& ModelFamily::get_family() const {
return family_;
}

Model::Model(ModelFamily model_family, std::string model_name)
: model_family_(std::move(model_family)), model_name_(std::move(model_name)) {}

Model::Model(std::string namespace_, std::string family, std::string model_name)
: Model(ModelFamily(std::move(namespace_), std::move(family)), std::move(model_name)) {}

const ModelFamily& Model::get_model_family() const {
return model_family_;
}

const std::string& Model::get_model_name() const {
return model_name_;
}

Model Model::from_str(std::string model) {
if (std::regex_match(model, MODEL_REGEX)) {
std::vector<std::string> model_parts;
Expand Down
11 changes: 9 additions & 2 deletions src/viam/sdk/resource/resource_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ class RPCSubtype {
class ModelFamily {
public:
ModelFamily(std::string namespace_, std::string family);

const std::string& get_namespace() const;
const std::string& get_family() const;

std::string to_string() const;

private:
Expand All @@ -124,12 +128,15 @@ class ModelFamily {
/// @brief Defines the namespace_, family, and name for a particular resource model.
class Model {
public:
std::string to_string() const;

Model(std::string namespace_, std::string family, std::string model_name);
Model(ModelFamily model, std::string model_name);
Model();

const ModelFamily& get_model_family() const;
const std::string& get_model_name() const;

std::string to_string() const;

/// @brief Parses a single model string into a Model, using default values for namespace and
/// family if not provided.
///
Expand Down
12 changes: 12 additions & 0 deletions src/viam/sdk/tests/test_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,32 @@ BOOST_AUTO_TEST_CASE(test_name) {
BOOST_AUTO_TEST_CASE(test_model) {
ModelFamily mf("ns", "mf");
BOOST_CHECK_EQUAL(mf.to_string(), "ns:mf");
BOOST_CHECK_EQUAL(mf.get_namespace(), "ns");
BOOST_CHECK_EQUAL(mf.get_family(), "mf");

Model model1(mf, "model1");
BOOST_CHECK_EQUAL(model1.to_string(), "ns:mf:model1");
BOOST_CHECK_EQUAL(model1.get_model_family().to_string(), "ns:mf");
BOOST_CHECK_EQUAL(model1.get_model_name(), "model1");
Model model2("ns", "mf", "model2");
BOOST_CHECK_EQUAL(model2.to_string(), "ns:mf:model2");
BOOST_CHECK_EQUAL(model2.get_model_family().to_string(), "ns:mf");
BOOST_CHECK_EQUAL(model2.get_model_name(), "model2");

Model model3 = Model::from_str("ns:mf:model3");
BOOST_CHECK_EQUAL(model3.to_string(), "ns:mf:model3");
BOOST_CHECK_EQUAL(model3.get_model_family().to_string(), "ns:mf");
BOOST_CHECK_EQUAL(model3.get_model_name(), "model3");
Model model4 = Model::from_str("model4");
BOOST_CHECK_EQUAL(model4.to_string(), "rdk:builtin:model4");
BOOST_CHECK_EQUAL(model4.get_model_family().to_string(), "rdk:builtin");
BOOST_CHECK_EQUAL(model4.get_model_name(), "model4");

ModelFamily empty("", "");
Model model5(empty, "model5");
BOOST_CHECK_EQUAL(model5.to_string(), "model5");
BOOST_CHECK_EQUAL(model5.get_model_family().to_string(), "");
BOOST_CHECK_EQUAL(model5.get_model_name(), "model5");

BOOST_CHECK_THROW(Model::from_str("@"), Exception);
}
Expand Down
Loading