diff --git a/README.md b/README.md index 20ba6fcb..a3e53734 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/examples/extensions/dune b/examples/extensions/dune new file mode 100644 index 00000000..095dd363 --- /dev/null +++ b/examples/extensions/dune @@ -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) +) diff --git a/examples/extensions/extensions.proto b/examples/extensions/extensions.proto new file mode 100644 index 00000000..15593601 --- /dev/null +++ b/examples/extensions/extensions.proto @@ -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; +} diff --git a/examples/extensions/test.ml b/examples/extensions/test.ml new file mode 100644 index 00000000..cfa7d832 --- /dev/null +++ b/examples/extensions/test.ml @@ -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 ()