-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add expect tests for generated code in examples/
- Loading branch information
Showing
15 changed files
with
2,127 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,173 @@ | ||
|
||
(** Code for calculator.proto *) | ||
|
||
(* generated from "calculator.proto", do not edit *) | ||
|
||
|
||
|
||
(** {2 Types} *) | ||
|
||
type div_by_zero = unit | ||
|
||
type i32 = { | ||
value : int32; | ||
} | ||
|
||
type add_req = { | ||
a : int32; | ||
b : int32; | ||
} | ||
|
||
type add_all_req = { | ||
ints : int32 list; | ||
} | ||
|
||
type empty = unit | ||
|
||
|
||
(** {2 Basic values} *) | ||
|
||
val default_div_by_zero : unit | ||
(** [default_div_by_zero ()] is the default value for type [div_by_zero] *) | ||
|
||
val default_i32 : | ||
?value:int32 -> | ||
unit -> | ||
i32 | ||
(** [default_i32 ()] is the default value for type [i32] *) | ||
|
||
val default_add_req : | ||
?a:int32 -> | ||
?b:int32 -> | ||
unit -> | ||
add_req | ||
(** [default_add_req ()] is the default value for type [add_req] *) | ||
|
||
val default_add_all_req : | ||
?ints:int32 list -> | ||
unit -> | ||
add_all_req | ||
(** [default_add_all_req ()] is the default value for type [add_all_req] *) | ||
|
||
val default_empty : unit | ||
(** [default_empty ()] is the default value for type [empty] *) | ||
|
||
|
||
(** {2 Formatters} *) | ||
|
||
val pp_div_by_zero : Format.formatter -> div_by_zero -> unit | ||
(** [pp_div_by_zero v] formats v *) | ||
|
||
val pp_i32 : Format.formatter -> i32 -> unit | ||
(** [pp_i32 v] formats v *) | ||
|
||
val pp_add_req : Format.formatter -> add_req -> unit | ||
(** [pp_add_req v] formats v *) | ||
|
||
val pp_add_all_req : Format.formatter -> add_all_req -> unit | ||
(** [pp_add_all_req v] formats v *) | ||
|
||
val pp_empty : Format.formatter -> empty -> unit | ||
(** [pp_empty v] formats v *) | ||
|
||
|
||
(** {2 Protobuf Encoding} *) | ||
|
||
val encode_pb_div_by_zero : div_by_zero -> Pbrt.Encoder.t -> unit | ||
(** [encode_pb_div_by_zero v encoder] encodes [v] with the given [encoder] *) | ||
|
||
val encode_pb_i32 : i32 -> Pbrt.Encoder.t -> unit | ||
(** [encode_pb_i32 v encoder] encodes [v] with the given [encoder] *) | ||
|
||
val encode_pb_add_req : add_req -> Pbrt.Encoder.t -> unit | ||
(** [encode_pb_add_req v encoder] encodes [v] with the given [encoder] *) | ||
|
||
val encode_pb_add_all_req : add_all_req -> Pbrt.Encoder.t -> unit | ||
(** [encode_pb_add_all_req v encoder] encodes [v] with the given [encoder] *) | ||
|
||
val encode_pb_empty : empty -> Pbrt.Encoder.t -> unit | ||
(** [encode_pb_empty v encoder] encodes [v] with the given [encoder] *) | ||
|
||
|
||
(** {2 Protobuf Decoding} *) | ||
|
||
val decode_pb_div_by_zero : Pbrt.Decoder.t -> div_by_zero | ||
(** [decode_pb_div_by_zero decoder] decodes a [div_by_zero] binary value from [decoder] *) | ||
|
||
val decode_pb_i32 : Pbrt.Decoder.t -> i32 | ||
(** [decode_pb_i32 decoder] decodes a [i32] binary value from [decoder] *) | ||
|
||
val decode_pb_add_req : Pbrt.Decoder.t -> add_req | ||
(** [decode_pb_add_req decoder] decodes a [add_req] binary value from [decoder] *) | ||
|
||
val decode_pb_add_all_req : Pbrt.Decoder.t -> add_all_req | ||
(** [decode_pb_add_all_req decoder] decodes a [add_all_req] binary value from [decoder] *) | ||
|
||
val decode_pb_empty : Pbrt.Decoder.t -> empty | ||
(** [decode_pb_empty decoder] decodes a [empty] binary value from [decoder] *) | ||
|
||
|
||
(** {2 Protobuf YoJson Encoding} *) | ||
|
||
val encode_json_div_by_zero : div_by_zero -> Yojson.Basic.t | ||
(** [encode_json_div_by_zero v encoder] encodes [v] to to json *) | ||
|
||
val encode_json_i32 : i32 -> Yojson.Basic.t | ||
(** [encode_json_i32 v encoder] encodes [v] to to json *) | ||
|
||
val encode_json_add_req : add_req -> Yojson.Basic.t | ||
(** [encode_json_add_req v encoder] encodes [v] to to json *) | ||
|
||
val encode_json_add_all_req : add_all_req -> Yojson.Basic.t | ||
(** [encode_json_add_all_req v encoder] encodes [v] to to json *) | ||
|
||
val encode_json_empty : empty -> Yojson.Basic.t | ||
(** [encode_json_empty v encoder] encodes [v] to to json *) | ||
|
||
|
||
(** {2 JSON Decoding} *) | ||
|
||
val decode_json_div_by_zero : Yojson.Basic.t -> div_by_zero | ||
(** [decode_json_div_by_zero decoder] decodes a [div_by_zero] value from [decoder] *) | ||
|
||
val decode_json_i32 : Yojson.Basic.t -> i32 | ||
(** [decode_json_i32 decoder] decodes a [i32] value from [decoder] *) | ||
|
||
val decode_json_add_req : Yojson.Basic.t -> add_req | ||
(** [decode_json_add_req decoder] decodes a [add_req] value from [decoder] *) | ||
|
||
val decode_json_add_all_req : Yojson.Basic.t -> add_all_req | ||
(** [decode_json_add_all_req decoder] decodes a [add_all_req] value from [decoder] *) | ||
|
||
val decode_json_empty : Yojson.Basic.t -> empty | ||
(** [decode_json_empty decoder] decodes a [empty] value from [decoder] *) | ||
|
||
|
||
(** {2 Services} *) | ||
|
||
(** Calculator service *) | ||
module Calculator : sig | ||
open Pbrt_services | ||
open Pbrt_services.Value_mode | ||
|
||
module Client : sig | ||
|
||
val add : (add_req, unary, i32, unary) Client.rpc | ||
|
||
val add_all : (add_all_req, unary, i32, unary) Client.rpc | ||
|
||
val ping : (empty, unary, empty, unary) Client.rpc | ||
|
||
val get_pings : (empty, unary, i32, unary) Client.rpc | ||
end | ||
|
||
module Server : sig | ||
(** Produce a server implementation from handlers *) | ||
val make : | ||
add:((add_req, unary, i32, unary) Server.rpc -> 'handler) -> | ||
add_all:((add_all_req, unary, i32, unary) Server.rpc -> 'handler) -> | ||
ping:((empty, unary, empty, unary) Server.rpc -> 'handler) -> | ||
get_pings:((empty, unary, i32, unary) Server.rpc -> 'handler) -> | ||
unit -> 'handler Pbrt_services.Server.t | ||
end | ||
end |
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,105 @@ | ||
[@@@ocaml.warning "-27-30-39"] | ||
|
||
type person = { | ||
name : string; | ||
id : int32; | ||
email : string; | ||
phone : string list; | ||
} | ||
|
||
let rec default_person | ||
?name:((name:string) = "") | ||
?id:((id:int32) = 0l) | ||
?email:((email:string) = "") | ||
?phone:((phone:string list) = []) | ||
() : person = { | ||
name; | ||
id; | ||
email; | ||
phone; | ||
} | ||
|
||
type person_mutable = { | ||
mutable name : string; | ||
mutable id : int32; | ||
mutable email : string; | ||
mutable phone : string list; | ||
} | ||
|
||
let default_person_mutable () : person_mutable = { | ||
name = ""; | ||
id = 0l; | ||
email = ""; | ||
phone = []; | ||
} | ||
|
||
[@@@ocaml.warning "-27-30-39"] | ||
|
||
(** {2 Formatters} *) | ||
|
||
let rec pp_person fmt (v:person) = | ||
let pp_i fmt () = | ||
Pbrt.Pp.pp_record_field ~first:true "name" Pbrt.Pp.pp_string fmt v.name; | ||
Pbrt.Pp.pp_record_field ~first:false "id" Pbrt.Pp.pp_int32 fmt v.id; | ||
Pbrt.Pp.pp_record_field ~first:false "email" Pbrt.Pp.pp_string fmt v.email; | ||
Pbrt.Pp.pp_record_field ~first:false "phone" (Pbrt.Pp.pp_list Pbrt.Pp.pp_string) fmt v.phone; | ||
in | ||
Pbrt.Pp.pp_brk pp_i fmt () | ||
|
||
[@@@ocaml.warning "-27-30-39"] | ||
|
||
(** {2 Protobuf Encoding} *) | ||
|
||
let rec encode_pb_person (v:person) encoder = | ||
Pbrt.Encoder.string v.name encoder; | ||
Pbrt.Encoder.key 1 Pbrt.Bytes encoder; | ||
Pbrt.Encoder.int32_as_varint v.id encoder; | ||
Pbrt.Encoder.key 2 Pbrt.Varint encoder; | ||
Pbrt.Encoder.string v.email encoder; | ||
Pbrt.Encoder.key 3 Pbrt.Bytes encoder; | ||
Pbrt.List_util.rev_iter_with (fun x encoder -> | ||
Pbrt.Encoder.string x encoder; | ||
Pbrt.Encoder.key 4 Pbrt.Bytes encoder; | ||
) v.phone encoder; | ||
() | ||
|
||
[@@@ocaml.warning "-27-30-39"] | ||
|
||
(** {2 Protobuf Decoding} *) | ||
|
||
let rec decode_pb_person d = | ||
let v = default_person_mutable () in | ||
let continue__= ref true in | ||
while !continue__ do | ||
match Pbrt.Decoder.key d with | ||
| None -> ( | ||
v.phone <- List.rev v.phone; | ||
); continue__ := false | ||
| Some (1, Pbrt.Bytes) -> begin | ||
v.name <- Pbrt.Decoder.string d; | ||
end | ||
| Some (1, pk) -> | ||
Pbrt.Decoder.unexpected_payload "Message(person), field(1)" pk | ||
| Some (2, Pbrt.Varint) -> begin | ||
v.id <- Pbrt.Decoder.int32_as_varint d; | ||
end | ||
| Some (2, pk) -> | ||
Pbrt.Decoder.unexpected_payload "Message(person), field(2)" pk | ||
| Some (3, Pbrt.Bytes) -> begin | ||
v.email <- Pbrt.Decoder.string d; | ||
end | ||
| Some (3, pk) -> | ||
Pbrt.Decoder.unexpected_payload "Message(person), field(3)" pk | ||
| Some (4, Pbrt.Bytes) -> begin | ||
v.phone <- (Pbrt.Decoder.string d) :: v.phone; | ||
end | ||
| Some (4, pk) -> | ||
Pbrt.Decoder.unexpected_payload "Message(person), field(4)" pk | ||
| Some (_, payload_kind) -> Pbrt.Decoder.skip d payload_kind | ||
done; | ||
({ | ||
name = v.name; | ||
id = v.id; | ||
email = v.email; | ||
phone = v.phone; | ||
} : person) |
Oops, something went wrong.