Skip to content

Commit

Permalink
Merge pull request #184 from mransan/proto3-optional
Browse files Browse the repository at this point in the history
Proto3 optional
  • Loading branch information
c-cube authored Aug 16, 2022
2 parents 1ab501a + ae0ce80 commit 06a0fd6
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ ocaml_protoc.native
.merlin

*.orig
_opam
4 changes: 2 additions & 2 deletions dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(env
(_
(flags -bin-annot -annot -safe-string -strict-sequence
-keep-locs -no-alias-deps -w A-4-42-41-48-70)))
(flags -bin-annot -annot -strict-sequence -keep-locs -no-alias-deps -w
A-4-42-41-48-70)))
14 changes: 8 additions & 6 deletions src/compilerlib/pb_parsing_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ let verify_syntax2 proto =
let finalize_syntax3 proto =
let {Pt.messages; enums; _ } = proto in

(* make sure there are no `Required` or `Optional`` fields
* in messages *)

(* make sure there are no `Required` fields
in messages. Optional is now allowed, but default values might
not be. *)
let verify_no_default_field_options field_name message_name field_options =
match Pb_option.get field_options "default" with
| None -> ()
Expand Down Expand Up @@ -317,9 +317,8 @@ let finalize_syntax3 proto =
field_options; _} = field in
let field =
match field_label with
| `Required
| `Optional -> E.invalid_proto3_field_label
~field_name ~message_name
| `Required ->
E.invalid_proto3_field_label ~field_name ~message_name
| `Repeated ->
begin match field_type with
(* only builtin type of varint int64, int32 encoding
Expand All @@ -333,6 +332,9 @@ let finalize_syntax3 proto =
{field with Pt.field_options}
| _ -> field
end
| `Optional ->
(* optional is now allowed *)
field
| `Nolabel -> field
in
verify_no_default_field_options field_name message_name field_options;
Expand Down
22 changes: 22 additions & 0 deletions src/tests/integration-tests/dune
Original file line number Diff line number Diff line change
Expand Up @@ -739,3 +739,25 @@
test27_pp
)
(libraries test_util unix))

(rule
(targets
test_proto3_optional_types.ml
test_proto3_optional_pb.ml
test_proto3_optional_pp.ml
test_proto3_optional_types.mli
test_proto3_optional_pb.mli
test_proto3_optional_pp.mli
)
(deps (:proto test_proto3_optional.proto) ../../include/ocaml-protoc/ocamloptions.proto)
(action (run ocaml-protoc
-binary -pp
-ml_out ./
%{proto}))
)

(executable
(name test_proto3_optional)
(libraries pbrt)
(modules test_proto3_optional_types test_proto3_optional
test_proto3_optional_pb test_proto3_optional_pp))
19 changes: 19 additions & 0 deletions src/tests/integration-tests/test_proto3_optional.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

open Test_proto3_optional_types
open Test_proto3_optional_pp
module Pb = Test_proto3_optional_pb

let foo1: foo = {
x=1l;
y=None;
name=Some "hello";
}

let () =
let enc = Pbrt.Encoder.create() in
Pb.encode_foo foo1 enc ;
let s = Pbrt.Encoder.to_string enc in
let dec = Pbrt.Decoder.of_string s in
let foo1' = Pb.decode_foo dec in
assert (foo1 = foo1')

9 changes: 9 additions & 0 deletions src/tests/integration-tests/test_proto3_optional.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

syntax="proto3";

message Foo {
int32 x = 1;
optional int32 y = 2;
optional string name = 3;
}

0 comments on commit 06a0fd6

Please sign in to comment.