Skip to content

Commit

Permalink
Merge pull request #131 from kit-ty-kate/fix-default-wrap
Browse files Browse the repository at this point in the history
Revert #128 and reapply the original fix from #126
  • Loading branch information
kit-ty-kate authored Nov 7, 2020
2 parents 811dbcb + 1ade989 commit 926cf01
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/ppx_deriving_yojson.ml
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ and desu_expr_of_only_typ ~path typ =
raise_errorf ~loc:ptyp_loc "%s cannot be derived for %s"
deriver (Ppx_deriving.string_of_core_type typ)

(* TODO: Do not wrap runtime around [@default ...].
We do currently and for instance the following doesn't currently work:
module List = struct let x = [1; 2] end
type t = {field : int list [@default List.x]} [@@deriving to_yojson]
*)
let wrap_runtime decls =
Ppx_deriving.sanitize ~module_:(Lident "Ppx_deriving_yojson_runtime") decls

Expand All @@ -321,7 +326,7 @@ let ser_str_of_record ~loc varname labels =
| None ->
[%expr [%e result] :: fields]
| Some default ->
[%expr if Pervasives.(=) [%e field] [%e default] then fields else [%e result] :: fields])
[%expr if [%e field] = [%e default] then fields else [%e result] :: fields])
in
let assoc =
List.fold_left
Expand Down
16 changes: 14 additions & 2 deletions src/ppx_deriving_yojson_runtime.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
include module type of Ppx_deriving_runtime

type 'a error_or = ('a, string) Result.result

val ( >>= ) : 'a error_or -> ('a -> 'b error_or) -> 'b error_or
Expand All @@ -10,3 +8,17 @@ val map_bind : ('a -> 'b error_or) -> 'b list -> 'a list -> 'b list error_or
computes it tail-recursively so that large list lengths don't
cause a stack overflow *)
val safe_map : ('a -> 'b) -> 'a list -> 'b list

val ( = ) : 'a -> 'a -> bool (* NOTE: Used for [@default ...] *)
module List : (module type of List)
module String : (module type of String)
module Bytes : (module type of Bytes)
module Int32 : (module type of Int32)
module Int64 : (module type of Int64)
module Nativeint : (module type of Nativeint)
module Array : (module type of Array)
module Result : sig
type ('a, 'b) result = ('a, 'b) Result.result =
| Ok of 'a
| Error of 'b
end
20 changes: 19 additions & 1 deletion src_test/test_ppx_yojson.ml
Original file line number Diff line number Diff line change
Expand Up @@ -505,16 +505,34 @@ let test_int_redefined ctxt =
let expected = `Int 1 in
assert_equal ~ctxt ~printer:show_json expected M.x

(* TODO: Make this work *)
(*
let test_list_redefined ctxt =
let module M = struct
type redef_list =
| []
| (::) of int * int
type t = {field : int list} [@@deriving to_yojson]
let x = {field = List.([1;2])}
end
in
let expected = `List [`Int 1; `Int 2] in
assert_equal ~ctxt ~printer:show_json expected M.x
*)

let test_equality_redefined ctxt =
let module M = struct
module Pervasives = struct
let (=) : int -> int -> bool = fun a b -> a = b
let _ = 1 = 1 (* just dummy usage of `=` to suppress compiler warning *)

let never_gonna_be_in_pervasives = None
end
let (=) : int -> int -> bool = fun a b -> a = b
let _ = 1 = 1 (* just dummy usage of `=` to suppress compiler warning *)

type t = {field : int option [@default None]} [@@deriving to_yojson]
type t = {field : int option [@default Pervasives.never_gonna_be_in_pervasives]} [@@deriving to_yojson]
let x = {field = Some 42}
end
in
Expand Down

0 comments on commit 926cf01

Please sign in to comment.