Skip to content
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

RSDK-8343 - create public version_metadata file #290

Merged
merged 6 commits into from
Sep 23, 2024
Merged
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
2 changes: 2 additions & 0 deletions src/viam/sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ target_sources(viamsdk
common/proto_type.cpp
common/service_helper.cpp
common/utils.cpp
common/version_metadata.cpp
common/world_state.cpp
components/arm.cpp
components/base.cpp
Expand Down Expand Up @@ -140,6 +141,7 @@ target_sources(viamsdk
../../viam/sdk/common/proto_type.hpp
../../viam/sdk/common/service_helper.hpp
../../viam/sdk/common/utils.hpp
../../viam/sdk/common/version_metadata.hpp
../../viam/sdk/common/world_state.hpp
../../viam/sdk/components/arm.hpp
../../viam/sdk/components/base.hpp
Expand Down
53 changes: 53 additions & 0 deletions src/viam/sdk/common/version_metadata.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <viam/sdk/common/version_metadata.hpp>

#include <array>
#include <sstream>

#include <viam/sdk/common/private/version_metadata.hpp>

namespace viam {
namespace sdk {

std::string sdk_version() {
static const std::string result = [] {
std::string version_metadata(impl::k_version);
version_metadata.erase(0, version_metadata.find(';') + 1);
return version_metadata.substr(0, version_metadata.find(';'));
}();

return result;
}

int get_sub_version(int which) {
static const std::array<int, 3> components = [] {
std::array<int, 3> result;
auto version = sdk_version();
version.erase(0, version.find_first_not_of('v'));

std::string substr;
std::stringstream ss(version);

for (int i = 0; i < 3; ++i) {
getline(ss, substr, '.');
result[i] = std::stoi(substr);
}

return result;
}();

return components[which];
}

int sdk_major_version() {
return get_sub_version(0);
}

int sdk_minor_version() {
return get_sub_version(1);
}

int sdk_patch_version() {
return get_sub_version(2);
}
} // namespace sdk
} // namespace viam
14 changes: 14 additions & 0 deletions src/viam/sdk/common/version_metadata.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <string>

namespace viam {
namespace sdk {

std::string sdk_version();
int sdk_major_version();
int sdk_minor_version();
int sdk_patch_version();

} // namespace sdk
} // namespace viam
15 changes: 15 additions & 0 deletions src/viam/sdk/tests/test_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <viam/api/common/v1/common.pb.h>

#include <viam/sdk/common/utils.hpp>
#include <viam/sdk/common/version_metadata.hpp>
#include <viam/sdk/tests/test_utils.hpp>

namespace viam {
Expand Down Expand Up @@ -115,6 +116,20 @@ BOOST_AUTO_TEST_CASE(test_from_dm_from_extra) {
BOOST_CHECK_EQUAL(from_dm_from_extra(map), false);
}

BOOST_AUTO_TEST_CASE(test_version_metadata) {
// we don't want to check the specific values because they're going to be changing,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we get the sdk_version() string here, and then check that it matches

std::to_string(sdk_major_version()) + "." + std::to_string(sdk_minor_version()) + "." + std::to_string(sdk_patch_version());

// but we want to confirm that the parsing works and extracts an int value successfully.
int major = sdk_major_version();
int minor = sdk_minor_version();
int patch = sdk_patch_version();

std::string version_constructed =
"v" + std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(patch);
std::string version = sdk_version();

BOOST_CHECK_EQUAL(version_constructed, version);
}

BOOST_AUTO_TEST_SUITE_END()

} // namespace sdktests
Expand Down