Skip to content

Commit

Permalink
Change database format into decimal integer pair
Browse files Browse the repository at this point in the history
  • Loading branch information
Dadido3 committed Apr 28, 2023
1 parent 07cf1b1 commit a437136
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
8 changes: 4 additions & 4 deletions db-test/gorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestCompositeType(t *testing.T) {
}

// Create custom composite type.
if err := gormDB.Exec("DROP TYPE IF EXISTS d3money; CREATE TYPE d3money AS (amount DECIMAL, currency VARCHAR);").Error; err != nil {
if err := gormDB.Exec("DROP TYPE IF EXISTS d3money; CREATE TYPE d3money AS (amount DECIMAL, currency INTEGER);").Error; err != nil {
t.Fatalf("Failed to create d3money composite type: %v", err)
}

Expand Down Expand Up @@ -118,15 +118,15 @@ func TestCompositeType(t *testing.T) {
// Test direct access to the fields of the composite type.
type CompositeFields struct {
Amount decimal.Decimal
Currency string
Currency int32
}

var a1ReadFields CompositeFields
if err := gormDB.Raw("SELECT (balance).amount, (balance).currency FROM test_account_composite_types WHERE id = ?", 1).Scan(&a1ReadFields).Error; err != nil {
t.Errorf("Failed to query account 1: %v", err)
}

a1ExpectedFields := CompositeFields{a1.Balance.Decimal(), a1.Balance.Currency().UniqueCode()}
a1ExpectedFields := CompositeFields{a1.Balance.Decimal(), a1.Balance.Currency().UniqueID()}
if !reflect.DeepEqual(a1ReadFields, a1ExpectedFields) {
t.Errorf("Queried balance fields %+v don't match expected fields %+v", a1ReadFields, a1ExpectedFields)
}
Expand All @@ -136,7 +136,7 @@ func TestCompositeType(t *testing.T) {
t.Errorf("Failed to query account 2: %v", err)
}

a2ExpectedFields := CompositeFields{a2.Balance.Decimal(), ""}
a2ExpectedFields := CompositeFields{a2.Balance.Decimal(), 0}
if !reflect.DeepEqual(a2ReadFields, a2ExpectedFields) {
t.Errorf("Queried balance fields %+v don't match expected fields %+v", a2ReadFields, a2ExpectedFields)
}
Expand Down
47 changes: 41 additions & 6 deletions value-databindings.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022 David Vogel
// Copyright (c) 2021-2023 David Vogel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
Expand All @@ -10,6 +10,7 @@ import (
"encoding/binary"
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/shopspring/decimal"
Expand Down Expand Up @@ -123,7 +124,7 @@ func (v *Value) GobDecode(data []byte) error {
func (v Value) Value() (driver.Value, error) {
if v.currency != nil {
// Output "Amount UniqueCode" pair.
return "(" + v.amount.String() + "," + v.currency.UniqueCode() + ")", nil
return "(" + v.amount.String() + "," + strconv.Itoa(int(v.currency.UniqueID())) + ")", nil
}

// If there is no currency output only "Amount".
Expand All @@ -138,13 +139,47 @@ func (v *Value) Scan(value interface{}) error {
}

trimmed := strings.Trim(str, "()")
replaced := strings.ReplaceAll(trimmed, ",", " ")
amount, newCur, err := parse(strings.TrimRight(replaced, " "), Currencies, nil)

var amountStr, curStr string
var currency Currency

// Parse expression.
split := strings.Split(trimmed, ",")
switch len(split) {
case 1:
// String contains an amount string.
amountStr = strings.Trim(split[0], " ")

case 2:
// String contains an amount string + unique currency code.
amountStr, curStr = strings.Trim(split[0], " "), strings.Trim(split[1], " ")

if curStr != "" {
// Look in global collection for the currency.
uniqueID64, err := strconv.ParseInt(curStr, 10, 32)
if err != nil {
return fmt.Errorf("failed to parse currency ID from database field: %w", err)
}
uniqueID := int32(uniqueID64)
currency = Currencies.ByUniqueID(uniqueID)

// If there is no match, return error.
if currency == nil {
return &ErrorCantFindUniqueID{uniqueID}
}
}

default:
return fmt.Errorf("input string %q contains too many spaces", str)
}

// Parse amount string.
amount, err := decimal.NewFromString(amountStr)
if err != nil {
return fmt.Errorf("failed to parse string %q: %w", str, err)
return err
}

v.amount, v.currency = amount, newCur
v.amount, v.currency = amount, currency

return nil
}
Expand Down

0 comments on commit a437136

Please sign in to comment.