From 4870976adf4aa8a82d6be378f91571d39756af10 Mon Sep 17 00:00:00 2001 From: Greg Rychlewski Date: Tue, 6 Aug 2024 09:17:01 -0400 Subject: [PATCH 1/4] dialyzer --- lib/postgrex/extensions/interval.ex | 101 ++++++++++++++++------------ 1 file changed, 58 insertions(+), 43 deletions(-) diff --git a/lib/postgrex/extensions/interval.ex b/lib/postgrex/extensions/interval.ex index 7860eb70..23e20e30 100644 --- a/lib/postgrex/extensions/interval.ex +++ b/lib/postgrex/extensions/interval.ex @@ -43,46 +43,15 @@ defmodule Postgrex.Extensions.Interval do def decode(type) do quote location: :keep do <<16::int32(), microseconds::int64(), days::int32(), months::int32()>> -> - seconds = div(microseconds, 1_000_000) - microseconds = rem(microseconds, 1_000_000) - - case unquote(type) do - Postgrex.Interval -> - %Postgrex.Interval{ - months: months, - days: days, - secs: seconds, - microsecs: microseconds - } - - Duration -> - years = div(months, 12) - months = rem(months, 12) - weeks = div(days, 7) - days = rem(days, 7) - minutes = div(seconds, 60) - seconds = rem(seconds, 60) - hours = div(minutes, 60) - minutes = rem(minutes, 60) - type_mod = var!(mod) - precision = if type_mod, do: type_mod &&& unquote(@precision_mask) - - precision = - if precision in unquote(@unspecified_precision), - do: unquote(@default_precision), - else: precision - - Duration.new!( - year: years, - month: months, - week: weeks, - day: days, - hour: hours, - minute: minutes, - second: seconds, - microsecond: {microseconds, precision} - ) - end + precision = if var!(mod), do: var!(mod) &&& unquote(@precision_mask) + + unquote(__MODULE__).decode_interval( + microseconds, + days, + months, + precision, + unquote(type) + ) end end else @@ -100,10 +69,56 @@ defmodule Postgrex.Extensions.Interval do def decode(_) do quote location: :keep do <<16::int32(), microseconds::int64(), days::int32(), months::int32()>> -> - seconds = div(microseconds, 1_000_000) - microseconds = rem(microseconds, 1_000_000) - %Postgrex.Interval{months: months, days: days, secs: seconds, microsecs: microseconds} + unquote(__MODULE__).decode_interval(microseconds, days, months, nil, Postgrex.Interval) end end end + + ## Helpers + + def decode_interval(microseconds, days, months, _precision, Postgrex.Interval) do + seconds = div(microseconds, 1_000_000) + microseconds = rem(microseconds, 1_000_000) + + %Postgrex.Interval{ + months: months, + days: days, + secs: seconds, + microsecs: microseconds + } + end + + def decode_interval(microseconds, days, months, precision, Duration) do + years = div(months, 12) + months = rem(months, 12) + weeks = div(days, 7) + days = rem(days, 7) + seconds = div(microseconds, 1_000_000) + microseconds = rem(microseconds, 1_000_000) + minutes = div(seconds, 60) + seconds = rem(seconds, 60) + hours = div(minutes, 60) + minutes = rem(minutes, 60) + + precision = + if precision in unquote(@unspecified_precision), + do: unquote(@default_precision), + else: precision + + Duration.new!( + year: years, + month: months, + week: weeks, + day: days, + hour: hours, + minute: minutes, + second: seconds, + microsecond: {microseconds, precision} + ) + end + + def decode_interval(_, _, _, type) do + raise ArgumentError, + "#{inspect(type)} is not valid for `:interval_decode_type`. Please use either `Postgrex.Interval` or `Duration`" + end end From 104ca6d23da24890bcc4f6f3f682cb7fa09495c5 Mon Sep 17 00:00:00 2001 From: Greg Rychlewski Date: Tue, 6 Aug 2024 09:20:09 -0400 Subject: [PATCH 2/4] oops --- lib/postgrex/extensions/interval.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/postgrex/extensions/interval.ex b/lib/postgrex/extensions/interval.ex index 23e20e30..1737e8e7 100644 --- a/lib/postgrex/extensions/interval.ex +++ b/lib/postgrex/extensions/interval.ex @@ -117,7 +117,7 @@ defmodule Postgrex.Extensions.Interval do ) end - def decode_interval(_, _, _, type) do + def decode_interval(_, _, _, _, type) do raise ArgumentError, "#{inspect(type)} is not valid for `:interval_decode_type`. Please use either `Postgrex.Interval` or `Duration`" end From 636003bce91c8b33a9c8c33787f1561f7694cf6f Mon Sep 17 00:00:00 2001 From: Greg Rychlewski Date: Tue, 6 Aug 2024 09:22:53 -0400 Subject: [PATCH 3/4] oops2 --- lib/postgrex/extensions/interval.ex | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/postgrex/extensions/interval.ex b/lib/postgrex/extensions/interval.ex index 1737e8e7..d3ac4e54 100644 --- a/lib/postgrex/extensions/interval.ex +++ b/lib/postgrex/extensions/interval.ex @@ -3,7 +3,16 @@ defmodule Postgrex.Extensions.Interval do import Postgrex.BinaryUtils, warn: false use Postgrex.BinaryExtension, send: "interval_send" - def init(opts), do: Keyword.get(opts, :interval_decode_type, Postgrex.Interval) + def init(opts) do + case Keyword.get(opts, :interval_decode_type, Postgrex.Interval) do + type when type in [Postgrex.Interval, Duration] -> + type + + other -> + raise ArgumentError, + "#{inspect(other)} is not valid for `:interval_decode_type`. Please use either `Postgrex.Interval` or `Duration`" + end + end if Code.ensure_loaded?(Duration) do import Bitwise, warn: false @@ -116,9 +125,4 @@ defmodule Postgrex.Extensions.Interval do microsecond: {microseconds, precision} ) end - - def decode_interval(_, _, _, _, type) do - raise ArgumentError, - "#{inspect(type)} is not valid for `:interval_decode_type`. Please use either `Postgrex.Interval` or `Duration`" - end end From 7d5e2eb8f2eb35655dadcb83766f380d278c6dfe Mon Sep 17 00:00:00 2001 From: Greg Rychlewski Date: Tue, 6 Aug 2024 09:28:47 -0400 Subject: [PATCH 4/4] oops 3 --- lib/postgrex/extensions/interval.ex | 94 +++++++++++++++++------------ 1 file changed, 54 insertions(+), 40 deletions(-) diff --git a/lib/postgrex/extensions/interval.ex b/lib/postgrex/extensions/interval.ex index d3ac4e54..5fa91b2e 100644 --- a/lib/postgrex/extensions/interval.ex +++ b/lib/postgrex/extensions/interval.ex @@ -63,6 +63,49 @@ defmodule Postgrex.Extensions.Interval do ) end end + + ## Helpers + + def decode_interval(microseconds, days, months, _precision, Postgrex.Interval) do + seconds = div(microseconds, 1_000_000) + microseconds = rem(microseconds, 1_000_000) + + %Postgrex.Interval{ + months: months, + days: days, + secs: seconds, + microsecs: microseconds + } + end + + def decode_interval(microseconds, days, months, precision, Duration) do + years = div(months, 12) + months = rem(months, 12) + weeks = div(days, 7) + days = rem(days, 7) + seconds = div(microseconds, 1_000_000) + microseconds = rem(microseconds, 1_000_000) + minutes = div(seconds, 60) + seconds = rem(seconds, 60) + hours = div(minutes, 60) + minutes = rem(minutes, 60) + + precision = + if precision in unquote(@unspecified_precision), + do: unquote(@default_precision), + else: precision + + Duration.new!( + year: years, + month: months, + week: weeks, + day: days, + hour: hours, + minute: minutes, + second: seconds, + microsecond: {microseconds, precision} + ) + end else def encode(_) do quote location: :keep do @@ -81,48 +124,19 @@ defmodule Postgrex.Extensions.Interval do unquote(__MODULE__).decode_interval(microseconds, days, months, nil, Postgrex.Interval) end end - end - ## Helpers + ## Helpers - def decode_interval(microseconds, days, months, _precision, Postgrex.Interval) do - seconds = div(microseconds, 1_000_000) - microseconds = rem(microseconds, 1_000_000) + def decode_interval(microseconds, days, months, _precision, Postgrex.Interval) do + seconds = div(microseconds, 1_000_000) + microseconds = rem(microseconds, 1_000_000) - %Postgrex.Interval{ - months: months, - days: days, - secs: seconds, - microsecs: microseconds - } - end - - def decode_interval(microseconds, days, months, precision, Duration) do - years = div(months, 12) - months = rem(months, 12) - weeks = div(days, 7) - days = rem(days, 7) - seconds = div(microseconds, 1_000_000) - microseconds = rem(microseconds, 1_000_000) - minutes = div(seconds, 60) - seconds = rem(seconds, 60) - hours = div(minutes, 60) - minutes = rem(minutes, 60) - - precision = - if precision in unquote(@unspecified_precision), - do: unquote(@default_precision), - else: precision - - Duration.new!( - year: years, - month: months, - week: weeks, - day: days, - hour: hours, - minute: minutes, - second: seconds, - microsecond: {microseconds, precision} - ) + %Postgrex.Interval{ + months: months, + days: days, + secs: seconds, + microsecs: microseconds + } + end end end