Skip to content

Commit

Permalink
Sync with piqi
Browse files Browse the repository at this point in the history
- add support for .internal flag for fields which leads to ignoring them during
  serialization/deserialization
- completely remove the notion of 'flags' (i.e. .field [ .name ... .optional ]
  for Protobuf serialization. They remain as a concept only in piq format. For
  all other formatsthey are equivalent to .field [ .name ... .optional .type
  bool .default false ]
- upgrade to the latest piqi self-spec
  • Loading branch information
alavrik committed Oct 4, 2017
1 parent 3b114f7 commit d372514
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 120 deletions.
28 changes: 4 additions & 24 deletions piqic-ocaml/piqic_ocaml_defaults.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ open C
open Iolist


let gen_type context typename =
let import, parent_piqi, typedef = C.resolve_typename context typename in
let parent_mod = C.gen_parent_mod import in
iol [parent_mod; ios "default_"; ios (C.typedef_mlname typedef); ios " ()"]
let gen_type context typename = Piqic_ocaml_in.gen_default_type context typename


let gen_int piqi_type wire_type =
Expand Down Expand Up @@ -115,30 +112,13 @@ let rec gen_alias_type ?wire_type context a =
gen_type context typename


let gen_field_cons context rname f =
let gen_field_default_cons context rname f =
let open Field in
let fname = C.mlname_of_field context f in
let ffname = (* fully-qualified field name *)
iol [ios rname; ios "."; ios fname]
in
let value =
match f.mode, f.default with
| `required, _ -> gen_type context (some_of f.typename)
| `optional, _ when f.typename = None -> ios "false" (* flag *)
| `optional, Some piqi_any when not f.ocaml_optional ->
let pb = some_of piqi_any.Any.protobuf in
let default_str = String.escaped pb in
let typename = some_of f.typename in
iol [
Piqic_ocaml_in.gen_type context typename;
ios " (Piqirun.parse_default "; ioq default_str; ios ")";
]
| `optional, _ -> ios "None"
| `repeated, _ ->
if f.ocaml_array
then ios "[||]"
else ios "[]"
in
let value = Piqic_ocaml_in.gen_field_default_value context f in
(* field construction code *)
iol [ffname; ios " = "; value; ios ";"]

Expand All @@ -150,7 +130,7 @@ let gen_record context r =
let fields = List.sort (fun a b -> compare a.F.code b.F.code) r.R.field in
let fconsl = (* field constructor list *)
if fields <> []
then List.map (gen_field_cons context rname) fields
then List.map (gen_field_default_cons context rname) fields
else [ios rname; ios "._dummy = ();"]
in
let fconsl =
Expand Down
90 changes: 56 additions & 34 deletions piqic-ocaml/piqic_ocaml_in.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,40 @@ let gen_default = function
let esc x = ios "_" ^^ ios x


let gen_default_type context typename =
let import, parent_piqi, typedef = C.resolve_typename context typename in
let parent_mod = C.gen_parent_mod import in
iol [parent_mod; ios "default_"; ios (C.typedef_mlname typedef); ios " ()"]


let gen_field_default_value context f =
let open Field in
let typename = some_of f.typename in
match f.mode, f.default with
| `required, _ ->
gen_default_type context typename
| `optional, Some piqi_any when not f.ocaml_optional ->
let pb = some_of piqi_any.Any.protobuf in
let default_str = String.escaped pb in
iol [
gen_cc "(Piqloc.pause (); let res = ";
gen_type context typename;
ios " (Piqirun.parse_default "; ioq default_str; ios ")";
gen_cc " in Piqloc.resume (); res)";
]
| `optional, _ -> ios "None"
| `repeated, _ ->
if f.ocaml_array
then ios "[||]"
else ios "[]"


let gen_field_cons context rname f =
let open Field in
let fname = C.mlname_of_field context f in
let ffname = (* fully-qualified field name *)
iol [ios rname; ios "."; ios fname]
in
in
(* field construction code *)
iol [ ffname; ios " = "; esc fname; ios ";" ]

Expand All @@ -101,36 +129,33 @@ let gen_field_parser context f =
let fname = C.mlname_of_field context f in
let mode = C.gen_field_mode context f in
let code = C.gen_code f.code in
let fcons =
match f.typename with
| Some typename ->
(* field constructor *)
iod " " [
(* "parse_(required|optional|repeated)_field" function invocation *)
ios "Piqirun.parse_" ^^ ios mode ^^ ios "_field";
code;
gen_type context typename ~is_packed:f.protobuf_packed;

(* when parsing packed repeated fields, we should also accept
* fields in unpacked representation; therefore, specifying an
* unpacked field parser as another parameter *)
if f.protobuf_packed
then gen_type context typename
else iol [];

ios "x";
gen_default f.default;
]
| None ->
(* flag constructor *)
iod " " [
gen_cc "incr_count_if_true (";
ios "Piqirun.parse_flag"; code; ios "x";
gen_cc ")";
]
in
(* field parsing code *)
iol [ ios "let "; esc fname; ios ", x = "; fcons; ios " in"; eol ]
let typename = some_of f.typename in
if not f.internal
then
let fcons =
(* field constructor *)
iod " " [
(* "parse_(required|optional|repeated)_field" function invocation *)
ios "Piqirun.parse_" ^^ ios mode ^^ ios "_field";
code;
gen_type context typename ~is_packed:f.protobuf_packed;

(* when parsing packed repeated fields, we should also accept
* fields in unpacked representation; therefore, specifying an
* unpacked field parser as another parameter *)
if f.protobuf_packed
then gen_type context typename
else iol [];

ios "x";
gen_default f.default;
]
in
(* field parsing code *)
iol [ ios "let "; esc fname; ios ", x = "; fcons; ios " in"; eol ]
else
let fcons = gen_field_default_value context f in
iol [ ios "let "; esc fname; ios " = "; fcons; ios " in"; eol ]


let gen_record context r =
Expand Down Expand Up @@ -340,9 +365,6 @@ let gen_typedefs context typedefs =
if not (Obj.is_int (Obj.repr obj))
then Piqloc.addrefret ref obj
else obj\n";
gen_cc "let incr_count_if_true ((obj, _) as res) =
if obj then ignore(next_count());
res\n\n";

ios "let rec "; iod "and " (newlines (newlines defs));
eol
Expand Down
33 changes: 12 additions & 21 deletions piqic-ocaml/piqic_ocaml_out.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,25 @@ let gen_field context rname f =
in
let code = C.gen_code f.code in
let mode = C.gen_field_mode context f in
let fgen =
match f.typename with
| Some typename ->
(* field generation code *)
iod " " [
ios "Piqirun.gen_" ^^ ios mode ^^ ios "_field";
code;
gen_type context typename ~is_packed:f.protobuf_packed;
ffname
]
| None ->
(* flag generation code *)
iod " " [
gen_cc "reference_if_true ";
ios "Piqirun.gen_flag"; code; ffname;
]
let typename = some_of f.typename in
(* field generation code *)
let fgen = iod " " [
ios "Piqirun.gen_" ^^ ios mode ^^ ios "_field";
code;
gen_type context typename ~is_packed:f.protobuf_packed;
ffname
]
in (fname, fgen)


let gen_record context r =
(* fully-qualified capitalized record name *)
let rname = String.capitalize (some_of r.R.ocaml_name) in
(* skip fields marked as .internal *)
let fields = r.R.field in
let fields = List.filter (fun x -> not x.F.internal) fields in
(* order fields by are by their integer codes *)
let fields = List.sort (fun a b -> compare a.F.code b.F.code) r.R.field in
let fields = List.sort (fun a b -> compare a.F.code b.F.code) fields in
let fgens = (* field generators list *)
List.map (gen_field context rname) fields
in
Expand Down Expand Up @@ -319,10 +314,6 @@ let gen_typedefs context typedefs =
then Piqloc.addref obj count\n";
gen_cc "let reference f code x = refer x; f code x\n";
gen_cc "let reference1 f x = refer x; f x\n";
gen_cc "let reference_if_true f code x =
if x
then reference f code x
else f code x\n\n";

ios "let rec "; iod "and " (newlines (newlines defs_2));
eol;
Expand Down
Loading

0 comments on commit d372514

Please sign in to comment.