Skip to content

Commit

Permalink
Add helpers for advanced users
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbin committed Jan 10, 2024
1 parent 027e57e commit d40c193
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
10 changes: 10 additions & 0 deletions lib/grpc-protoc-plugin/grpc_protoc_plugin.mli
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ module Server_rpc : sig
end

val handlers : 'a list -> ('a, _) Grpc.Rpc.Handlers.t

(** {1 Advanced users API}
The following functions are meant for advanced users only. *)

val service_spec :
(module S with type Request.t = 'request and type Response.t = 'response) ->
Grpc.Rpc.Service_spec.t
(** This is the function used by this module to extract and build a gRPC service
spec from the information generated by [ocaml_protoc_plugin]. *)
19 changes: 15 additions & 4 deletions lib/grpc-protoc/grpc_protoc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ let decode (type a) (decode : Pbrt.Decoder.t -> a) buffer =
let decoder = Pbrt.Decoder.of_string buffer in
decode decoder

let client_service_spec (rpc : _ Pbrt_services.Client.rpc) =
{
Grpc.Rpc.Service_spec.package = rpc.package;
service_name = rpc.service_name;
}

module Client_rpc = struct
let make (type request response)
(rpc : (request, _, response, _) Pbrt_services.Client.rpc) ~request_mode
~response_mode =
{
Grpc.Rpc.Client_rpc.service_spec =
{ package = rpc.package; service_name = rpc.service_name };
Grpc.Rpc.Client_rpc.service_spec = client_service_spec rpc;
rpc_name = rpc.rpc_name;
encode_request = encode rpc.encode_pb_req;
decode_response = decode rpc.decode_pb_res;
Expand Down Expand Up @@ -50,6 +55,12 @@ module Server_rpc = struct
make rpc ~request_mode:Stream ~response_mode:Stream
end

let handlers { Pbrt_services.Server.package; service_name; handlers } =
let server_service_spec
{ Pbrt_services.Server.package; service_name; handlers = _ } =
{ Grpc.Rpc.Service_spec.package; service_name }

let handlers
({ Pbrt_services.Server.package = _; service_name = _; handlers } as server)
=
Grpc.Rpc.Handlers.With_service_spec
{ service_spec = { package; service_name }; handlers }
{ service_spec = server_service_spec server; handlers }
12 changes: 12 additions & 0 deletions lib/grpc-protoc/grpc_protoc.mli
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,15 @@ module Server_rpc : sig
end

val handlers : 'a Pbrt_services.Server.t -> (_, 'a) Grpc.Rpc.Handlers.t

(** {1 Advanced users API}
The following functions are meant for advanced users only. *)

val client_service_spec : _ Pbrt_services.Client.rpc -> Grpc.Rpc.Service_spec.t
(** This is the function used by this module to extract and build a gRPC service
spec from the information generated by [ocaml_protoc] on the client side. *)

val server_service_spec : _ Pbrt_services.Server.t -> Grpc.Rpc.Service_spec.t
(** This is the function used by this module to extract and build a gRPC service
spec from the information generated by [ocaml_protoc] on the server side. *)

0 comments on commit d40c193

Please sign in to comment.