-
Notifications
You must be signed in to change notification settings - Fork 0
/
null_big_rat.go
54 lines (44 loc) · 986 Bytes
/
null_big_rat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package sqlutil
import (
"database/sql/driver"
"encoding/json"
)
//NullBigRat is a wrapper for BigRat that allows nil and satisfies:
//json.Marshaler
//sql.Scanner
//driver.Valuer
type NullBigRat struct {
BigRat BigRat
Valid bool
}
//MarshalJSON marshals nested BigRat struct or nil if invalid
func (nbr *NullBigRat) MarshalJSON() ([]byte, error) {
if !nbr.Valid {
return json.Marshal(nil)
}
return json.Marshal(&nbr.BigRat)
}
//Scan implements sql.Scanner
//
//Accepts nil, proxies everything else to nested BigRat
func (nbr *NullBigRat) Scan(value interface{}) (err error) {
nbr.BigRat = BigRat{}
if value == nil {
nbr.Valid = false
return nil
}
err = nbr.BigRat.Scan(value)
if err == nil {
nbr.Valid = true
}
return err
}
//Value implements driver.Valuer
//
//Returns nil if invalid, otherwise proxies to nested BigRat
func (nbr NullBigRat) Value() (value driver.Value, err error) {
if !nbr.Valid {
return nil, nil
}
return nbr.BigRat.Value()
}