File tree 4 files changed +232
-0
lines changed
4 files changed +232
-0
lines changed Original file line number Diff line number Diff line change
1
+ package null
2
+
3
+ import (
4
+ "database/sql"
5
+ "database/sql/driver"
6
+ "encoding/json"
7
+ "fmt"
8
+ )
9
+
10
+ type Bool struct {
11
+ nullValue * sql.NullBool
12
+ }
13
+
14
+ func (s Bool ) String () string {
15
+ if s .nullValue != nil {
16
+ return fmt .Sprint (s .nullValue .Bool )
17
+ }
18
+
19
+ return ""
20
+ }
21
+
22
+ func NewBool (value bool ) Bool {
23
+ return Bool {
24
+ nullValue : & sql.NullBool {
25
+ Valid : true ,
26
+ Bool : value ,
27
+ },
28
+ }
29
+ }
30
+
31
+ func (s Bool ) Get () bool {
32
+ if s .nullValue != nil {
33
+ return s .nullValue .Bool
34
+ }
35
+
36
+ return false
37
+ }
38
+
39
+ func (s * Bool ) Value () (driver.Value , error ) {
40
+ if s .nullValue != nil {
41
+ return s .nullValue .Value ()
42
+ }
43
+
44
+ return nil , nil
45
+ }
46
+
47
+ func (s * Bool ) Scan (value any ) error {
48
+ if err := s .nullValue .Scan (value ); err != nil {
49
+ return err
50
+ }
51
+
52
+ return nil
53
+ }
54
+
55
+ func (s * Bool ) UnmarshalJSON (bytes []byte ) error {
56
+ s .nullValue = & sql.NullBool {
57
+ Bool : false ,
58
+ Valid : false ,
59
+ }
60
+
61
+ if len (bytes ) > 0 {
62
+ if err := json .Unmarshal (bytes , & s .nullValue .Bool ); err != nil {
63
+ return err
64
+ }
65
+ }
66
+
67
+ return nil
68
+ }
69
+
70
+ func (s Bool ) MarshalJSON () ([]byte , error ) {
71
+ if s .nullValue .Valid {
72
+ return json .Marshal (s .nullValue .Bool )
73
+ }
74
+
75
+ return json .Marshal (nil )
76
+ }
Original file line number Diff line number Diff line change
1
+ package null
2
+
3
+ import (
4
+ "database/sql"
5
+ "database/sql/driver"
6
+ "encoding/json"
7
+ "fmt"
8
+ )
9
+
10
+ type Byte struct {
11
+ nullValue * sql.NullByte
12
+ }
13
+
14
+ func (s Byte ) String () string {
15
+ if s .nullValue != nil {
16
+ return fmt .Sprint (s .nullValue .Byte )
17
+ }
18
+
19
+ return ""
20
+ }
21
+
22
+ func NewByte (value byte ) Byte {
23
+ return Byte {
24
+ nullValue : & sql.NullByte {
25
+ Valid : true ,
26
+ Byte : value ,
27
+ },
28
+ }
29
+ }
30
+
31
+ func (s Byte ) Get () byte {
32
+ if s .nullValue != nil {
33
+ return s .nullValue .Byte
34
+ }
35
+
36
+ return 0
37
+ }
38
+
39
+ func (s * Byte ) Value () (driver.Value , error ) {
40
+ if s .nullValue != nil {
41
+ return s .nullValue .Value ()
42
+ }
43
+
44
+ return nil , nil
45
+ }
46
+
47
+ func (s * Byte ) Scan (value any ) error {
48
+ if err := s .nullValue .Scan (value ); err != nil {
49
+ return err
50
+ }
51
+
52
+ return nil
53
+ }
54
+
55
+ func (s * Byte ) UnmarshalJSON (bytes []byte ) error {
56
+ s .nullValue = & sql.NullByte {
57
+ Byte : 0 ,
58
+ Valid : false ,
59
+ }
60
+
61
+ if len (bytes ) > 0 {
62
+ if err := json .Unmarshal (bytes , & s .nullValue .Byte ); err != nil {
63
+ return err
64
+ }
65
+ }
66
+
67
+ return nil
68
+ }
69
+
70
+ func (s Byte ) MarshalJSON () ([]byte , error ) {
71
+ if s .nullValue .Valid {
72
+ return json .Marshal (s .nullValue .Byte )
73
+ }
74
+
75
+ return json .Marshal (nil )
76
+ }
Original file line number Diff line number Diff line change
1
+ module github.com/abdivasiyev/go-null
2
+
3
+ go 1.18
Original file line number Diff line number Diff line change
1
+ package null
2
+
3
+ import (
4
+ "database/sql"
5
+ "database/sql/driver"
6
+ "encoding/json"
7
+ "fmt"
8
+ "time"
9
+ )
10
+
11
+ type Time struct {
12
+ nullValue * sql.NullTime
13
+ }
14
+
15
+ func (s Time ) String () string {
16
+ if s .nullValue != nil {
17
+ return fmt .Sprint (s .nullValue .Time )
18
+ }
19
+
20
+ return ""
21
+ }
22
+
23
+ func NewTime (value time.Time ) Time {
24
+ return Time {
25
+ nullValue : & sql.NullTime {
26
+ Valid : true ,
27
+ Time : value ,
28
+ },
29
+ }
30
+ }
31
+
32
+ func (s Time ) Get () time.Time {
33
+ if s .nullValue != nil {
34
+ return s .nullValue .Time
35
+ }
36
+
37
+ return time.Time {}
38
+ }
39
+
40
+ func (s * Time ) Value () (driver.Value , error ) {
41
+ if s .nullValue != nil {
42
+ return s .nullValue .Value ()
43
+ }
44
+
45
+ return nil , nil
46
+ }
47
+
48
+ func (s * Time ) Scan (value any ) error {
49
+ if err := s .nullValue .Scan (value ); err != nil {
50
+ return err
51
+ }
52
+
53
+ return nil
54
+ }
55
+
56
+ func (s * Time ) UnmarshalJSON (bytes []byte ) error {
57
+ s .nullValue = & sql.NullTime {
58
+ Time : time.Time {},
59
+ Valid : false ,
60
+ }
61
+
62
+ if len (bytes ) > 0 {
63
+ if err := json .Unmarshal (bytes , & s .nullValue .Time ); err != nil {
64
+ return err
65
+ }
66
+ }
67
+
68
+ return nil
69
+ }
70
+
71
+ func (s Time ) MarshalJSON () ([]byte , error ) {
72
+ if s .nullValue .Valid {
73
+ return json .Marshal (s .nullValue .Time )
74
+ }
75
+
76
+ return json .Marshal (nil )
77
+ }
You can’t perform that action at this time.
0 commit comments