Skip to content

Commit

Permalink
Merge branch 'or-zero'
Browse files Browse the repository at this point in the history
  • Loading branch information
guregu committed Dec 14, 2017
2 parents 26f8e13 + 932cc7d commit e81d6d8
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
coverage.out
coverage.out
.idea/
5 changes: 5 additions & 0 deletions bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func BoolFromPtr(b *bool) Bool {
return NewBool(*b, true)
}

// ValueOrZero returns the inner value if valid, otherwise false.
func (b Bool) ValueOrZero() bool {
return b.Valid && b.Bool
}

// UnmarshalJSON implements json.Unmarshaler.
// It supports number and null input.
// 0 will not be considered a null Bool.
Expand Down
12 changes: 12 additions & 0 deletions bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ func TestBoolScan(t *testing.T) {
assertNullBool(t, null, "scanned null")
}

func TestBoolValueOrZero(t *testing.T) {
valid := NewBool(true, true)
if valid.ValueOrZero() != true {
t.Error("unexpected ValueOrZero", valid.ValueOrZero())
}

invalid := NewBool(true, false)
if invalid.ValueOrZero() != false {
t.Error("unexpected ValueOrZero", invalid.ValueOrZero())
}
}

func assertBool(t *testing.T, b Bool, from string) {
if b.Bool != true {
t.Errorf("bad %s bool: %v ≠ %v\n", from, b.Bool, true)
Expand Down
8 changes: 8 additions & 0 deletions float.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func FloatFromPtr(f *float64) Float {
return NewFloat(*f, true)
}

// ValueOrZero returns the inner value if valid, otherwise zero.
func (f Float) ValueOrZero() float64 {
if !f.Valid {
return 0
}
return f.Float64
}

// UnmarshalJSON implements json.Unmarshaler.
// It supports number and null input.
// 0 will not be considered a null Float.
Expand Down
12 changes: 12 additions & 0 deletions float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ func TestFloatInfNaN(t *testing.T) {
}
}

func TestFloatValueOrZero(t *testing.T) {
valid := NewFloat(1.2345, true)
if valid.ValueOrZero() != 1.2345 {
t.Error("unexpected ValueOrZero", valid.ValueOrZero())
}

invalid := NewFloat(1.2345, false)
if invalid.ValueOrZero() != 0 {
t.Error("unexpected ValueOrZero", invalid.ValueOrZero())
}
}

func assertFloat(t *testing.T, f Float, from string) {
if f.Float64 != 1.2345 {
t.Errorf("bad %s float: %f ≠ %f\n", from, f.Float64, 1.2345)
Expand Down
8 changes: 8 additions & 0 deletions int.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func IntFromPtr(i *int64) Int {
return NewInt(*i, true)
}

// ValueOrZero returns the inner value if valid, otherwise zero.
func (i Int) ValueOrZero() int64 {
if !i.Valid {
return 0
}
return i.Int64
}

// UnmarshalJSON implements json.Unmarshaler.
// It supports number and null input.
// 0 will not be considered a null Int.
Expand Down
12 changes: 12 additions & 0 deletions int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@ func TestIntScan(t *testing.T) {
assertNullInt(t, null, "scanned null")
}

func TestIntValueOrZero(t *testing.T) {
valid := NewInt(12345, true)
if valid.ValueOrZero() != 12345 {
t.Error("unexpected ValueOrZero", valid.ValueOrZero())
}

invalid := NewInt(12345, false)
if invalid.ValueOrZero() != 0 {
t.Error("unexpected ValueOrZero", invalid.ValueOrZero())
}
}

func assertInt(t *testing.T, i Int, from string) {
if i.Int64 != 12345 {
t.Errorf("bad %s int: %d ≠ %d\n", from, i.Int64, 12345)
Expand Down
8 changes: 8 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ func StringFromPtr(s *string) String {
return NewString(*s, true)
}

// ValueOrZero returns the inner value if valid, otherwise zero.
func (s String) ValueOrZero() string {
if !s.Valid {
return ""
}
return s.String
}

// NewString creates a new String
func NewString(s string, valid bool) String {
return String{
Expand Down
12 changes: 12 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ func TestStringScan(t *testing.T) {
assertNullStr(t, null, "scanned null")
}

func TestStringValueOrZero(t *testing.T) {
valid := NewString("test", true)
if valid.ValueOrZero() != "test" {
t.Error("unexpected ValueOrZero", valid.ValueOrZero())
}

invalid := NewString("test", false)
if invalid.ValueOrZero() != "" {
t.Error("unexpected ValueOrZero", invalid.ValueOrZero())
}
}

func maybePanic(err error) {
if err != nil {
panic(err)
Expand Down
8 changes: 8 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ func TimeFromPtr(t *time.Time) Time {
return NewTime(*t, true)
}

// ValueOrZero returns the inner value if valid, otherwise zero.
func (t Time) ValueOrZero() time.Time {
if !t.Valid {
return time.Time{}
}
return t.Time
}

// MarshalJSON implements json.Marshaler.
// It will encode null if this time is null.
func (t Time) MarshalJSON() ([]byte, error) {
Expand Down
13 changes: 13 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ func TestTimeScanValue(t *testing.T) {
assertNullTime(t, wrong, "scanned wrong")
}

func TestTimeValueOrZero(t *testing.T) {
valid := TimeFrom(timeValue)
if valid.ValueOrZero() != valid.Time || valid.ValueOrZero().IsZero() {
t.Error("unexpected ValueOrZero", valid.ValueOrZero())
}

invalid := valid
invalid.Valid = false
if !invalid.ValueOrZero().IsZero() {
t.Error("unexpected ValueOrZero", invalid.ValueOrZero())
}
}

func assertTime(t *testing.T, ti Time, from string) {
if ti.Time != timeValue {
t.Errorf("bad %v time: %v ≠ %v\n", from, ti.Time, timeValue)
Expand Down

0 comments on commit e81d6d8

Please sign in to comment.