diff --git a/src/decoders_deriver.ml b/src/decoders_deriver.ml index e4c6b7c..7a95ab5 100644 --- a/src/decoders_deriver.ml +++ b/src/decoders_deriver.ml @@ -67,13 +67,21 @@ let rec expr_of_typ (typ : core_type) match typ with | [%type: unit] | [%type: unit] -> Ast_builder.Default.evar ~loc "D.null" | [%type: int] -> Ast_builder.Default.evar ~loc "D.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_dec = Ast_builder.Default.evar ~loc "D.int" in + [%expr + let open D.Infix in + [%e int_dec] >|= Int32.of_int] + | [%type: int64] | [%type: Int64.t] -> + let int_dec = Ast_builder.Default.evar ~loc "D.int" in + [%expr + let open D.Infix in + [%e int_dec] >|= Int64.of_int] + | [%type: nativeint] | [%type: Nativeint.t] -> + let int_dec = Ast_builder.Default.evar ~loc "D.int" in + [%expr + let open D.Infix in + [%e int_dec] >|= Nativeint.of_int] | [%type: float] -> Ast_builder.Default.evar ~loc "D.float" | [%type: bool] -> Ast_builder.Default.evar ~loc "D.bool" | [%type: char] -> 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_decoders.ml b/test/test_decoders.ml index 169574c..fde8e90 100644 --- a/test/test_decoders.ml +++ b/test/test_decoders.ml @@ -273,3 +273,40 @@ let%test "double type var" = match D.decode_string double_wrapped_decoder {|{"fst":"99","snd":100}|} with | Ok { fst = "99"; snd = 100 } -> true | _ -> false + +module Ints = struct + type my_int32 = int32 [@@deriving decoders] + + let%test "int32" = + match D.decode_string my_int32_decoder "123445" with + | Ok 123445l -> true + | _ -> false + + type my_int32t = Int32.t [@@deriving decoders] + + let%test "int32t" = + match D.decode_string my_int32t_decoder "5438" with + | Ok 5438l -> true + | _ -> false + + type my_int64 = int64 [@@deriving decoders] + + let%test "int64" = + match D.decode_string my_int64_decoder "123445" with + | Ok 123445L -> true + | _ -> false + + type my_int64t = Int64.t [@@deriving decoders] + + let%test "int64t" = + match D.decode_string my_int64t_decoder "5438" with + | Ok 5438L -> true + | _ -> false + + type my_nativeint = Nativeint.t [@@deriving decoders] + + let%test "my_nativeint" = + match D.decode_string my_nativeint_decoder "5438" with + | Ok 5438n -> true + | _ -> false +end 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