From c5e848c941647e7614fbf4b6ab834e5d669c22be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20P=C3=A9rez?= Date: Mon, 16 Dec 2024 23:37:34 +0100 Subject: [PATCH] experiments with pgtype numeric --- go.mod | 4 +++ go.sum | 8 +++++ pgutils/pgutils.go | 72 ++++++++++++++++++++++++++++++++++--- pgutils/pgutils_test.go | 78 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+), 5 deletions(-) create mode 100644 pgutils/pgutils_test.go diff --git a/go.mod b/go.mod index d9b5291..18ca497 100644 --- a/go.mod +++ b/go.mod @@ -7,15 +7,19 @@ require ( github.com/golang-migrate/migrate/v4 v4.18.1 github.com/jackc/pgconn v1.14.3 github.com/jackc/pgx/v5 v5.7.1 + github.com/stretchr/testify v1.9.0 github.com/xuri/excelize/v2 v2.9.0 ) require ( aidanwoods.dev/go-result v0.1.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/lib/pq v1.10.9 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect go.uber.org/atomic v1.7.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) require ( diff --git a/go.sum b/go.sum index c809d90..dc15c5d 100644 --- a/go.sum +++ b/go.sum @@ -55,6 +55,10 @@ github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs= github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA= github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= @@ -78,6 +82,8 @@ github.com/richardlehane/mscfb v1.0.4/go.mod h1:YzVpcZg9czvAuhk9T+a3avCpcFPMUWm7 github.com/richardlehane/msoleps v1.0.1/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg= github.com/richardlehane/msoleps v1.0.4 h1:WuESlvhX3gH2IHcd8UqyCuFY5yiq/GR/yqaSM/9/g00= github.com/richardlehane/msoleps v1.0.4/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= @@ -113,6 +119,8 @@ golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/pgutils/pgutils.go b/pgutils/pgutils.go index 02f4af4..ea6c459 100644 --- a/pgutils/pgutils.go +++ b/pgutils/pgutils.go @@ -2,19 +2,21 @@ package pgutils import ( "log/slog" + "math" + "math/big" "strconv" "github.com/jackc/pgx/v5/pgtype" ) func NumericToFloat64(n pgtype.Numeric) float64 { - value, err := n.Value() + val, err := n.Value() if err != nil { slog.Error("error getting numeric value", "error", err) return 0 } - strValue, ok := value.(string) + strValue, ok := val.(string) if !ok { slog.Error("error converting numeric value to string") return 0 @@ -29,10 +31,70 @@ func NumericToFloat64(n pgtype.Numeric) float64 { return floatValue } -func FloatToNumeric(number float64) (value pgtype.Numeric) { - parse := strconv.FormatFloat(number, 'f', -1, 64) +func NumericToInt64(n pgtype.Numeric) int64 { + return n.Int.Int64() * int64(math.Pow(10, float64(n.Exp))) +} + +func FloatToNumeric(number float64, precision int) (value pgtype.Numeric) { + parse := strconv.FormatFloat(number, 'f', precision, 64) + slog.Info("parse", "parse", parse) + if err := value.Scan(parse); err != nil { - slog.Error("error scanning float to numeric", "error", err) + slog.Error("error scanning numeric", "error", err) } return value } + +func AddNumeric(a, b pgtype.Numeric) pgtype.Numeric { + minExp := a.Exp + if b.Exp < minExp { + minExp = b.Exp + } + + aInt := new(big.Int).Set(a.Int) + bInt := new(big.Int).Set(b.Int) + + for a.Exp > minExp { + aInt.Mul(aInt, big.NewInt(10)) + a.Exp-- + } + for b.Exp > minExp { + bInt.Mul(bInt, big.NewInt(10)) + b.Exp-- + } + + resultado := new(big.Int).Add(aInt, bInt) + + return pgtype.Numeric{ + Int: resultado, + Exp: minExp, + Valid: true, + } +} + +func SubtractNumeric(a, b pgtype.Numeric) pgtype.Numeric { + minExp := a.Exp + if b.Exp < minExp { + minExp = b.Exp + } + + aInt := new(big.Int).Set(a.Int) + bInt := new(big.Int).Set(b.Int) + + for a.Exp > minExp { + aInt.Mul(aInt, big.NewInt(10)) + a.Exp-- + } + for b.Exp > minExp { + bInt.Mul(bInt, big.NewInt(10)) + b.Exp-- + } + + resultado := new(big.Int).Sub(aInt, bInt) + + return pgtype.Numeric{ + Int: resultado, + Exp: minExp, + Valid: true, + } +} diff --git a/pgutils/pgutils_test.go b/pgutils/pgutils_test.go new file mode 100644 index 0000000..d1a803e --- /dev/null +++ b/pgutils/pgutils_test.go @@ -0,0 +1,78 @@ +package pgutils + +import ( + "fmt" + "log/slog" + "math/big" + "testing" + + "github.com/jackc/pgx/v5/pgtype" + "github.com/stretchr/testify/assert" +) + +func Test_NumericToFloat(t *testing.T) { + tests := []struct { + given pgtype.Numeric + expected float64 + }{ + {given: pgtype.Numeric{Int: big.NewInt(0), Exp: 0, Valid: true}, expected: 0}, + {given: pgtype.Numeric{Int: big.NewInt(5), Exp: 0, Valid: true}, expected: 5}, + {given: pgtype.Numeric{Int: big.NewInt(10), Exp: 0, Valid: true}, expected: 10}, + {given: pgtype.Numeric{Int: big.NewInt(1000), Exp: -2, Valid: true}, expected: 10.00}, + {given: pgtype.Numeric{Int: big.NewInt(1000), Exp: -3, Valid: true}, expected: 1.000}, + {given: pgtype.Numeric{Int: big.NewInt(1000), Exp: -4, Valid: true}, expected: 0.1000}, + {given: pgtype.Numeric{Int: big.NewInt(2555), Exp: -2, Valid: true}, expected: 25.55}, + {given: pgtype.Numeric{Int: big.NewInt(-1), Exp: -2, Valid: true}, expected: -0.01}, + } + for _, test := range tests { + t.Run(fmt.Sprintf("given %v, expected %v", test.given, test.expected), func(t *testing.T) { + actual := NumericToFloat64(test.given) + assert.Equal(t, test.expected, actual) + }) + } + +} + +func Test_FloatToNumeric(t *testing.T) { + tests := []struct { + given float64 + expected pgtype.Numeric + precision int + }{ + {given: 0.0, expected: pgtype.Numeric{Int: big.NewInt(0), Exp: 0, Valid: true}, precision: 0}, + {given: 25.50, expected: pgtype.Numeric{Int: big.NewInt(2550), Exp: -2, Valid: true}, precision: 2}, + {given: 100.0, expected: pgtype.Numeric{Int: big.NewInt(1), Exp: 2, Valid: true}, precision: 0}, + {given: 0.0001, expected: pgtype.Numeric{Int: big.NewInt(0), Exp: -2, Valid: true}, precision: 2}, + {given: 0.0001, expected: pgtype.Numeric{Int: big.NewInt(1), Exp: -4, Valid: true}, precision: 4}, + } + + for _, test := range tests { + t.Run(fmt.Sprintf("given %v, expected %v", test.given, test.expected), func(t *testing.T) { + actual := FloatToNumeric(test.given, test.precision) + assert.Equal(t, test.expected, actual) + }) + } +} + +func Test_AddNumeric(t *testing.T) { + valueA := pgtype.Numeric{Int: big.NewInt(1), Exp: -3, Valid: true} + valueB := pgtype.Numeric{Int: big.NewInt(2), Exp: -2, Valid: true} + + slog.Info("valueA", "valueA", valueA) + slog.Info("valueB", "valueB", valueB) + + actual := AddNumeric(valueA, valueB) + + slog.Info("actual", "actual", actual) + assert.Equal(t, pgtype.Numeric{Int: big.NewInt(21), Exp: -3, Valid: true}, actual) +} + +func Test_SubtractNumeric(t *testing.T) { + valueA := pgtype.Numeric{Int: big.NewInt(1), Exp: -3, Valid: true} + valueB := pgtype.Numeric{Int: big.NewInt(2), Exp: -2, Valid: true} + + actual := SubtractNumeric(valueA, valueB) + + slog.Info("actual", "actual", actual) + assert.Equal(t, pgtype.Numeric{Int: big.NewInt(-19), Exp: -3, Valid: true}, actual) +}