Skip to content

Commit

Permalink
add NewNullDecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensayshi committed Oct 12, 2021
1 parent cc4584f commit 38c8188
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions decimal.go
Expand Up @@ -1411,6 +1411,13 @@ type NullDecimal struct {
Valid bool
}

func NewNullDecimal(d Decimal) NullDecimal {
return NullDecimal{
Decimal: d,
Valid: true,
}
}

// Scan implements the sql.Scanner interface for database deserialization.
func (d *NullDecimal) Scan(value interface{}) error {
if value == nil {
Expand Down
12 changes: 12 additions & 0 deletions decimal_test.go
Expand Up @@ -3081,6 +3081,18 @@ func TestTan(t *testing.T) {
}
}

func TestNewNullDecimal(t *testing.T) {
d := NewFromInt(1)
nd := NewNullDecimal(d)

if !nd.Valid {
t.Errorf("expected NullDecimal to be valid")
}
if nd.Decimal != d {
t.Errorf("expected NullDecimal to hold the provided Decimal")
}
}

func ExampleNewFromFloat32() {
fmt.Println(NewFromFloat32(123.123123123123).String())
fmt.Println(NewFromFloat32(.123123123123123).String())
Expand Down

0 comments on commit 38c8188

Please sign in to comment.