From 29a8921fc7edf3e78b78177eceee4bb6d7231021 Mon Sep 17 00:00:00 2001 From: Konstantin Olkhovskiy Date: Fri, 19 Jan 2024 14:30:15 +0400 Subject: [PATCH] Drop special-casing for messages with only oneof Now oneofs there are also properly encoded as options --- src/compilerlib/pb_codegen_backend.ml | 15 --------------- src/examples/example03.ml.expected | 22 +++++++++++++++++++--- src/examples/example03.mli.expected | 17 +++++++++++++++-- src/examples/example04.ml.expected | 22 +++++++++++++++++++--- src/examples/example04.mli.expected | 17 +++++++++++++++-- 5 files changed, 68 insertions(+), 25 deletions(-) diff --git a/src/compilerlib/pb_codegen_backend.ml b/src/compilerlib/pb_codegen_backend.ml index 14194ced..81a721e2 100644 --- a/src/compilerlib/pb_codegen_backend.ml +++ b/src/compilerlib/pb_codegen_backend.ml @@ -411,21 +411,6 @@ let compile_message ~(unsigned_tag : bool) (file_options : Pb_option.set) } in [ type_ ] - | Tt.Message_oneof_field f :: [] -> - let outer_message_names = message_names @ [ message_name ] in - let variant = - variant_of_oneof ~unsigned_tag ~outer_message_names ~all_types - file_options file_name f - in - [ - Ot. - { - module_prefix; - spec = Variant variant; - type_level_ppx_extension; - type_options = message_options; - }; - ] | _ -> let variants, fields = List.fold_left diff --git a/src/examples/example03.ml.expected b/src/examples/example03.ml.expected index 24f42961..5b8cc697 100644 --- a/src/examples/example03.ml.expected +++ b/src/examples/example03.ml.expected @@ -2,13 +2,23 @@ type string_some_none = unit -type string_some = +type string_some_t = | None | Some of string +and string_some = { + t : string_some_t option; +} + let rec default_string_some_none = () -let rec default_string_some (): string_some = None +let rec default_string_some_t (): string_some_t = None + +and default_string_some + ?t:((t:string_some_t option) = None) + () : string_some = { + t; +} [@@@ocaml.warning "-27-30-39"] @@ -20,7 +30,13 @@ let rec pp_string_some_none fmt (v:string_some_none) = in Pbrt.Pp.pp_brk pp_i fmt () -let rec pp_string_some fmt (v:string_some) = +let rec pp_string_some_t fmt (v:string_some_t) = match v with | None -> Format.fprintf fmt "None" | Some x -> Format.fprintf fmt "@[Some(@,%a)@]" Pbrt.Pp.pp_string x + +and pp_string_some fmt (v:string_some) = + let pp_i fmt () = + Pbrt.Pp.pp_record_field ~first:true "t" (Pbrt.Pp.pp_option pp_string_some_t) fmt v.t; + in + Pbrt.Pp.pp_brk pp_i fmt () diff --git a/src/examples/example03.mli.expected b/src/examples/example03.mli.expected index 6911af79..ffee5707 100644 --- a/src/examples/example03.mli.expected +++ b/src/examples/example03.mli.expected @@ -9,17 +9,27 @@ type string_some_none = unit -type string_some = +type string_some_t = | None | Some of string +and string_some = { + t : string_some_t option; +} + (** {2 Basic values} *) val default_string_some_none : unit (** [default_string_some_none ()] is the default value for type [string_some_none] *) -val default_string_some : unit -> string_some +val default_string_some_t : unit -> string_some_t +(** [default_string_some_t ()] is the default value for type [string_some_t] *) + +val default_string_some : + ?t:string_some_t option -> + unit -> + string_some (** [default_string_some ()] is the default value for type [string_some] *) @@ -28,5 +38,8 @@ val default_string_some : unit -> string_some val pp_string_some_none : Format.formatter -> string_some_none -> unit (** [pp_string_some_none v] formats v *) +val pp_string_some_t : Format.formatter -> string_some_t -> unit +(** [pp_string_some_t v] formats v *) + val pp_string_some : Format.formatter -> string_some -> unit (** [pp_string_some v] formats v *) diff --git a/src/examples/example04.ml.expected b/src/examples/example04.ml.expected index 0f9f6b90..ee2b3158 100644 --- a/src/examples/example04.ml.expected +++ b/src/examples/example04.ml.expected @@ -7,10 +7,14 @@ type int_list_cons = { next : int_list; } -and int_list = +and int_list_t = | Cons of int_list_cons | Nil +and int_list = { + t : int_list_t option; +} + let rec default_int_list_nil = () let rec default_int_list_cons @@ -21,7 +25,13 @@ let rec default_int_list_cons next; } -and default_int_list () : int_list = Cons (default_int_list_cons ()) +and default_int_list_t () : int_list_t = Cons (default_int_list_cons ()) + +and default_int_list + ?t:((t:int_list_t option) = None) + () : int_list = { + t; +} [@@@ocaml.warning "-27-30-39"] @@ -40,7 +50,13 @@ let rec pp_int_list_cons fmt (v:int_list_cons) = in Pbrt.Pp.pp_brk pp_i fmt () -and pp_int_list fmt (v:int_list) = +and pp_int_list_t fmt (v:int_list_t) = match v with | Cons x -> Format.fprintf fmt "@[Cons(@,%a)@]" pp_int_list_cons x | Nil -> Format.fprintf fmt "Nil" + +and pp_int_list fmt (v:int_list) = + let pp_i fmt () = + Pbrt.Pp.pp_record_field ~first:true "t" (Pbrt.Pp.pp_option pp_int_list_t) fmt v.t; + in + Pbrt.Pp.pp_brk pp_i fmt () diff --git a/src/examples/example04.mli.expected b/src/examples/example04.mli.expected index 1b65cb51..71dd0192 100644 --- a/src/examples/example04.mli.expected +++ b/src/examples/example04.mli.expected @@ -14,10 +14,14 @@ type int_list_cons = { next : int_list; } -and int_list = +and int_list_t = | Cons of int_list_cons | Nil +and int_list = { + t : int_list_t option; +} + (** {2 Basic values} *) @@ -31,7 +35,13 @@ val default_int_list_cons : int_list_cons (** [default_int_list_cons ()] is the default value for type [int_list_cons] *) -val default_int_list : unit -> int_list +val default_int_list_t : unit -> int_list_t +(** [default_int_list_t ()] is the default value for type [int_list_t] *) + +val default_int_list : + ?t:int_list_t option -> + unit -> + int_list (** [default_int_list ()] is the default value for type [int_list] *) @@ -43,5 +53,8 @@ val pp_int_list_nil : Format.formatter -> int_list_nil -> unit val pp_int_list_cons : Format.formatter -> int_list_cons -> unit (** [pp_int_list_cons v] formats v *) +val pp_int_list_t : Format.formatter -> int_list_t -> unit +(** [pp_int_list_t v] formats v *) + val pp_int_list : Format.formatter -> int_list -> unit (** [pp_int_list v] formats v *)