|
| 1 | +#include "pb_stub.hpp" |
| 2 | + |
| 3 | +#include <cstdint> |
| 4 | +#include <functional> |
| 5 | +#include <string> |
| 6 | + |
| 7 | +// Commit 4: [[pb::service("Name")]] — a record marked this way emits as |
| 8 | +// a proto3 `service Name { ... }` block. Each std::function<Resp(Req)> |
| 9 | +// member becomes one `rpc <member-name>(Req) returns (Resp);` line. |
| 10 | +// Non-function fields would be skipped with a diagnostic; this fixture |
| 11 | +// keeps the struct RPC-handler-only. |
| 12 | +// |
| 13 | +// The Request / Response struct types are walked as dependencies so |
| 14 | +// they emit as proto3 `message` blocks BEFORE the service block. |
| 15 | + |
| 16 | +namespace acme::users { |
| 17 | + |
| 18 | +// Request / response messages. Plain pb::field-only structs. |
| 19 | +struct [[pb::doc("user-id lookup request")]] get_user_req_t { |
| 20 | + [[pb::field(1)]] std::int64_t user_id; |
| 21 | +}; |
| 22 | +struct [[pb::doc("user-id lookup response")]] get_user_resp_t { |
| 23 | + [[pb::field(1)]] std::string display_name; |
| 24 | + [[pb::field(2)]] std::int64_t created_ns; |
| 25 | +}; |
| 26 | + |
| 27 | +struct list_users_req_t { |
| 28 | + [[pb::field(1)]] std::int32_t page; |
| 29 | + [[pb::field(2)]] std::int32_t page_size; |
| 30 | +}; |
| 31 | +struct list_users_resp_t { |
| 32 | + [[pb::field(1)]] std::int32_t total; |
| 33 | +}; |
| 34 | + |
| 35 | +struct auth_req_t { [[pb::field(1)]] std::string token; }; |
| 36 | +struct auth_resp_t { [[pb::field(1)]] bool ok; }; |
| 37 | + |
| 38 | +// The service definition. No data fields — only RPC handlers. |
| 39 | +struct [[pb::service("UserService"), |
| 40 | + pb::doc("user-account RPC surface")]] |
| 41 | +user_service_t { |
| 42 | + std::function<get_user_resp_t(get_user_req_t)> get_user; |
| 43 | + std::function<list_users_resp_t(list_users_req_t)> list_users; |
| 44 | + std::function<auth_resp_t(auth_req_t)> authenticate; |
| 45 | +}; |
| 46 | + |
| 47 | +} // namespace acme::users |
| 48 | + |
| 49 | +void use() { |
| 50 | + // Touching the messages teaches h5cpp-compiler the requests/responses |
| 51 | + // exist as user records — they emit as `message` blocks before the |
| 52 | + // service that references them. |
| 53 | + acme::users::get_user_req_t q1{}; (void)pb::encode(q1); |
| 54 | + acme::users::get_user_resp_t r1{}; (void)pb::encode(r1); |
| 55 | + acme::users::list_users_req_t q2{}; (void)pb::encode(q2); |
| 56 | + acme::users::list_users_resp_t r2{}; (void)pb::encode(r2); |
| 57 | + acme::users::auth_req_t q3{}; (void)pb::encode(q3); |
| 58 | + acme::users::auth_resp_t r3{}; (void)pb::encode(r3); |
| 59 | + acme::users::user_service_t svc{}; (void)pb::encode(svc); |
| 60 | +} |
0 commit comments