Skip to content

Commit

Permalink
Use specs::Platform in RepoData
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Aug 3, 2023
1 parent 1031d0b commit 059ceda
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
13 changes: 13 additions & 0 deletions libmamba/include/mamba/specs/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <optional>
#include <string_view>

#include <nlohmann/json_fwd.hpp>

namespace mamba::specs
{
enum class Platform
Expand Down Expand Up @@ -46,5 +48,16 @@ namespace mamba::specs
*/
auto build_platform() -> Platform;
auto build_platform_name() -> std::string_view;

/**
* Serialize to JSON string.
*/
void to_json(nlohmann::json& j, const Platform& p);

/**
* Deserialize from JSON string.
*/
void from_json(const nlohmann::json& j, Platform& p);

}
#endif
3 changes: 2 additions & 1 deletion libmamba/include/mamba/specs/repo_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <nlohmann/json_fwd.hpp>

#include "mamba/specs/platform.hpp"
#include "mamba/specs/version.hpp"

namespace mamba::specs
Expand Down Expand Up @@ -169,7 +170,7 @@ namespace mamba::specs
struct ChannelInfo
{
/** The channel's subdirectory. */
std::string subdir = {};
Platform subdir = {};
};

/** Serialize to JSON. */
Expand Down
21 changes: 21 additions & 0 deletions libmamba/src/specs/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <cassert>
#include <string>

#include <fmt/format.h>
#include <nlohmann/json.hpp>

#include "mamba/core/util_string.hpp"
#include "mamba/specs/platform.hpp"

Expand Down Expand Up @@ -146,4 +149,22 @@ namespace mamba::specs
{
return platform_name(build_platform());
}

void to_json(nlohmann::json& j, const Platform& p)
{
j = platform_name(p);
}

void from_json(const nlohmann::json& j, Platform& p)
{
const auto j_str = j.get<std::string_view>();
if (const auto maybe = platform_parse(j_str))
{
p = *maybe;
}
else
{
throw std::invalid_argument(fmt::format("Invalid platform: {}", j_str));
}
}
}
8 changes: 4 additions & 4 deletions libmamba/tests/src/specs/test_repo_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ TEST_SUITE("repo_data")
{
auto data = RepoData();
data.version = 1;
data.info = ChannelInfo{ /* .subdir= */ "linux-64" };
data.info = ChannelInfo{ /* .subdir= */ Platform::linux_64 };
data.packages = {
{ "mamba-1.0-h12345.tar.bz2", RepoDataPackage{ "mamba" } },
{ "conda-1.0-h54321.tar.bz2", RepoDataPackage{ "conda" } },
Expand All @@ -96,7 +96,7 @@ TEST_SUITE("repo_data")

const nl::json j = data;
CHECK_EQ(j.at("version"), data.version);
CHECK_EQ(j.at("info").at("subdir"), data.info.value().subdir);
CHECK_EQ(j.at("info").at("subdir"), platform_name(data.info.value().subdir));
CHECK_EQ(
j.at("packages").at("mamba-1.0-h12345.tar.bz2"),
data.packages.at("mamba-1.0-h12345.tar.bz2")
Expand All @@ -112,7 +112,7 @@ TEST_SUITE("repo_data")
{
auto j = nl::json::object();
j["version"] = 1;
j["info"]["subdir"] = "somedir";
j["info"]["subdir"] = "osx-arm64";
j["packages"]["mamba-1.0-h12345.tar.bz2"]["name"] = "mamba";
j["packages"]["mamba-1.0-h12345.tar.bz2"]["version"] = "1.1.0";
j["packages"]["mamba-1.0-h12345.tar.bz2"]["build"] = "foo1";
Expand All @@ -128,7 +128,7 @@ TEST_SUITE("repo_data")
REQUIRE(data.version.has_value());
CHECK_EQ(data.version, j["version"]);
REQUIRE(data.info.has_value());
CHECK_EQ(data.info.value().subdir, j["info"]["subdir"]);
CHECK_EQ(platform_name(data.info.value().subdir), j["info"]["subdir"]);
CHECK_EQ(
data.packages.at("mamba-1.0-h12345.tar.bz2").name,
j["packages"]["mamba-1.0-h12345.tar.bz2"]["name"]
Expand Down

0 comments on commit 059ceda

Please sign in to comment.