Skip to content

Commit

Permalink
Merge pull request #13 from benbellick/ints
Browse files Browse the repository at this point in the history
Ints
  • Loading branch information
benbellick authored Dec 15, 2024
2 parents 2a70361 + a999851 commit 094f2dd
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 14 deletions.
22 changes: 15 additions & 7 deletions src/decoders_deriver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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] ->
Expand Down
16 changes: 9 additions & 7 deletions src/encoders_deriver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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] ->
Expand Down
37 changes: 37 additions & 0 deletions test/test_decoders.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 37 additions & 0 deletions test/test_encoders.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 094f2dd

Please sign in to comment.