Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ func (n *NullBool) Scan(src any) error {
n.Valid = reflect.TypeOf(src) != nil
return nil
}

func (n *NullBool) IsZero() bool {
return !n.Valid
}
4 changes: 4 additions & 0 deletions byte.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ func (n *NullByte) Scan(src any) error {
n.Valid = reflect.TypeOf(src) != nil
return nil
}

func (n *NullByte) IsZero() bool {
return !n.Valid
}
4 changes: 4 additions & 0 deletions float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ func (n *NullFloat64) Scan(src any) error {
n.Valid = reflect.TypeOf(src) != nil
return nil
}

func (n *NullFloat64) IsZero() bool {
return !n.Valid
}
4 changes: 4 additions & 0 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ func (n *Null[T]) Scan(src any) error {
n.Valid = reflect.TypeOf(src) != nil
return nil
}

func (n *Null[T]) IsZero() bool {
return !n.Valid
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gkits/sqlnull

go 1.23.2
go 1.24

require (
github.com/mattn/go-sqlite3 v1.14.24
Expand Down
4 changes: 4 additions & 0 deletions int16.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ func (n *NullInt16) Scan(src any) error {
n.Valid = reflect.TypeOf(src) != nil
return nil
}

func (n *NullInt16) IsZero() bool {
return !n.Valid
}
4 changes: 4 additions & 0 deletions int32.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ func (n *NullInt32) Scan(src any) error {
n.Valid = reflect.TypeOf(src) != nil
return nil
}

func (n *NullInt32) IsZero() bool {
return !n.Valid
}
4 changes: 4 additions & 0 deletions int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ func (n *NullInt64) Scan(src any) error {
n.Valid = reflect.TypeOf(src) != nil
return nil
}

func (n *NullInt64) IsZero() bool {
return !n.Valid
}
89 changes: 87 additions & 2 deletions sqlnull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ type target struct {
Time sqlnull.NullTime `json:"time"`
}

type targetOmitZero struct {
Generic sqlnull.Null[string] `json:"generic,omitzero"`
String sqlnull.NullString `json:"string,omitzero"`
Bool sqlnull.NullBool `json:"bool,omitzero"`
Byte sqlnull.NullByte `json:"byte,omitzero"`
Int16 sqlnull.NullInt16 `json:"int16,omitzero"`
Int32 sqlnull.NullInt32 `json:"int32,omitzero"`
Int64 sqlnull.NullInt64 `json:"int64,omitzero"`
Float64 sqlnull.NullFloat64 `json:"float64,omitzero"`
Time sqlnull.NullTime `json:"time,omitzero"`
}

func Test_MarshalJSON(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -98,7 +110,7 @@ func Test_UnmarshalJSON(t *testing.T) {
want target
}{
{
name: "successfully marshal with values",
name: "successfully unmarshal with values",
in: []byte(`{
"generic": "generic",
"string": "string",
Expand All @@ -123,7 +135,7 @@ func Test_UnmarshalJSON(t *testing.T) {
},
},
{
name: "successfully marshal with null",
name: "successfully unmarshal with null",
in: []byte(`{
"generic": null,
"string": null,
Expand All @@ -147,6 +159,21 @@ func Test_UnmarshalJSON(t *testing.T) {
Time: sqlnull.NullTime{Time: time.Time{}, Valid: false},
},
},
{
name: "successfully unmarshal with empty json",
in: []byte(`{}`),
want: target{
Generic: sqlnull.Null[string]{V: "", Valid: false},
String: sqlnull.NullString{String: "", Valid: false},
Bool: sqlnull.NullBool{Bool: false, Valid: false},
Byte: sqlnull.NullByte{Byte: 0, Valid: false},
Int16: sqlnull.NullInt16{Int16: 0, Valid: false},
Int32: sqlnull.NullInt32{Int32: 0, Valid: false},
Int64: sqlnull.NullInt64{Int64: 0, Valid: false},
Float64: sqlnull.NullFloat64{Float64: 0, Valid: false},
Time: sqlnull.NullTime{Time: time.Time{}, Valid: false},
},
},
}

for _, c := range cases {
Expand Down Expand Up @@ -244,3 +271,61 @@ func Test_Scan(t *testing.T) {
})
}
}

func Test_MarshalJSONOmitZero(t *testing.T) {
cases := []struct {
name string
in targetOmitZero
want string
}{
{
name: "successfully marshal with values",
in: targetOmitZero{
Generic: sqlnull.Null[string]{V: "generic", Valid: true},
String: sqlnull.NullString{String: "string", Valid: true},
Bool: sqlnull.NullBool{Bool: true, Valid: true},
Byte: sqlnull.NullByte{Byte: 255, Valid: true},
Int16: sqlnull.NullInt16{Int16: 16, Valid: true},
Int32: sqlnull.NullInt32{Int32: 32, Valid: true},
Int64: sqlnull.NullInt64{Int64: 64, Valid: true},
Float64: sqlnull.NullFloat64{Float64: 64.6464, Valid: true},
Time: sqlnull.NullTime{Time: time.Date(2024, 10, 23, 17, 50, 0, 0, time.UTC), Valid: true},
},
want: `{
"generic": "generic",
"string": "string",
"bool": true,
"byte": 255,
"int16": 16,
"int32": 32,
"int64": 64,
"float64": 64.6464,
"time": "2024-10-23T17:50:00Z"
}`,
},
{
name: "successfully marshal empty json",
in: targetOmitZero{
Generic: sqlnull.Null[string]{V: "generic", Valid: false},
String: sqlnull.NullString{String: "string", Valid: false},
Bool: sqlnull.NullBool{Bool: true, Valid: false},
Byte: sqlnull.NullByte{Byte: 255, Valid: false},
Int16: sqlnull.NullInt16{Int16: 16, Valid: false},
Int32: sqlnull.NullInt32{Int32: 32, Valid: false},
Int64: sqlnull.NullInt64{Int64: 64, Valid: false},
Float64: sqlnull.NullFloat64{Float64: 64.6464, Valid: false},
Time: sqlnull.NullTime{Time: time.Date(2024, 10, 23, 17, 50, 0, 0, time.UTC), Valid: false},
},
want: `{}`,
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got, err := json.Marshal(c.in)
t.Log(string(got))
require.NoError(t, err)
assert.JSONEq(t, c.want, string(got))
})
}
}
4 changes: 4 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ func (n *NullString) Scan(src any) error {
n.Valid = reflect.TypeOf(src) != nil
return nil
}

func (n *NullString) IsZero() bool {
return !n.Valid
}
4 changes: 4 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ func (n *NullTime) Scan(src any) error {
n.Valid = reflect.TypeOf(src) != nil
return nil
}

func (n *NullTime) IsZero() bool {
return !n.Valid
}