Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Method Scan, Value fo fixedpoint #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions fixedpoint/fixedpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package fixedpoint

import (
"database/sql/driver"
"math"
"math/big"
"strings"
Expand Down Expand Up @@ -414,3 +415,30 @@ func (f FixedPoint) IsOverPrecision(precision int) bool {
precisionCutoff := NewFromBigIntExp(big.NewInt(10), int32(-precision))
return f.Abs().Cmp(precisionCutoff) < 0
}

// Scan implements the sql.Scanner interface for database deserialization.
func (f *FixedPoint) Scan(value interface{}) error {
if value == nil {
f.d.Valid = false
return nil
}

switch v := value.(type) {
case decimal.Decimal:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add case decimal.NullDecimal, because why not

// Directly handle decimal.Decimal type
f.d.Decimal = v
f.d.Valid = true
return nil
default:
// Use the normal NullDecimal scan for other types
return f.d.Scan(value)
}
}

// Value implements the driver.Valuer interface for database serialization.
func (f FixedPoint) Value() (driver.Value, error) {
if f.IsValid() {
return nil, nil
}
return f.d.Decimal.Value()
}
Loading