Skip to content

Commit

Permalink
Add example on howe to use proto2 extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
andersfugmann committed Jan 6, 2020
1 parent 4f03eb6 commit 9fa9fbb
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 20 deletions.
46 changes: 26 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,37 +211,43 @@ Below is an example on how to set and get extension fields


```protobuf
// File ext.proto
// file: ext.proto
syntax = "proto2";
message Foo {
required uint32 i = 1;
extensions 100 to 200;
}
extend Foo {
option uint32 bar = 128;
option string baz = 128;
optional uint32 bar = 128;
optional string baz = 129;
}
```

```ocaml
(* test.ml *)
(* file: test.ml *)
open Extensions
(* Set extensions *)
let foo = Foo.{ extensions' = Ocaml_protoc_plugin.Extensions.default }
let foo_with_bar = Bar.set foo (Some 42) in
let foo_with_baz = Bar.set foo (Some "Test String") in
let foo_with_bar_baz = Bar.set foo_with_bar (Some "Test String") in
(* Get extensions *)
let open Ocaml_protoc_plugin.Result in
Bar.get foo_with_bar >>= fun bar ->
Bar.get foo_with_baz >>= fun baz ->
assert (bar = Some 42);
assert (baz = Some "Test String");
Bar.get foo_with_bar_baz >>= fun bar' ->
Bar.get foo_with_bar_baz >>= fun baz' ->
assert (bar' = Some 42);
assert (baz' = Some "Test String");
()
let _ =
let foo = Foo.{ i = 31; extensions' = Ocaml_protoc_plugin.Extensions.default } in
let foo_with_bar = Bar.set foo (Some 42) in
let foo_with_baz = Baz.set foo (Some "Test String") in
let foo_with_bar_baz = Baz.set foo_with_bar (Some "Test String") in
(* Get extensions *)
let open Ocaml_protoc_plugin.Result in
Bar.get foo_with_bar >>= fun bar ->
Baz.get foo_with_baz >>= fun baz ->
assert (bar = Some 42);
assert (baz = Some "Test String");
Bar.get foo_with_bar_baz >>= fun bar' ->
Baz.get foo_with_bar_baz >>= fun baz' ->
assert (bar' = Some 42);
assert (baz' = Some "Test String");
return ()
```
Extensions are replaced by proto3 `Any` type, and use is discouraged.

Expand Down
16 changes: 16 additions & 0 deletions examples/extensions/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(executable
(name test)
(libraries ocaml-protoc-plugin ocaml-protoc-plugin.google_types unix)
)

(rule
(targets extensions.ml)
(deps
(:proto extensions.proto))
(action
(run protoc -I . "--ocaml_out=:." %{proto})))

(alias
(name runtest)
(deps test.exe)
)
10 changes: 10 additions & 0 deletions examples/extensions/extensions.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto2";
message Foo {
required uint32 i = 1;
extensions 100 to 200;

}
extend Foo {
optional uint32 bar = 128;
optional string baz = 129;
}
20 changes: 20 additions & 0 deletions examples/extensions/test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
open Extensions

(* Set extensions *)
let _ =
let foo = Foo.{ i = 31; extensions' = Ocaml_protoc_plugin.Extensions.default } in
let foo_with_bar = Bar.set foo (Some 42) in
let foo_with_baz = Baz.set foo (Some "Test String") in
let foo_with_bar_baz = Baz.set foo_with_bar (Some "Test String") in

(* Get extensions *)
let open Ocaml_protoc_plugin.Result in
Bar.get foo_with_bar >>= fun bar ->
Baz.get foo_with_baz >>= fun baz ->
assert (bar = Some 42);
assert (baz = Some "Test String");
Bar.get foo_with_bar_baz >>= fun bar' ->
Baz.get foo_with_bar_baz >>= fun baz' ->
assert (bar' = Some 42);
assert (baz' = Some "Test String");
return ()

0 comments on commit 9fa9fbb

Please sign in to comment.