Plugin: add reusable module descriptor
This commit is contained in:
1 parent
053ee5b296
commit
fb5388a7ee
2 files changed
+208
No files matched your search
@@ -0,0 +1,130 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Void/Data/Json.hpp>
|
||||||
|
#include <Void/Functional/Result.hpp>
|
||||||
|
#include <Void/Plugin/Plugin.hpp>
|
||||||
|
#include <Void/Service/Registry.hpp>
|
||||||
|
#include <Void/Text/Semver.hpp>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace Void::Plugin
|
||||||
|
{
|
||||||
|
inline constexpr std::uint32_t MODULE_DESCRIPTOR_VERSION = 1;
|
||||||
|
|
||||||
|
enum class ActivationPolicy : std::uint8_t {
|
||||||
|
Reloadable,
|
||||||
|
RestartRequired,
|
||||||
|
InstallerRequired,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ModuleDependency {
|
||||||
|
std::string module_id;
|
||||||
|
std::string version_range;
|
||||||
|
std::string abi_hash;
|
||||||
|
bool optional{false};
|
||||||
|
|
||||||
|
[[nodiscard]] bool accepts(const Semver::Version& version) const
|
||||||
|
{
|
||||||
|
const auto range = Semver::VersionRange::parse(version_range);
|
||||||
|
return range.has_value() && range->contains(version);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ModuleReadiness {
|
||||||
|
bool ready{false};
|
||||||
|
std::string detail;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ModuleHost {
|
||||||
|
Service::ServiceRegistry& services;
|
||||||
|
};
|
||||||
|
|
||||||
|
class IModule : public IPlugin
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
[[nodiscard]] virtual Result::Result<void>
|
||||||
|
activate(ModuleHost& host) = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual Result::Result<void>
|
||||||
|
quiesce(std::chrono::steady_clock::time_point deadline)
|
||||||
|
{
|
||||||
|
static_cast<void>(deadline);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] virtual Json::Value checkpoint() const
|
||||||
|
{
|
||||||
|
return Json::object();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] virtual Result::Result<void>
|
||||||
|
restore(const Json::Value& state)
|
||||||
|
{
|
||||||
|
static_cast<void>(state);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] virtual ModuleReadiness readiness() const = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual Result::Result<void> deactivate()
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using module_factory_fn = std::unique_ptr<IModule> (*)();
|
||||||
|
|
||||||
|
struct ModuleDescriptor {
|
||||||
|
std::size_t struct_size{sizeof(ModuleDescriptor)};
|
||||||
|
std::uint32_t descriptor_version{MODULE_DESCRIPTOR_VERSION};
|
||||||
|
std::string module_id;
|
||||||
|
Semver::Version component_version;
|
||||||
|
std::string abi_hash;
|
||||||
|
Json::Value abi_contract{Json::object()};
|
||||||
|
std::vector<ModuleDependency> dependencies;
|
||||||
|
ActivationPolicy activation_policy{
|
||||||
|
ActivationPolicy::RestartRequired};
|
||||||
|
module_factory_fn create{};
|
||||||
|
|
||||||
|
[[nodiscard]] Result::Result<void> validate() const
|
||||||
|
{
|
||||||
|
if (struct_size < sizeof(ModuleDescriptor)) {
|
||||||
|
return Result::fail("module descriptor is truncated");
|
||||||
|
}
|
||||||
|
if (descriptor_version != MODULE_DESCRIPTOR_VERSION) {
|
||||||
|
return Result::fail(
|
||||||
|
"unsupported module descriptor version");
|
||||||
|
}
|
||||||
|
if (module_id.empty()) {
|
||||||
|
return Result::fail("module id is required");
|
||||||
|
}
|
||||||
|
if (abi_hash.empty() || !abi_contract.is_object()) {
|
||||||
|
return Result::fail(
|
||||||
|
"module ABI hash and object contract are required");
|
||||||
|
}
|
||||||
|
if (create == nullptr) {
|
||||||
|
return Result::fail("module factory is required");
|
||||||
|
}
|
||||||
|
for (const auto& dependency : dependencies) {
|
||||||
|
if (dependency.module_id.empty() ||
|
||||||
|
!Semver::VersionRange::parse(
|
||||||
|
dependency.version_range)) {
|
||||||
|
return Result::fail(
|
||||||
|
"module dependency id or version range is invalid");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using module_query_fn = const ModuleDescriptor* (*)();
|
||||||
|
|
||||||
|
} // namespace Void::Plugin
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
#include <Void/Plugin/Module.hpp>
|
||||||
|
#include <Void/Testing/Assert.hpp>
|
||||||
|
#include <Void/Testing/Test.hpp>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
using namespace Void::Plugin;
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
class TestModule final : public IModule
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
std::string_view name() const noexcept override
|
||||||
|
{
|
||||||
|
return "test.module";
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<void> activate(ModuleHost&) override
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
ModuleReadiness readiness() const override
|
||||||
|
{
|
||||||
|
return {.ready = true, .detail = "ready"};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unique_ptr<IModule> createTestModule()
|
||||||
|
{
|
||||||
|
return std::make_unique<TestModule>();
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
Void::Testing::Test module_descriptor_uses_void_contract_types()
|
||||||
|
{
|
||||||
|
const ModuleDescriptor descriptor{
|
||||||
|
.module_id = "test.module",
|
||||||
|
.component_version = {.major = 2, .minor = 0, .patch = 0},
|
||||||
|
.abi_hash = "sha256:test",
|
||||||
|
.abi_contract = Void::Json::object({{"schemaVersion", 1}}),
|
||||||
|
.dependencies =
|
||||||
|
{
|
||||||
|
{
|
||||||
|
.module_id = "echoion.api",
|
||||||
|
.version_range = "^2.0.0",
|
||||||
|
.abi_hash = "sha256:api",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
.activation_policy = ActivationPolicy::Reloadable,
|
||||||
|
.create = createTestModule,
|
||||||
|
};
|
||||||
|
|
||||||
|
Void::Testing::expect(descriptor.validate().has_value());
|
||||||
|
Void::Testing::expect(
|
||||||
|
descriptor.dependencies.front().accepts(Void::Semver::Version{
|
||||||
|
.major = 2,
|
||||||
|
.minor = 3,
|
||||||
|
.patch = 0,
|
||||||
|
.pre_release = {},
|
||||||
|
.build = {},
|
||||||
|
}));
|
||||||
|
Void::Testing::expect(
|
||||||
|
!descriptor.dependencies.front().accepts(Void::Semver::Version{
|
||||||
|
.major = 3,
|
||||||
|
.minor = 0,
|
||||||
|
.patch = 0,
|
||||||
|
.pre_release = {},
|
||||||
|
.build = {},
|
||||||
|
}));
|
||||||
|
|
||||||
|
auto module = descriptor.create();
|
||||||
|
Void::Testing::expect(module != nullptr);
|
||||||
|
Void::Testing::expect_eq(module->name(), std::string_view{"test.module"});
|
||||||
|
}
|
||||||
Reference in new issue
Block a user