Skip to content

Commit

Permalink
lia pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
stuqdog committed Sep 20, 2024
1 parent ca8039b commit fbb0c75
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
34 changes: 23 additions & 11 deletions src/viam/sdk/common/version_metadata.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <viam/sdk/common/version_metadata.hpp>

#include <array>
#include <sstream>

#include <viam/sdk/common/private/version_metadata.hpp>
Expand All @@ -8,22 +9,33 @@ namespace viam {
namespace sdk {

std::string sdk_version() {
std::string version_metadata(impl::k_version);
version_metadata.erase(0, version_metadata.find(';') + 1);
return version_metadata.substr(0, version_metadata.find(';'));
static 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) {
auto version = sdk_version();
version.erase(0, version.find_first_not_of('v'));
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);
}

std::string substr;
std::stringstream ss(version);
return result;
}();

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

int sdk_major_version() {
Expand Down
6 changes: 5 additions & 1 deletion src/viam/sdk/tests/test_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ BOOST_AUTO_TEST_CASE(test_version_metadata) {
int minor = sdk_minor_version();
int patch = sdk_patch_version();

BOOST_CHECK(true);
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()
Expand Down

0 comments on commit fbb0c75

Please sign in to comment.