Skip to content

Commit

Permalink
Merge pull request #20 from benbellick/better-errors
Browse files Browse the repository at this point in the history
fully improve errors and add test for open variant cstr
  • Loading branch information
benbellick authored Dec 19, 2024
2 parents efb192e + 7d80981 commit ba56d2b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/decoders_deriver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,19 @@ let implementation_generator ~(loc : location) ~rec_flag ~substitutions
let imple_expr =
match (type_decl.ptype_kind, type_decl.ptype_manifest) with
| Ptype_abstract, Some manifest -> expr_of_typ ~substitutions manifest
| Ptype_variant cstrs, None -> expr_of_variant ~loc ~substitutions cstrs
| Ptype_abstract, None ->
Location.raise_errorf ~loc
"Cannot construct decoder for %s: cannot decode abstract type"
type_decl.ptype_name.txt
| Ptype_variant cstrs, _ -> expr_of_variant ~loc ~substitutions cstrs
| Ptype_record label_decs, _ ->
expr_of_record ~substitutions ~loc label_decs
| Ptype_open, _ -> Location.raise_errorf ~loc "Unhandled open"
| _ -> Location.raise_errorf ~loc "Unhandled mystery"
| Ptype_open, _ ->
Location.raise_errorf ~loc
"Cannot construct decoder for %s: cannot decode extensible type"
type_decl.ptype_name.txt
in

match rec_flag with
| Nonrecursive -> imple_expr
| Recursive -> wrap_as_aux ~loc ~name ~expr:imple_expr
Expand Down
13 changes: 9 additions & 4 deletions src/encoders_deriver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ and expr_of_variant ~loc cstrs =
pexp_function ~loc cases

let implementation_generator ~(loc : location) type_decl : expression =
let _name = to_encoder_name type_decl.ptype_name.txt in
let imple_expr =
match (type_decl.ptype_kind, type_decl.ptype_manifest) with
| Ptype_abstract, Some manifest -> (
Expand All @@ -223,7 +222,11 @@ let implementation_generator ~(loc : location) type_decl : expression =
in
[%expr fun [%p args] -> [%e expr]]
| _ -> expr)
| Ptype_variant cstrs, None -> expr_of_variant ~loc cstrs
| Ptype_abstract, None ->
Location.raise_errorf ~loc
"Cannot construct encoder for %s: cannot encode abstract type"
type_decl.ptype_name.txt
| Ptype_variant cstrs, _ -> expr_of_variant ~loc cstrs
| Ptype_record label_decs, _ ->
(* And in the case of a top-level record, we also need to explicitly wrap in a lambda with args *)
let arg_fields =
Expand All @@ -237,8 +240,10 @@ let implementation_generator ~(loc : location) type_decl : expression =
let args = Ast_builder.Default.ppat_record ~loc arg_fields Closed in
let expr = expr_of_record ~loc label_decs in
[%expr fun [%p args] -> [%e expr]]
| Ptype_open, _ -> Location.raise_errorf ~loc "Unhandled open"
| _ -> Location.raise_errorf ~loc "Unhandled mystery"
| Ptype_open, _ ->
Location.raise_errorf ~loc
"Cannot construct encoder for %s: cannot encode extensible type"
type_decl.ptype_name.txt
in
imple_expr

Expand Down
8 changes: 8 additions & 0 deletions test/test_decoders.ml
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,11 @@ let%test "char" =
(* We expect an error here because the string must have length 1 *)
| Error _ -> true
| _ -> false

type var = A | B
type open_var = var = A | B [@@deriving decoders]

let%test "type alias opened cstrs" =
match D.decode_string open_var_decoder {|{"B":null}|} with
| Ok B -> true
| _ -> false
8 changes: 8 additions & 0 deletions test/test_encoders.ml
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,11 @@ type my_char = char [@@deriving encoders]

let%test "char" =
match E.encode_string my_char_encoder 'c' with {|"c"|} -> true | _ -> false

type var = A | B
type open_var = var = A | B [@@deriving encoders]

let%test "type alias opened cstrs" =
match E.encode_string open_var_encoder A with
| {|{"A":null}|} -> true
| _ -> false

0 comments on commit ba56d2b

Please sign in to comment.