Skip to content

Commit 08eea6c

Browse files
committed
fix: scan from db fixed
1 parent c85e16c commit 08eea6c

17 files changed

+84
-40
lines changed

bool.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ type Bool struct {
1111
nullValue *sql.NullBool
1212
}
1313

14-
func (s Bool) String() string {
14+
func (s *Bool) String() string {
1515
if s.nullValue != nil {
1616
return fmt.Sprint(s.nullValue.Bool)
1717
}
1818

1919
return ""
2020
}
2121

22-
func NewBool(value bool) Bool {
23-
return Bool{
22+
func NewBool(value bool) *Bool {
23+
return &Bool{
2424
nullValue: &sql.NullBool{
2525
Valid: true,
2626
Bool: value,
2727
},
2828
}
2929
}
3030

31-
func (s Bool) Get() bool {
31+
func (s *Bool) Get() bool {
3232
if s.nullValue != nil {
3333
return s.nullValue.Bool
3434
}
@@ -45,6 +45,11 @@ func (s *Bool) Value() (driver.Value, error) {
4545
}
4646

4747
func (s *Bool) Scan(value any) error {
48+
s.nullValue = &sql.NullBool{
49+
Bool: false,
50+
Valid: false,
51+
}
52+
4853
if err := s.nullValue.Scan(value); err != nil {
4954
return err
5055
}
@@ -62,6 +67,7 @@ func (s *Bool) UnmarshalJSON(bytes []byte) error {
6267
if err := json.Unmarshal(bytes, &s.nullValue.Bool); err != nil {
6368
return err
6469
}
70+
s.nullValue.Valid = true
6571
}
6672

6773
return nil

byte.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func (s Byte) String() string {
1919
return ""
2020
}
2121

22-
func NewByte(value byte) Byte {
23-
return Byte{
22+
func NewByte(value byte) *Byte {
23+
return &Byte{
2424
nullValue: &sql.NullByte{
2525
Valid: true,
2626
Byte: value,
@@ -36,7 +36,7 @@ func (s Byte) Get() byte {
3636
return 0
3737
}
3838

39-
func (s *Byte) Value() (driver.Value, error) {
39+
func (s Byte) Value() (driver.Value, error) {
4040
if s.nullValue != nil {
4141
return s.nullValue.Value()
4242
}
@@ -45,11 +45,12 @@ func (s *Byte) Value() (driver.Value, error) {
4545
}
4646

4747
func (s *Byte) Scan(value any) error {
48-
if err := s.nullValue.Scan(value); err != nil {
49-
return err
48+
s.nullValue = &sql.NullByte{
49+
Byte: 0,
50+
Valid: false,
5051
}
5152

52-
return nil
53+
return s.nullValue.Scan(value)
5354
}
5455

5556
func (s *Byte) UnmarshalJSON(bytes []byte) error {
@@ -62,6 +63,7 @@ func (s *Byte) UnmarshalJSON(bytes []byte) error {
6263
if err := json.Unmarshal(bytes, &s.nullValue.Byte); err != nil {
6364
return err
6465
}
66+
s.nullValue.Valid = true
6567
}
6668

6769
return nil

float32.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func (s Float32) String() string {
1919
return ""
2020
}
2121

22-
func NewFloat32(value float32) Float32 {
23-
return Float32{
22+
func NewFloat32(value float32) *Float32 {
23+
return &Float32{
2424
nullValue: &sql.NullFloat64{
2525
Valid: true,
2626
Float64: float64(value),
@@ -36,7 +36,7 @@ func (s Float32) Get() float32 {
3636
return 0
3737
}
3838

39-
func (s *Float32) Value() (driver.Value, error) {
39+
func (s Float32) Value() (driver.Value, error) {
4040
if s.nullValue != nil {
4141
return s.nullValue.Value()
4242
}
@@ -45,6 +45,10 @@ func (s *Float32) Value() (driver.Value, error) {
4545
}
4646

4747
func (s *Float32) Scan(value any) error {
48+
s.nullValue = &sql.NullFloat64{
49+
Valid: false,
50+
Float64: 0,
51+
}
4852
if err := s.nullValue.Scan(value); err != nil {
4953
return err
5054
}
@@ -62,6 +66,7 @@ func (s *Float32) UnmarshalJSON(bytes []byte) error {
6266
if err := json.Unmarshal(bytes, &s.nullValue.Float64); err != nil {
6367
return err
6468
}
69+
s.nullValue.Valid = true
6570
}
6671

6772
return nil

float32_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
func TestFloat32(t *testing.T) {
99
testCases := []struct {
1010
name string
11-
s Float32
11+
s *Float32
1212
}{
1313
{
1414
name: "Not null marshaller",

float64.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func (s Float64) String() string {
1919
return ""
2020
}
2121

22-
func NewFloat64(value float64) Float64 {
23-
return Float64{
22+
func NewFloat64(value float64) *Float64 {
23+
return &Float64{
2424
nullValue: &sql.NullFloat64{
2525
Valid: true,
2626
Float64: value,
@@ -45,6 +45,10 @@ func (s *Float64) Value() (driver.Value, error) {
4545
}
4646

4747
func (s *Float64) Scan(value any) error {
48+
s.nullValue = &sql.NullFloat64{
49+
Valid: false,
50+
Float64: 0,
51+
}
4852
if err := s.nullValue.Scan(value); err != nil {
4953
return err
5054
}
@@ -62,6 +66,7 @@ func (s *Float64) UnmarshalJSON(bytes []byte) error {
6266
if err := json.Unmarshal(bytes, &s.nullValue.Float64); err != nil {
6367
return err
6468
}
69+
s.nullValue.Valid = true
6570
}
6671

6772
return nil

float64_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
func TestFloat64(t *testing.T) {
99
testCases := []struct {
1010
name string
11-
s Float64
11+
s *Float64
1212
}{
1313
{
1414
name: "Not null marshaller",

int.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func (s Int) String() string {
1919
return ""
2020
}
2121

22-
func NewInt(value int) Int {
23-
return Int{
22+
func NewInt(value int) *Int {
23+
return &Int{
2424
nullValue: &sql.NullInt32{
2525
Valid: true,
2626
Int32: int32(value),
@@ -36,7 +36,7 @@ func (s Int) Get() int {
3636
return 0
3737
}
3838

39-
func (s *Int) Value() (driver.Value, error) {
39+
func (s Int) Value() (driver.Value, error) {
4040
if s.nullValue != nil {
4141
return s.nullValue.Value()
4242
}
@@ -45,6 +45,10 @@ func (s *Int) Value() (driver.Value, error) {
4545
}
4646

4747
func (s *Int) Scan(value any) error {
48+
s.nullValue = &sql.NullInt32{
49+
Int32: 0,
50+
Valid: false,
51+
}
4852
if err := s.nullValue.Scan(value); err != nil {
4953
return err
5054
}
@@ -62,6 +66,7 @@ func (s *Int) UnmarshalJSON(bytes []byte) error {
6266
if err := json.Unmarshal(bytes, &s.nullValue.Int32); err != nil {
6367
return err
6468
}
69+
s.nullValue.Valid = true
6570
}
6671

6772
return nil

int16.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func (s Int16) String() string {
1919
return ""
2020
}
2121

22-
func NewInt16(value int16) Int16 {
23-
return Int16{
22+
func NewInt16(value int16) *Int16 {
23+
return &Int16{
2424
nullValue: &sql.NullInt16{
2525
Valid: true,
2626
Int16: value,
@@ -36,7 +36,7 @@ func (s Int16) Get() int16 {
3636
return 0
3737
}
3838

39-
func (s *Int16) Value() (driver.Value, error) {
39+
func (s Int16) Value() (driver.Value, error) {
4040
if s.nullValue != nil {
4141
return s.nullValue.Value()
4242
}
@@ -45,6 +45,10 @@ func (s *Int16) Value() (driver.Value, error) {
4545
}
4646

4747
func (s *Int16) Scan(value any) error {
48+
s.nullValue = &sql.NullInt16{
49+
Int16: 0,
50+
Valid: false,
51+
}
4852
if err := s.nullValue.Scan(value); err != nil {
4953
return err
5054
}
@@ -62,6 +66,7 @@ func (s *Int16) UnmarshalJSON(bytes []byte) error {
6266
if err := json.Unmarshal(bytes, &s.nullValue.Int16); err != nil {
6367
return err
6468
}
69+
s.nullValue.Valid = true
6570
}
6671

6772
return nil

int16_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func TestInt16(t *testing.T) {
1010
testCases := []struct {
1111
name string
12-
s Int16
12+
s *Int16
1313
marshalledBytes []byte
1414
}{
1515
{

int32.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func (s Int32) String() string {
1919
return ""
2020
}
2121

22-
func NewInt32(value int32) Int32 {
23-
return Int32{
22+
func NewInt32(value int32) *Int32 {
23+
return &Int32{
2424
nullValue: &sql.NullInt32{
2525
Valid: true,
2626
Int32: value,
@@ -36,7 +36,7 @@ func (s Int32) Get() int32 {
3636
return 0
3737
}
3838

39-
func (s *Int32) Value() (driver.Value, error) {
39+
func (s Int32) Value() (driver.Value, error) {
4040
if s.nullValue != nil {
4141
return s.nullValue.Value()
4242
}
@@ -45,6 +45,10 @@ func (s *Int32) Value() (driver.Value, error) {
4545
}
4646

4747
func (s *Int32) Scan(value any) error {
48+
s.nullValue = &sql.NullInt32{
49+
Int32: 0,
50+
Valid: false,
51+
}
4852
if err := s.nullValue.Scan(value); err != nil {
4953
return err
5054
}
@@ -62,6 +66,7 @@ func (s *Int32) UnmarshalJSON(bytes []byte) error {
6266
if err := json.Unmarshal(bytes, &s.nullValue.Int32); err != nil {
6367
return err
6468
}
69+
s.nullValue.Valid = true
6570
}
6671

6772
return nil

int32_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func TestInt32(t *testing.T) {
1010
testCases := []struct {
1111
name string
12-
s Int32
12+
s *Int32
1313
marshalledBytes []byte
1414
}{
1515
{

int64.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func (s Int64) String() string {
1919
return ""
2020
}
2121

22-
func NewInt64(value int64) Int64 {
23-
return Int64{
22+
func NewInt64(value int64) *Int64 {
23+
return &Int64{
2424
nullValue: &sql.NullInt64{
2525
Valid: true,
2626
Int64: value,
@@ -36,7 +36,7 @@ func (s Int64) Get() int64 {
3636
return 0
3737
}
3838

39-
func (s *Int64) Value() (driver.Value, error) {
39+
func (s Int64) Value() (driver.Value, error) {
4040
if s.nullValue != nil {
4141
return s.nullValue.Value()
4242
}
@@ -62,6 +62,7 @@ func (s *Int64) UnmarshalJSON(bytes []byte) error {
6262
if err := json.Unmarshal(bytes, &s.nullValue.Int64); err != nil {
6363
return err
6464
}
65+
s.nullValue.Valid = true
6566
}
6667

6768
return nil

int64_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func TestInt64(t *testing.T) {
1010
testCases := []struct {
1111
name string
12-
s Int64
12+
s *Int64
1313
marshalledBytes []byte
1414
}{
1515
{

int_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func TestInt(t *testing.T) {
1010
testCases := []struct {
1111
name string
12-
s Int
12+
s *Int
1313
marshalledBytes []byte
1414
}{
1515
{

string.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ func (s String) String() string {
1818
return ""
1919
}
2020

21-
func NewString(value string) String {
22-
return String{
21+
func NewString(value string) *String {
22+
return &String{
2323
nullValue: &sql.NullString{
2424
Valid: value != "",
2525
String: value,
@@ -35,7 +35,7 @@ func (s String) Get() string {
3535
return ""
3636
}
3737

38-
func (s *String) Value() (driver.Value, error) {
38+
func (s String) Value() (driver.Value, error) {
3939
if s.nullValue != nil {
4040
return s.nullValue.Value()
4141
}
@@ -44,6 +44,10 @@ func (s *String) Value() (driver.Value, error) {
4444
}
4545

4646
func (s *String) Scan(value any) error {
47+
s.nullValue = &sql.NullString{
48+
String: "",
49+
Valid: false,
50+
}
4751
if err := s.nullValue.Scan(value); err != nil {
4852
return err
4953
}
@@ -61,6 +65,7 @@ func (s *String) UnmarshalJSON(bytes []byte) error {
6165
if err := json.Unmarshal(bytes, &s.nullValue.String); err != nil {
6266
return err
6367
}
68+
s.nullValue.Valid = true
6469
}
6570

6671
return nil

0 commit comments

Comments
 (0)