-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper library for using typed-rpc with ocaml-protoc
- Loading branch information
Showing
5 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# This file is generated by dune, edit dune-project instead | ||
opam-version: "2.0" | ||
synopsis: "An implementation of gRPC using ocaml-protoc" | ||
description: | ||
"Functionality for building gRPC services and rpcs with `ocaml-protoc`" | ||
maintainer: ["Daniel Quernheim <[email protected]>"] | ||
authors: [ | ||
"Andrew Jeffery <[email protected]>" | ||
"Daniel Quernheim <[email protected]>" | ||
"Michael Bacarella <[email protected]>" | ||
"Sven Anderson <[email protected]>" | ||
"Tim McGilchrist <[email protected]>" | ||
"Wojtek Czekalski <[email protected]>" | ||
"dimitris.mostrous <[email protected]>" | ||
] | ||
license: "BSD-3-Clause" | ||
homepage: "https://github.com/dialohq/ocaml-grpc" | ||
doc: "https://dialohq.github.io/ocaml-grpc" | ||
bug-reports: "https://github.com/dialohq/ocaml-grpc/issues" | ||
depends: [ | ||
"dune" {>= "3.7"} | ||
"grpc" {= version} | ||
"ocaml-protoc" {>= "3.0"} | ||
"pbrt" {>= "3.0"} | ||
"pbrt_services" {>= "3.0"} | ||
"odoc" {with-doc} | ||
] | ||
build: [ | ||
["dune" "subst"] {dev} | ||
[ | ||
"dune" | ||
"build" | ||
"-p" | ||
name | ||
"-j" | ||
jobs | ||
"@install" | ||
"@runtest" {with-test} | ||
"@doc" {with-doc} | ||
] | ||
] | ||
dev-repo: "git+https://github.com/dialohq/ocaml-grpc.git" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(library | ||
(name grpc_protoc) | ||
(public_name grpc-protoc) | ||
(libraries grpc ocaml-protoc pbrt pbrt_services)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
let encode (type a) (encode : a -> Pbrt.Encoder.t -> unit) (a : a) = | ||
let encoder = Pbrt.Encoder.create () in | ||
encode a encoder; | ||
Pbrt.Encoder.to_string encoder | ||
|
||
let decode (type a) (decode : Pbrt.Decoder.t -> a) buffer = | ||
let decoder = Pbrt.Decoder.of_string buffer in | ||
decode decoder | ||
|
||
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 }; | ||
rpc_name = rpc.rpc_name; | ||
encode_request = encode rpc.encode_pb_req; | ||
decode_response = decode rpc.decode_pb_res; | ||
request_mode; | ||
response_mode; | ||
} | ||
|
||
let unary rpc = make rpc ~request_mode:Unary ~response_mode:Unary | ||
let client_streaming rpc = make rpc ~request_mode:Stream ~response_mode:Unary | ||
let server_streaming rpc = make rpc ~request_mode:Unary ~response_mode:Stream | ||
|
||
let bidirectional_streaming rpc = | ||
make rpc ~request_mode:Stream ~response_mode:Stream | ||
end | ||
|
||
module Server_rpc = struct | ||
let make (type request response) | ||
(rpc : (request, _, response, _) Pbrt_services.Server.rpc) ~request_mode | ||
~response_mode = | ||
{ | ||
Grpc.Rpc.Server_rpc.service_spec = None; | ||
rpc_name = rpc.name; | ||
decode_request = decode rpc.decode_pb_req; | ||
encode_response = encode rpc.encode_pb_res; | ||
request_mode; | ||
response_mode; | ||
} | ||
|
||
let unary rpc = make rpc ~request_mode:Unary ~response_mode:Unary | ||
let client_streaming rpc = make rpc ~request_mode:Stream ~response_mode:Unary | ||
let server_streaming rpc = make rpc ~request_mode:Unary ~response_mode:Stream | ||
|
||
let bidirectional_streaming rpc = | ||
make rpc ~request_mode:Stream ~response_mode:Stream | ||
end | ||
|
||
let handlers { Pbrt_services.Server.package; service_name; handlers } = | ||
Grpc.Rpc.Handlers.With_service_spec | ||
{ service_spec = { package; service_name }; handlers } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
(** A utility library for constructing gRPC specifications using [Ocaml_protoc]. | ||
This module is designed to work alongside [Ocaml_protoc] to generate gRPC | ||
stubs, as outlined in {!module:Grpc.Rpc}. It offers a collection of helper | ||
functions that construct gRPC specifications from the code produced by | ||
[Ocaml_protoc] based on the services defined in *.proto files. *) | ||
|
||
(** {1 Client side} *) | ||
|
||
module Client_rpc : sig | ||
val unary : | ||
( 'request, | ||
Pbrt_services.Value_mode.unary, | ||
'response, | ||
Pbrt_services.Value_mode.unary ) | ||
Pbrt_services.Client.rpc -> | ||
( 'request, | ||
Grpc.Rpc.Value_mode.unary, | ||
'response, | ||
Grpc.Rpc.Value_mode.unary ) | ||
Grpc.Rpc.Client_rpc.t | ||
|
||
val client_streaming : | ||
( 'request, | ||
Pbrt_services.Value_mode.stream, | ||
'response, | ||
Pbrt_services.Value_mode.unary ) | ||
Pbrt_services.Client.rpc -> | ||
( 'request, | ||
Grpc.Rpc.Value_mode.stream, | ||
'response, | ||
Grpc.Rpc.Value_mode.unary ) | ||
Grpc.Rpc.Client_rpc.t | ||
|
||
val server_streaming : | ||
( 'request, | ||
Pbrt_services.Value_mode.unary, | ||
'response, | ||
Pbrt_services.Value_mode.stream ) | ||
Pbrt_services.Client.rpc -> | ||
( 'request, | ||
Grpc.Rpc.Value_mode.unary, | ||
'response, | ||
Grpc.Rpc.Value_mode.stream ) | ||
Grpc.Rpc.Client_rpc.t | ||
|
||
val bidirectional_streaming : | ||
( 'request, | ||
Pbrt_services.Value_mode.stream, | ||
'response, | ||
Pbrt_services.Value_mode.stream ) | ||
Pbrt_services.Client.rpc -> | ||
( 'request, | ||
Grpc.Rpc.Value_mode.stream, | ||
'response, | ||
Grpc.Rpc.Value_mode.stream ) | ||
Grpc.Rpc.Client_rpc.t | ||
end | ||
|
||
(** {1 Server side} *) | ||
|
||
module Server_rpc : sig | ||
val unary : | ||
( 'request, | ||
Pbrt_services.Value_mode.unary, | ||
'response, | ||
Pbrt_services.Value_mode.unary ) | ||
Pbrt_services.Server.rpc -> | ||
( 'request, | ||
Grpc.Rpc.Value_mode.unary, | ||
'response, | ||
Grpc.Rpc.Value_mode.unary, | ||
unit ) | ||
Grpc.Rpc.Server_rpc.t | ||
|
||
val client_streaming : | ||
( 'request, | ||
Pbrt_services.Value_mode.stream, | ||
'response, | ||
Pbrt_services.Value_mode.unary ) | ||
Pbrt_services.Server.rpc -> | ||
( 'request, | ||
Grpc.Rpc.Value_mode.stream, | ||
'response, | ||
Grpc.Rpc.Value_mode.unary, | ||
unit ) | ||
Grpc.Rpc.Server_rpc.t | ||
|
||
val server_streaming : | ||
( 'request, | ||
Pbrt_services.Value_mode.unary, | ||
'response, | ||
Pbrt_services.Value_mode.stream ) | ||
Pbrt_services.Server.rpc -> | ||
( 'request, | ||
Grpc.Rpc.Value_mode.unary, | ||
'response, | ||
Grpc.Rpc.Value_mode.stream, | ||
unit ) | ||
Grpc.Rpc.Server_rpc.t | ||
|
||
val bidirectional_streaming : | ||
( 'request, | ||
Pbrt_services.Value_mode.stream, | ||
'response, | ||
Pbrt_services.Value_mode.stream ) | ||
Pbrt_services.Server.rpc -> | ||
( 'request, | ||
Grpc.Rpc.Value_mode.stream, | ||
'response, | ||
Grpc.Rpc.Value_mode.stream, | ||
unit ) | ||
Grpc.Rpc.Server_rpc.t | ||
end | ||
|
||
val handlers : 'a Pbrt_services.Server.t -> (_, 'a) Grpc.Rpc.Handlers.t |