ores.cpp.nats-sub-registrar.nats_registrar_implementation
Per-entity NATS sub-registrar implementation. Defines
register_<entity>_handlers() which creates the handler and wires its
NATS subjects. nats-sub-registrar profile.
Each entity's registrar includes only its own handler header, so a compile failure in one entity's handler cannot block the rest of the service.
Model flags consumed by this template:
no_history(bool, Flags) — skip the history subject subscription. Set on entities whose service has noget_<entity>_historymethod.list_only(bool, Flags) — wire only the list subject. Set on read-only lookup entities.read_for_cache(bool, Flags) — wire the generatedread_<entity_plural>_for_cachesubject to the handler'sread_for_cachemethod. Requireshas_tenant_id. This is the standard archetype for the bulk cache-warming read.partymotivated this archetype but does not use it yet — it still wires its own hand-writtenread_parties_for_cachevia the custom-subscription paste point below, pending a fix to its codegen drift.
Custom-includes paste point: the marker
<<paste:068323E2-D722-46F1-8B86-46BD32426003>> is emitted after the standard
protocol include so an entity can pull in extra headers (e.g. a split
<entity>_history_protocol.hpp). Provide them as
#+begin_src cpp :name custom_includes :implements 068323E2-D722-46F1-8B86-46BD32426003.
Custom-subscription paste point: the marker
<<paste:FB267332-79AB-4873-AFDA-CEEE68A0F398>> is emitted just before the
return so an entity can wire hand-crafted subjects (beyond the standard
list/save/remove/history) that are not modelled by codegen. Provide them in
the entity model as
#+begin_src cpp :name custom_subscriptions :implements FB267332-79AB-4873-AFDA-CEEE68A0F398;
entities without such a block collapse the marker to nothing. Example:
party wires its read_parties_for_cache subject this way.
See the Template variable reference for the complete list of available variables and their semantics.
Template
The full template source. Edit here and re-tangle with
compass build --direct tangle_codegen_templates to regenerate
library/templates/cpp_nats_registrar.cpp.mustache.
{{! GENERATED FILE — tangled from projects/ores.codegen/library/templates/ores.cpp.nats-sub-registrar.nats_registrar_implementation.org. Edit the org source. }}
{{{cpp_license}}}
{{#domain_entity}}
#include "ores.{{component_core}}/messaging/{{entity_singular}}_registrar.hpp"
#include "ores.{{component_core}}/messaging/{{entity_singular}}_handler.hpp"
#include "ores.{{component_include}}/messaging/{{entity_singular}}_protocol.hpp"
<<paste:068323E2-D722-46F1-8B86-46BD32426003>>
#include <memory>
namespace ores::{{component}}::messaging {
namespace {
static constexpr std::string_view queue_group = "ores.{{component}}.service";
} // namespace
std::vector<ores::nats::service::subscription>
register_{{entity_singular}}_handlers(ores::nats::service::client& nats,
ores::database::context ctx,
std::optional<ores::security::jwt::jwt_authenticator> verifier) {
std::vector<ores::nats::service::subscription> subs;
auto h = std::make_shared<{{entity_singular}}_handler>(nats, std::move(ctx), std::move(verifier));
{{#list_only}}
subs.push_back(nats.queue_subscribe(
get_{{entity_plural}}_request::nats_subject, queue_group,
[h](ores::nats::message msg) { h->list(std::move(msg)); }));
{{/list_only}}
{{^list_only}}
subs.push_back(nats.queue_subscribe(
get_{{entity_plural}}_request::nats_subject, queue_group,
[h](ores::nats::message msg) { h->list(std::move(msg)); }));
subs.push_back(nats.queue_subscribe(
save_{{entity_singular}}_request::nats_subject, queue_group,
[h](ores::nats::message msg) { h->save(std::move(msg)); }));
subs.push_back(nats.queue_subscribe(
delete_{{entity_singular}}_request::nats_subject, queue_group,
[h](ores::nats::message msg) { h->remove(std::move(msg)); }));
{{^no_history}}
subs.push_back(nats.queue_subscribe(
get_{{entity_singular}}_history_request::nats_subject, queue_group,
[h](ores::nats::message msg) { h->history(std::move(msg)); }));
{{/no_history}}
{{/list_only}}
{{#extra_list_requests}}
subs.push_back(nats.queue_subscribe(
get_{{entity_plural}}_{{name_suffix}}_request::nats_subject, queue_group,
[h](ores::nats::message msg) { h->list_{{name_suffix}}(std::move(msg)); }));
{{/extra_list_requests}}
{{#has_parent_id}}
subs.push_back(nats.queue_subscribe(
get_{{entity_singular}}_hierarchy_request::nats_subject, queue_group, [h](ores::nats::message msg) {
h->hierarchy(std::move(msg));
}));
{{/has_parent_id}}
{{#read_for_cache}}
subs.push_back(nats.queue_subscribe(
read_{{entity_plural}}_for_cache_request::nats_subject, queue_group,
[h](ores::nats::message msg) { h->read_for_cache(std::move(msg)); }));
{{/read_for_cache}}
<<paste:FB267332-79AB-4873-AFDA-CEEE68A0F398>>
return subs;
}
} // namespace ores::{{component}}::messaging
{{/domain_entity}}
{{#junction}}
#include "ores.{{component_core}}/messaging/{{name_singular}}_registrar.hpp"
#include "ores.{{component_core}}/messaging/{{name_singular}}_handler.hpp"
#include "ores.{{component_include}}/messaging/{{name_singular}}_protocol.hpp"
#include <memory>
namespace ores::{{component}}::messaging {
namespace {
static constexpr std::string_view queue_group = "ores.{{component}}.service";
} // namespace
std::vector<ores::nats::service::subscription>
register_{{name_singular}}_handlers(ores::nats::service::client& nats,
ores::database::context ctx,
std::optional<ores::security::jwt::jwt_authenticator> verifier) {
std::vector<ores::nats::service::subscription> subs;
auto h = std::make_shared<{{name_singular}}_handler>(nats, std::move(ctx), std::move(verifier));
{{#left.list_by}}
subs.push_back(nats.queue_subscribe(
get_{{name}}_by_{{left.column_short}}_request::nats_subject, queue_group,
[h](ores::nats::message msg) { h->list_by_{{left.column_short}}(std::move(msg)); }));
{{/left.list_by}}
{{#right.list_by}}
subs.push_back(nats.queue_subscribe(
get_{{name}}_by_{{right.column_short}}_request::nats_subject, queue_group,
[h](ores::nats::message msg) { h->list_by_{{right.column_short}}(std::move(msg)); }));
{{/right.list_by}}
return subs;
}
} // namespace ores::{{component}}::messaging
{{/junction}}
{{#entity}}
#include "ores.{{component_core}}/messaging/{{entity_singular}}_registrar.hpp"
#include "ores.{{component_core}}/messaging/{{entity_singular}}_handler.hpp"
#include "ores.{{component_include}}/messaging/{{entity_singular}}_protocol.hpp"
<<paste:068323E2-D722-46F1-8B86-46BD32426003>>
#include <memory>
namespace ores::{{component}}::messaging {
namespace {
static constexpr std::string_view queue_group = "ores.{{component}}.service";
} // namespace
std::vector<ores::nats::service::subscription>
register_{{entity_singular}}_handlers(ores::nats::service::client& nats,
ores::database::context ctx,
std::optional<ores::security::jwt::jwt_authenticator> verifier) {
std::vector<ores::nats::service::subscription> subs;
auto h = std::make_shared<{{entity_singular}}_handler>(nats, std::move(ctx), std::move(verifier));
{{#list_only}}
subs.push_back(nats.queue_subscribe(
get_{{entity_plural}}_request::nats_subject, queue_group,
[h](ores::nats::message msg) { h->list(std::move(msg)); }));
{{/list_only}}
{{^list_only}}
subs.push_back(nats.queue_subscribe(
get_{{entity_plural}}_request::nats_subject, queue_group,
[h](ores::nats::message msg) { h->list(std::move(msg)); }));
subs.push_back(nats.queue_subscribe(
save_{{entity_singular}}_request::nats_subject, queue_group,
[h](ores::nats::message msg) { h->save(std::move(msg)); }));
subs.push_back(nats.queue_subscribe(
delete_{{entity_singular}}_request::nats_subject, queue_group,
[h](ores::nats::message msg) { h->remove(std::move(msg)); }));
{{^no_history}}
subs.push_back(nats.queue_subscribe(
get_{{entity_singular}}_history_request::nats_subject, queue_group,
[h](ores::nats::message msg) { h->history(std::move(msg)); }));
{{/no_history}}
{{/list_only}}
{{#extra_list_requests}}
subs.push_back(nats.queue_subscribe(
get_{{entity_plural}}_{{name_suffix}}_request::nats_subject, queue_group,
[h](ores::nats::message msg) { h->list_{{name_suffix}}(std::move(msg)); }));
{{/extra_list_requests}}
<<paste:FB267332-79AB-4873-AFDA-CEEE68A0F398>>
return subs;
}
} // namespace ores::{{component}}::messaging
{{/entity}}
See also
- Parent facet: ores.cpp.nats-sub-registrar
- Template variable reference
- ores.cpp.nats-sub-registrar.nats_registrar_header — the paired header.
- ores.cpp.nats-handler.nats_handler_header — the handler this sub-registrar includes.