Skip to content

Commit

Permalink
Add the ability to generate Protobuf "oneof"
Browse files Browse the repository at this point in the history
"piqi to-proto" can now generate Protobuf "oneof" from Piqi "variant" if
.protobuf-oneof <oneof-name> field is defined in the variant definition
  • Loading branch information
alavrik committed Apr 18, 2018
1 parent 6c513f9 commit 27157ca
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 20 deletions.
15 changes: 15 additions & 0 deletions piqi/piqi.protobuf.piqi
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,18 @@
]
]


.extend [
.typedef variant

% specifies the name for the protobuf "oneof", when present "piqi to-proto"
% wraps all variant options into protobuf's oneof construct:
%
% oneof <name> { <options> }
%
.with.field [
.name protobuf-oneof
.type string
.optional
]
]
2 changes: 1 addition & 1 deletion piqilib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ piqi_boot:
expand_piqi_boot:
$(MAKE) -f Makefile.expand_piqi_boot top
echo "#print_length 1000000;; #print_depth 1000000;;" > expand_piqi_boot.ocaml
(set -e; \
(set -e; export OCAMLTOP_UTF_8=false; \
echo "let piqi_spec ="; \
echo "let t = Piqi_boot.piqi_spec;;" | ./expand_piqi_boot.top -noprompt -init expand_piqi_boot.ocaml | sed -e '1,3d'; \
echo "let piqi_lang ="; \
Expand Down
2 changes: 1 addition & 1 deletion piqilib/gen_piqi_boot.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let load_file fname =
let res = Bytes.create len in
really_input ch res 0 len;
close_in ch;
res
Bytes.unsafe_to_string res


let main () =
Expand Down
42 changes: 30 additions & 12 deletions piqilib/piqi_boot.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ let piqi_spec =
code = Some 217697453l; json_name = None; getopt_letter = None;
getopt_doc = None; proto_name = None}];
parent = None; is_func_param = false; unparsed_piq_ast = None;
protobuf_name = None; protobuf_custom = []; json_name = None;
proto_custom = []; proto_name = None};
protobuf_name = None; protobuf_custom = []; protobuf_oneof = None;
json_name = None; proto_custom = []; proto_name = None};
`alias
{Piqi_impl_piqi.Alias.name = Some "protobuf-int32";
typename = Some "int32"; piqi_type = Some `int; parent = None;
Expand Down Expand Up @@ -237,7 +237,8 @@ let piqi_spec =
getopt_doc = None; proto_name = None}];
parent = None; is_func_param = false; unparsed_piq_ast = None;
protobuf_name = Some "piqi_typedef"; protobuf_custom = [];
json_name = None; proto_custom = []; proto_name = None};
protobuf_oneof = None; json_name = None; proto_custom = [];
proto_name = None};
`enum
{Piqi_impl_piqi.Enum.name = Some "piqi-type";
option =
Expand Down Expand Up @@ -590,6 +591,14 @@ let piqi_spec =
protobuf_packed = false; json_name = None;
json_omit_missing = None; getopt_letter = None; getopt_doc = None;
internal = false; proto_name = None; wire_packed = false};
{Piqi_impl_piqi.Field.name = Some "protobuf-oneof";
typename = Some "string"; mode = `optional; default = None;
deprecated = false; piqtype = None; unparsed_piq_ast = None;
piq_format = None; piq_positional = None; piq_flag_default = None;
piq_alias = None; protobuf_name = None; code = Some 154222907l;
protobuf_packed = false; json_name = None;
json_omit_missing = None; getopt_letter = None; getopt_doc = None;
internal = false; proto_name = None; wire_packed = false};
{Piqi_impl_piqi.Field.name = Some "json-name";
typename = Some "string"; mode = `optional; default = None;
deprecated = false; piqtype = None; unparsed_piq_ast = None;
Expand Down Expand Up @@ -1125,8 +1134,8 @@ let piqi_lang =
code = Some 217697453l; json_name = None; getopt_letter = None;
getopt_doc = None; proto_name = None}];
parent = None; is_func_param = false; unparsed_piq_ast = None;
protobuf_name = None; protobuf_custom = []; json_name = None;
proto_custom = []; proto_name = None};
protobuf_name = None; protobuf_custom = []; protobuf_oneof = None;
json_name = None; proto_custom = []; proto_name = None};
`alias
{Piqi_impl_piqi.Alias.name = Some "protobuf-int32";
typename = Some "int32"; piqi_type = Some `int; parent = None;
Expand Down Expand Up @@ -1347,7 +1356,8 @@ let piqi_lang =
getopt_doc = None; proto_name = None}];
parent = None; is_func_param = false; unparsed_piq_ast = None;
protobuf_name = Some "piqi_typedef"; protobuf_custom = [];
json_name = None; proto_custom = []; proto_name = None};
protobuf_oneof = None; json_name = None; proto_custom = [];
proto_name = None};
`enum
{Piqi_impl_piqi.Enum.name = Some "piqi-type";
option =
Expand Down Expand Up @@ -1740,6 +1750,14 @@ let piqi_lang =
protobuf_packed = false; json_name = None;
json_omit_missing = None; getopt_letter = None; getopt_doc = None;
internal = false; proto_name = None; wire_packed = false};
{Piqi_impl_piqi.Field.name = Some "protobuf-oneof";
typename = Some "string"; mode = `optional; default = None;
deprecated = false; piqtype = None; unparsed_piq_ast = None;
piq_format = None; piq_positional = None; piq_flag_default = None;
piq_alias = None; protobuf_name = None; code = Some 154222907l;
protobuf_packed = false; json_name = None;
json_omit_missing = None; getopt_letter = None; getopt_doc = None;
internal = false; proto_name = None; wire_packed = false};
{Piqi_impl_piqi.Field.name = Some "json-name";
typename = Some "string"; mode = `optional; default = None;
deprecated = false; piqtype = None; unparsed_piq_ast = None;
Expand Down Expand Up @@ -2411,8 +2429,8 @@ let piqi_lang =
code = Some 26300816l; json_name = None; getopt_letter = None;
getopt_doc = None; proto_name = None}];
parent = None; is_func_param = false; unparsed_piq_ast = None;
protobuf_name = None; protobuf_custom = []; json_name = None;
proto_custom = []; proto_name = None};
protobuf_name = None; protobuf_custom = []; protobuf_oneof = None;
json_name = None; proto_custom = []; proto_name = None};
`record
{Piqi_impl_piqi.Record.name = Some "extend";
field =
Expand Down Expand Up @@ -2495,8 +2513,8 @@ let piqi_lang =
protobuf_name = None; code = Some 340962072l; json_name = None;
getopt_letter = None; getopt_doc = None; proto_name = None}];
parent = None; is_func_param = false; unparsed_piq_ast = None;
protobuf_name = None; protobuf_custom = []; json_name = None;
proto_custom = []; proto_name = None}];
protobuf_name = None; protobuf_custom = []; protobuf_oneof = None;
json_name = None; proto_custom = []; proto_name = None}];
import = []; func = []; custom_field = []; extended_typedef = [];
func_typedef = []; extended_func_typedef = []; resolved_typedef = [];
imported_typedef = []; resolved_import = []; extended_import = [];
Expand Down Expand Up @@ -2588,8 +2606,8 @@ let piq =
code = None; json_name = None; getopt_letter = None;
getopt_doc = None; proto_name = None}];
parent = None; is_func_param = false; unparsed_piq_ast = None;
protobuf_name = None; protobuf_custom = []; json_name = None;
proto_custom = []; proto_name = None};
protobuf_name = None; protobuf_custom = []; protobuf_oneof = None;
json_name = None; proto_custom = []; proto_name = None};
`record
{Piqi_impl_piqi.Record.name = Some "piq-node";
field =
Expand Down
7 changes: 6 additions & 1 deletion piqilib/piqi_impl_piqi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ and Variant:
mutable unparsed_piq_ast: Piqi_impl_piqi.uint option;
mutable protobuf_name: string option;
mutable protobuf_custom: string list;
mutable protobuf_oneof: string option;
mutable json_name: string option;
mutable proto_custom: string list;
mutable proto_name: string option;
Expand Down Expand Up @@ -617,6 +618,7 @@ and parse_variant x =
let _protobuf_custom, x = Piqirun.parse_repeated_field 112352691 parse_string x in
let _proto_name, x = Piqirun.parse_optional_field 139663632 parse_string x in
let _name, x = Piqirun.parse_optional_field 150958667 parse_name x in
let _protobuf_oneof, x = Piqirun.parse_optional_field 154222907 parse_string x in
let _option, x = Piqirun.parse_repeated_field 192598901 parse_option x in
let _parent = None in
let _is_func_param = (Piqloc.pause (); let res = parse_bool (Piqirun.parse_default "\b\000") in Piqloc.resume (); res) in
Expand All @@ -629,6 +631,7 @@ and parse_variant x =
Variant.protobuf_custom = _protobuf_custom;
Variant.proto_name = _proto_name;
Variant.name = _name;
Variant.protobuf_oneof = _protobuf_oneof;
Variant.option = _option;
Variant.parent = _parent;
Variant.is_func_param = _is_func_param;
Expand Down Expand Up @@ -1186,10 +1189,11 @@ and gen__variant code x =
let _protobuf_custom = Piqirun.gen_repeated_field 112352691 gen__string x.Variant.protobuf_custom in
let _proto_name = Piqirun.gen_optional_field 139663632 gen__string x.Variant.proto_name in
let _name = Piqirun.gen_optional_field 150958667 gen__name x.Variant.name in
let _protobuf_oneof = Piqirun.gen_optional_field 154222907 gen__string x.Variant.protobuf_oneof in
let _option = Piqirun.gen_repeated_field 192598901 gen__option x.Variant.option in
let _proto_custom = Piqirun.gen_repeated_field 405875126 gen__string x.Variant.proto_custom in
let _json_name = Piqirun.gen_optional_field 515275216 gen__string x.Variant.json_name in
Piqirun.gen_record code (_unparsed_piq_ast :: _protobuf_name :: _protobuf_custom :: _proto_name :: _name :: _option :: _proto_custom :: _json_name :: [])
Piqirun.gen_record code (_unparsed_piq_ast :: _protobuf_name :: _protobuf_custom :: _proto_name :: _name :: _protobuf_oneof :: _option :: _proto_custom :: _json_name :: [])

and gen__option code x =
refer x;
Expand Down Expand Up @@ -1467,6 +1471,7 @@ and default_variant () =
Variant.protobuf_custom = [];
Variant.proto_name = None;
Variant.name = None;
Variant.protobuf_oneof = None;
Variant.option = [];
Variant.parent = None;
Variant.is_func_param = (Piqloc.pause (); let res = parse_bool (Piqirun.parse_default "\b\000") in Piqloc.resume (); res);
Expand Down
7 changes: 6 additions & 1 deletion piqilib/piqi_piqi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ and Variant:
mutable option: Piqi_piqi.option list;
mutable protobuf_name: string option;
mutable protobuf_custom: string list;
mutable protobuf_oneof: string option;
mutable json_name: string option;
}
end = Variant
Expand Down Expand Up @@ -420,13 +421,15 @@ and parse_variant x =
let _protobuf_name, x = Piqirun.parse_optional_field 90072013 parse_string x in
let _protobuf_custom, x = Piqirun.parse_repeated_field 112352691 parse_string x in
let _name, x = Piqirun.parse_required_field 150958667 parse_name x in
let _protobuf_oneof, x = Piqirun.parse_optional_field 154222907 parse_string x in
let _option, x = Piqirun.parse_repeated_field 192598901 parse_option x in
let _json_name, x = Piqirun.parse_optional_field 515275216 parse_string x in
Piqirun.check_unparsed_fields x;
{
Variant.protobuf_name = _protobuf_name;
Variant.protobuf_custom = _protobuf_custom;
Variant.name = _name;
Variant.protobuf_oneof = _protobuf_oneof;
Variant.option = _option;
Variant.json_name = _json_name;
}
Expand Down Expand Up @@ -752,9 +755,10 @@ and gen__variant code x =
let _protobuf_name = Piqirun.gen_optional_field 90072013 gen__string x.Variant.protobuf_name in
let _protobuf_custom = Piqirun.gen_repeated_field 112352691 gen__string x.Variant.protobuf_custom in
let _name = Piqirun.gen_required_field 150958667 gen__name x.Variant.name in
let _protobuf_oneof = Piqirun.gen_optional_field 154222907 gen__string x.Variant.protobuf_oneof in
let _option = Piqirun.gen_repeated_field 192598901 gen__option x.Variant.option in
let _json_name = Piqirun.gen_optional_field 515275216 gen__string x.Variant.json_name in
Piqirun.gen_record code (_protobuf_name :: _protobuf_custom :: _name :: _option :: _json_name :: [])
Piqirun.gen_record code (_protobuf_name :: _protobuf_custom :: _name :: _protobuf_oneof :: _option :: _json_name :: [])

and gen__option code x =
let _code = Piqirun.gen_optional_field 29667629 gen__int32 x.Option.code in
Expand Down Expand Up @@ -938,6 +942,7 @@ and default_variant () =
Variant.protobuf_name = None;
Variant.protobuf_custom = [];
Variant.name = default_name ();
Variant.protobuf_oneof = None;
Variant.option = [];
Variant.json_name = None;
}
Expand Down
41 changes: 37 additions & 4 deletions src/to_proto.ml
Original file line number Diff line number Diff line change
Expand Up @@ -291,20 +291,25 @@ let gen_enum e =
make_enum_def enum_name ^^ eol


let gen_option parent o =
let gen_option_as_field parent o =
let open Option in
iod " " [
ios "optional"; gen_piqtype' o.piqtype ?parent;
gen_piqtype' o.piqtype ?parent;
protoname_of_option o; ios "="; gen_code o.code ^^ ios ";";
]


let gen_variant ?name ?parent v =
let gen_option_as_record_field parent o =
let field = gen_option_as_field parent o in
ios "optional " ^^ field


let gen_variant_as_record ?name ?parent v =
let open Variant in
let parent = recalc_import_parent ?parent v.parent in
let name = recalc_name ?name v.protobuf_name in
(* field definition list *)
let vdefs = List.map (gen_option parent) v.option in
let vdefs = List.map (gen_option_as_record_field parent) v.option in
let vdef = iol
[
ios "message "; ios name;
Expand All @@ -317,6 +322,34 @@ let gen_variant ?name ?parent v =
in vdef


let gen_variant_as_oneof ?name ?parent oneof_name v =
let open Variant in
let parent = recalc_import_parent ?parent v.parent in
let name = recalc_name ?name v.protobuf_name in
(* field definition list *)
let vdefs = List.map (gen_option_as_field parent) v.option in
let vdef = iol
[
ios "message "; ios name;
ios " {"; indent;
ios "oneof "; ios oneof_name; ios " {"; indent;
iod "\n" vdefs;
gen_proto_custom v.protobuf_custom;
unindent; eol;
ios "}"; unindent; eol;
ios "}"; eol;
]
in vdef


let gen_variant ?name ?parent v =
match v.Variant.protobuf_oneof with
| None ->
gen_variant_as_record ?name ?parent v
| Some oneof_name ->
gen_variant_as_oneof ?name ?parent oneof_name v


let gen_list ?name ?parent l =
let open L in
let parent = recalc_import_parent ?parent l.parent in
Expand Down

0 comments on commit 27157ca

Please sign in to comment.