From ec3c49882299bfe034782e5c42036eee08fe3c2c Mon Sep 17 00:00:00 2001 From: swrup Date: Sun, 16 Nov 2025 15:28:14 +0100 Subject: [PATCH] JJ: Description from the destination commit: change ptime util conversion JJ: Description from source commit: change amount fraction to int32 --- src/amount.ml | 21 ++++++++++++++------- src/pg.ml | 4 +--- test/test.ml | 2 +- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/amount.ml b/src/amount.ml index 1fb66513..5123ac37 100644 --- a/src/amount.ml +++ b/src/amount.ml @@ -15,17 +15,17 @@ type t = { sign: [ `Plus | `Minus ] option; currency: [ `Eur ]; value: Int64.t; - fraction: Int64.t; + fraction: Int32.t; } let ( let* ) o f = match o with Ok v -> f v | Error _ as e -> e let check_not msg = function false -> Ok () | true -> Error msg let value_upper_bound = Z.(pow (of_int 2) 52) |> Z.to_int64 -let fraction_upper_bound = Int64.of_int 100_000_000 +let fraction_upper_bound = Int32.of_int 100_000_000 let make ~sign ~currency ~value ~fraction = let* () = check_not "value is negative" (value < Int64.zero) in - let* () = check_not "fraction is negative" (fraction < Int64.zero) in + let* () = check_not "fraction is negative" (fraction < Int32.zero) in let* () = check_not "value is greater than 2^52" (value > value_upper_bound) in @@ -44,7 +44,7 @@ let to_string = in let pp_currency ppf = function `Eur -> string ppf "EUR" in fun ppf { sign; currency; value; fraction } -> - pf ppf "%a%a:%Ld.%02Ld" (Fmt.option pp_sign) sign pp_currency currency + pf ppf "%a%a:%Ld.%02ld" (Fmt.option pp_sign) sign pp_currency currency value fraction in Fmt.str "%a" pp @@ -59,19 +59,26 @@ let of_string = ] in let parse_currency = string "EUR" *> return `Eur in - let parse_int = + let parse_int64 = take_while1 (function '0' .. '9' -> true | _ -> false) >>| Int64.of_string_opt >>= function | None -> fail "invalid integer" | Some n -> return n in + let parse_int32 = + take_while1 (function '0' .. '9' -> true | _ -> false) + >>| Int32.of_string_opt + >>= function + | None -> fail "invalid integer" + | Some n -> return n + in let parse_t = lift4 (fun sign currency value fraction -> make ~sign ~currency ~value ~fraction) parse_sign parse_currency - (char ':' *> parse_int) - (char '.' *> parse_int) + (char ':' *> parse_int64) + (char '.' *> parse_int32) in fun s -> parse_string ~consume:Consume.All parse_t s |> Result.join diff --git a/src/pg.ml b/src/pg.ml index 6deccf9b..015cf9da 100644 --- a/src/pg.ml +++ b/src/pg.ml @@ -14,11 +14,9 @@ end let pg_amount : Amount.t Caqti_type.t = let open Amount in - (* TODO bad int32 conversion *) Caqti_type.custom - ~encode:(fun amount -> Ok (amount.value, Int64.to_int32 amount.fraction)) + ~encode:(fun amount -> Ok (amount.value, amount.fraction)) ~decode:(fun (value, fraction) -> - let fraction = Int64.of_int32 fraction in Ok { sign= None; currency= `Eur; value; fraction }) Caqti_type.(t2 int64 int32) diff --git a/test/test.ml b/test/test.ml index 26a811ac..45ea955a 100644 --- a/test/test.ml +++ b/test/test.ml @@ -34,7 +34,7 @@ let () = sign= Some `Plus; currency= `Eur; value= Int64.of_int 25; - fraction= Int64.of_int 678; + fraction= Int32.of_int 678; } in let s = to_string v in