diff --git a/src/encoders_deriver.ml b/src/encoders_deriver.ml index 0692f0e..7aebbe2 100644 --- a/src/encoders_deriver.ml +++ b/src/encoders_deriver.ml @@ -20,13 +20,15 @@ let rec expr_of_typ (typ : core_type) : expression = match typ with | [%type: unit] | [%type: unit] -> Ast_builder.Default.evar ~loc "E.null" | [%type: int] -> Ast_builder.Default.evar ~loc "E.int" - | [%type: int32] - | [%type: Int32.t] - | [%type: int64] - | [%type: Int64.t] - | [%type: nativeint] - | [%type: Nativeint.t] -> - failwith "Cannot yet handle any int-like but int" + | [%type: int32] | [%type: Int32.t] -> + let int_enc = Ast_builder.Default.evar ~loc "E.int" in + [%expr fun i -> i |> Int32.to_int |> [%e int_enc]] + | [%type: int64] | [%type: Int64.t] -> + let int_enc = Ast_builder.Default.evar ~loc "E.int" in + [%expr fun i -> i |> Int64.to_int |> [%e int_enc]] + | [%type: nativeint] | [%type: Nativeint.t] -> + let int_enc = Ast_builder.Default.evar ~loc "E.int" in + [%expr fun i -> i |> Nativeint.to_int |> [%e int_enc]] | [%type: float] -> Ast_builder.Default.evar ~loc "E.float" | [%type: bool] -> Ast_builder.Default.evar ~loc "E.bool" | [%type: char] -> diff --git a/test/test_encoders.ml b/test/test_encoders.ml index aeab430..605907d 100644 --- a/test/test_encoders.ml +++ b/test/test_encoders.ml @@ -98,3 +98,40 @@ let%test "module wrapped" = match E.encode_string outer_inner_wrapped_encoder { wrapped = "a thing" } with | {|{"wrapped":"a thing"}|} -> true | _ -> false + +module Ints = struct + type my_int32 = int32 [@@deriving encoders] + + let%test "int32" = + match E.encode_string my_int32_encoder 123445l with + | "123445" -> true + | _ -> false + + type my_int32t = Int32.t [@@deriving encoders] + + let%test "int32t" = + match E.encode_string my_int32t_encoder 5438l with + | "5438" -> true + | _ -> false + + type my_int64 = int64 [@@deriving encoders] + + let%test "int64" = + match E.encode_string my_int64_encoder 123445L with + | "123445" -> true + | _ -> false + + type my_int64t = Int64.t [@@deriving encoders] + + let%test "int64t" = + match E.encode_string my_int64t_encoder 5438L with + | "5438" -> true + | _ -> false + + type my_nativeint = Nativeint.t [@@deriving encoders] + + let%test "my_nativeint" = + match E.encode_string my_nativeint_encoder 5438n with + | "5438" -> true + | _ -> false +end