Add NewNullDecimal init method (#234)

This commit is contained in:
Ruben de Vries 2021-10-12 11:07:45 +02:00 committed by GitHub
parent 013e52d4e9
commit 3259e0a2de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View file

@ -1603,6 +1603,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 {

View file

@ -3269,6 +3269,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())