Skip to content

Commit 99fd141

Browse files
committed
Merge branch 'is-set-fixes'
2 parents 7353bec + 988f754 commit 99fd141

39 files changed

+577
-124
lines changed

bool.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import (
1313
type Bool struct {
1414
Bool bool
1515
Valid bool
16+
Set bool
1617
}
1718

1819
// NewBool creates a new Bool
19-
func NewBool(b bool, valid bool) Bool {
20+
func NewBool(b, valid bool) Bool {
2021
return Bool{
2122
Bool: b,
2223
Valid: valid,
24+
Set: true,
2325
}
2426
}
2527

@@ -36,8 +38,21 @@ func BoolFromPtr(b *bool) Bool {
3638
return NewBool(*b, true)
3739
}
3840

41+
// IsValid returns true if this carries and explicit value and
42+
// is not null.
43+
func (b Bool) IsValid() bool {
44+
return b.Set && b.Valid
45+
}
46+
47+
// IsSet returns true if this carries an explicit value (null inclusive)
48+
func (b Bool) IsSet() bool {
49+
return b.Set
50+
}
51+
3952
// UnmarshalJSON implements json.Unmarshaler.
4053
func (b *Bool) UnmarshalJSON(data []byte) error {
54+
b.Set = true
55+
4156
if bytes.Equal(data, NullBytes) {
4257
b.Bool = false
4358
b.Valid = false
@@ -54,7 +69,8 @@ func (b *Bool) UnmarshalJSON(data []byte) error {
5469

5570
// UnmarshalText implements encoding.TextUnmarshaler.
5671
func (b *Bool) UnmarshalText(text []byte) error {
57-
if text == nil || len(text) == 0 {
72+
b.Set = true
73+
if len(text) == 0 {
5874
b.Valid = false
5975
return nil
6076
}
@@ -99,6 +115,7 @@ func (b Bool) MarshalText() ([]byte, error) {
99115
func (b *Bool) SetValid(v bool) {
100116
b.Bool = v
101117
b.Valid = true
118+
b.Set = true
102119
}
103120

104121
// Ptr returns a pointer to this Bool's value, or a nil pointer if this Bool is null.
@@ -117,10 +134,10 @@ func (b Bool) IsZero() bool {
117134
// Scan implements the Scanner interface.
118135
func (b *Bool) Scan(value interface{}) error {
119136
if value == nil {
120-
b.Bool, b.Valid = false, false
137+
b.Bool, b.Valid, b.Set = false, false, false
121138
return nil
122139
}
123-
b.Valid = true
140+
b.Valid, b.Set = true, true
124141
return convert.ConvertAssign(&b.Bool, value)
125142
}
126143

bool_test.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66
)
77

88
var (
9-
boolJSON = []byte(`true`)
10-
falseJSON = []byte(`false`)
9+
boolJSON = []byte(`true`)
1110
)
1211

1312
func TestBoolFrom(t *testing.T) {
@@ -31,15 +30,13 @@ func TestBoolFromPtr(t *testing.T) {
3130
}
3231

3332
func TestUnmarshalBool(t *testing.T) {
34-
var b Bool
35-
err := json.Unmarshal(boolJSON, &b)
36-
maybePanic(err)
37-
assertBool(t, b, "bool json")
38-
3933
var null Bool
40-
err = json.Unmarshal(nullJSON, &null)
34+
err := json.Unmarshal(nullJSON, &null)
4135
maybePanic(err)
4236
assertNullBool(t, null, "null json")
37+
if !null.Set {
38+
t.Error("should be Set", err)
39+
}
4340

4441
var badType Bool
4542
err = json.Unmarshal(intJSON, &badType)

byte.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import (
1111
type Byte struct {
1212
Byte byte
1313
Valid bool
14+
Set bool
1415
}
1516

1617
// NewByte creates a new Byte
1718
func NewByte(b byte, valid bool) Byte {
1819
return Byte{
1920
Byte: b,
2021
Valid: valid,
22+
Set: true,
2123
}
2224
}
2325

@@ -34,8 +36,21 @@ func ByteFromPtr(b *byte) Byte {
3436
return NewByte(*b, true)
3537
}
3638

39+
// IsValid returns true if this carries and explicit value and
40+
// is not null.
41+
func (b Byte) IsValid() bool {
42+
return b.Set && b.Valid
43+
}
44+
45+
// IsSet returns true if this carries an explicit value (null inclusive)
46+
func (b Byte) IsSet() bool {
47+
return b.Set
48+
}
49+
3750
// UnmarshalJSON implements json.Unmarshaler.
3851
func (b *Byte) UnmarshalJSON(data []byte) error {
52+
b.Set = true
53+
3954
if len(data) == 0 || bytes.Equal(data, NullBytes) {
4055
b.Valid = false
4156
b.Byte = 0
@@ -58,7 +73,8 @@ func (b *Byte) UnmarshalJSON(data []byte) error {
5873

5974
// UnmarshalText implements encoding.TextUnmarshaler.
6075
func (b *Byte) UnmarshalText(text []byte) error {
61-
if text == nil || len(text) == 0 {
76+
b.Set = true
77+
if len(text) == 0 {
6278
b.Valid = false
6379
return nil
6480
}
@@ -92,6 +108,7 @@ func (b Byte) MarshalText() ([]byte, error) {
92108
func (b *Byte) SetValid(n byte) {
93109
b.Byte = n
94110
b.Valid = true
111+
b.Set = true
95112
}
96113

97114
// Ptr returns a pointer to this Byte's value, or a nil pointer if this Byte is null.
@@ -110,19 +127,17 @@ func (b Byte) IsZero() bool {
110127
// Scan implements the Scanner interface.
111128
func (b *Byte) Scan(value interface{}) error {
112129
if value == nil {
113-
b.Byte, b.Valid = 0, false
130+
b.Byte, b.Valid, b.Set = 0, false, false
114131
return nil
115132
}
116133

117134
val := value.(string)
118135
if len(val) == 0 {
119-
b.Valid = false
120-
b.Byte = 0
136+
b.Byte, b.Valid, b.Set = 0, false, false
121137
return nil
122138
}
123139

124-
b.Valid = true
125-
b.Byte = byte(val[0])
140+
b.Byte, b.Valid, b.Set = val[0], true, true
126141
return nil
127142
}
128143

byte_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import (
55
"testing"
66
)
77

8-
var (
9-
byteJSON = []byte(`"b"`)
10-
)
11-
128
func TestByteFrom(t *testing.T) {
139
i := ByteFrom('b')
1410
assertByte(t, i, "ByteFrom()")
@@ -34,6 +30,9 @@ func TestUnmarshalByte(t *testing.T) {
3430
err := json.Unmarshal(nullJSON, &null)
3531
maybePanic(err)
3632
assertNullByte(t, null, "null json")
33+
if !null.Set {
34+
t.Error("expected Set to be true")
35+
}
3736

3837
var badType Byte
3938
err = json.Unmarshal(boolJSON, &badType)

bytes.go

+25-8
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ var NullBytes = []byte("null")
1515
type Bytes struct {
1616
Bytes []byte
1717
Valid bool
18+
Set bool
1819
}
1920

2021
// NewBytes creates a new Bytes
2122
func NewBytes(b []byte, valid bool) Bytes {
2223
return Bytes{
2324
Bytes: b,
2425
Valid: valid,
26+
Set: true,
2527
}
2628
}
2729

@@ -39,27 +41,41 @@ func BytesFromPtr(b *[]byte) Bytes {
3941
return n
4042
}
4143

44+
// IsValid returns true if this carries and explicit value and
45+
// is not null.
46+
func (b Bytes) IsValid() bool {
47+
return b.Set && b.Valid
48+
}
49+
50+
// IsSet returns true if this carries an explicit value (null inclusive)
51+
func (b Bytes) IsSet() bool {
52+
return b.Set
53+
}
54+
4255
// UnmarshalJSON implements json.Unmarshaler.
4356
func (b *Bytes) UnmarshalJSON(data []byte) error {
57+
b.Set = true
58+
4459
if bytes.Equal(data, NullBytes) {
4560
b.Valid = false
4661
b.Bytes = nil
4762
return nil
4863
}
4964

50-
var s string
51-
if err := json.Unmarshal(data, &s); err != nil {
65+
var bv []byte
66+
if err := json.Unmarshal(data, &bv); err != nil {
5267
return err
5368
}
5469

55-
b.Bytes = []byte(s)
70+
b.Bytes = bv
5671
b.Valid = true
5772
return nil
5873
}
5974

6075
// UnmarshalText implements encoding.TextUnmarshaler.
6176
func (b *Bytes) UnmarshalText(text []byte) error {
62-
if text == nil || len(text) == 0 {
77+
b.Set = true
78+
if len(text) == 0 {
6379
b.Bytes = nil
6480
b.Valid = false
6581
} else {
@@ -72,10 +88,10 @@ func (b *Bytes) UnmarshalText(text []byte) error {
7288

7389
// MarshalJSON implements json.Marshaler.
7490
func (b Bytes) MarshalJSON() ([]byte, error) {
75-
if len(b.Bytes) == 0 || b.Bytes == nil {
91+
if len(b.Bytes) == 0 {
7692
return NullBytes, nil
7793
}
78-
return b.Bytes, nil
94+
return json.Marshal(b.Bytes)
7995
}
8096

8197
// MarshalText implements encoding.TextMarshaler.
@@ -90,6 +106,7 @@ func (b Bytes) MarshalText() ([]byte, error) {
90106
func (b *Bytes) SetValid(n []byte) {
91107
b.Bytes = n
92108
b.Valid = true
109+
b.Set = true
93110
}
94111

95112
// Ptr returns a pointer to this Bytes's value, or a nil pointer if this Bytes is null.
@@ -108,10 +125,10 @@ func (b Bytes) IsZero() bool {
108125
// Scan implements the Scanner interface.
109126
func (b *Bytes) Scan(value interface{}) error {
110127
if value == nil {
111-
b.Bytes, b.Valid = []byte{}, false
128+
b.Bytes, b.Valid, b.Set = nil, false, false
112129
return nil
113130
}
114-
b.Valid = true
131+
b.Valid, b.Set = true, true
115132
return convert.ConvertAssign(&b.Bytes, value)
116133
}
117134

0 commit comments

Comments
 (0)