Skip to content

Commit

Permalink
Drop special-casing for messages with only oneof
Browse files Browse the repository at this point in the history
Now oneofs there are also properly encoded as options
  • Loading branch information
Lupus authored and c-cube committed Jan 29, 2024
1 parent 9dbbe58 commit 29a8921
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 25 deletions.
15 changes: 0 additions & 15 deletions src/compilerlib/pb_codegen_backend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions src/examples/example03.ml.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand All @@ -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 "@[<hv2>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 ()
17 changes: 15 additions & 2 deletions src/examples/example03.mli.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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] *)


Expand All @@ -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 *)
22 changes: 19 additions & 3 deletions src/examples/example04.ml.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]

Expand All @@ -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 "@[<hv2>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 ()
17 changes: 15 additions & 2 deletions src/examples/example04.mli.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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} *)

Expand All @@ -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] *)


Expand All @@ -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 *)

0 comments on commit 29a8921

Please sign in to comment.