Skip to content

Commit

Permalink
RSDK-8343 - create public version_metadata file (#290)
Browse files Browse the repository at this point in the history
Co-authored-by: lia <[email protected]>
  • Loading branch information
stuqdog and lia-viam authored Sep 23, 2024
1 parent 9b2a576 commit f152998
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
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,
// 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

0 comments on commit f152998

Please sign in to comment.