Skip to content
Open
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
38 changes: 15 additions & 23 deletions field/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ type nullTime null.Time
// NullTime time that can be nil
type NullTime struct {
nullTime
validNull bool
shadow null.Time
shadowValidNull bool
invalidNull bool
shadow null.Time
shadowInvalidNull bool
ShadowInit
}

Expand All @@ -116,41 +116,38 @@ func (nt *NullTime) Scan(value interface{}) error {
case time.Time:
if v.IsZero() {
nt.Valid = false
nt.validNull = true
nt.invalidNull = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same true false flip for this. Is it because it is invalid?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, this checks to see if it is a legitimate Zero time, 12AM which should not be considered...
Ok, now I see I need to change this. Probably remove the Zero Check altogether.


} else {
nt.Time, nt.Valid = v, true
nt.validNull = false
nt.invalidNull = true
}
break
case []byte:
nt.Time, err = parseDateTime(string(v), time.UTC)
if nt.Time.IsZero() == true {
nt.Valid = false
nt.validNull = false
nt.invalidNull = true
return ErrorCouldNotScan("NullTime", value)
}
nt.Valid = (err == nil)
if err == nil {
nt.validNull = false
nt.invalidNull = true
}
break
case string:
nt.Time, err = parseDateTime(v, time.UTC)
if nt.Time.IsZero() == true {
nt.Valid = false
nt.validNull = false
nt.invalidNull = true
return ErrorCouldNotScan("NullTime", value)
}
nt.Valid = (err == nil)
if err == nil {
nt.validNull = false
nt.invalidNull = true
}
break
default:
if value == nil {
nt.Valid = false
nt.validNull = true
nt.invalidNull = false
} else {
err = ErrorCouldNotScan("NullTime", value)
}
Expand All @@ -159,29 +156,24 @@ func (nt *NullTime) Scan(value interface{}) error {
// load shadow on first scan only
nt.DoInit(func() {
_ = nt.shadow.Scan(nt.Time)
if value == nil {
nt.shadowValidNull = true
}
nt.shadowInvalidNull = (value != nil)
})
return err
}

// Value return the value of this field
func (nt NullTime) Value() (driver.Value, error) {
if nt.validNull {
return nil, nil
}
if nt.Time.IsZero() {
if !nt.invalidNull {
return nil, nil
}
return nt.Time, nil
}

// IsDirty if the shadow value does not match the field value
func (nt *NullTime) IsDirty() bool {
if nt.validNull && nt.shadowValidNull {
if !nt.invalidNull && !nt.shadowInvalidNull {
return false
} else if nt.validNull == false && nt.shadowValidNull == false {
} else if nt.invalidNull && nt.shadowInvalidNull {
return !nt.Time.Equal(nt.shadow.Time)
}
return true
Expand All @@ -195,7 +187,7 @@ func (nt NullTime) IsSet() bool {
// ShadowValue return the initial value of this field
func (nt NullTime) ShadowValue() (driver.Value, error) {
if nt.InitDone() {
if nt.shadowValidNull {
if !nt.shadowInvalidNull {
return nil, nil
}
return nt.shadow.Value()
Expand Down
34 changes: 15 additions & 19 deletions field/time_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ func (t *TimeDate) UnmarshalJSON(data []byte) error {
// NullTime time that can be nil
type NullTimeDate struct {
nullTime
validNull bool
shadow null.Time
shadowValidNull bool
invalidNull bool
shadow null.Time
shadowInvalidNull bool
ShadowInit
}

Expand All @@ -109,41 +109,39 @@ func (nt *NullTimeDate) Scan(value interface{}) error {
case time.Time:
if v.IsZero() {
nt.Valid = false
nt.validNull = true
nt.invalidNull = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this bool not being flipped because of invalidity?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also in this location! 👍


} else {
nt.Time, nt.Valid = v, true
nt.validNull = false
nt.invalidNull = true
}
break
case []byte:
nt.Time, err = parseTimeDate(string(v))
if nt.Time.IsZero() == true {
nt.Valid = false
nt.validNull = false
nt.invalidNull = true
return ErrorCouldNotScan("NullTimeDate", value)
}
nt.Valid = (err == nil)
if err == nil {
nt.validNull = false
nt.invalidNull = true
}
break
case string:
nt.Time, err = parseTimeDate(v)
if nt.Time.IsZero() == true {
nt.Valid = false
nt.validNull = false
nt.invalidNull = true
return ErrorCouldNotScan("NullTimeDate", value)
}
nt.Valid = (err == nil)
if err == nil {
nt.validNull = false
nt.invalidNull = true
}
break
default:
if value == nil {
nt.Valid = false
nt.validNull = true
nt.invalidNull = false
} else {
err = ErrorCouldNotScan("NullTimeDate", value)
}
Expand All @@ -152,26 +150,24 @@ func (nt *NullTimeDate) Scan(value interface{}) error {
// load shadow on first scan only
nt.DoInit(func() {
_ = nt.shadow.Scan(nt.Time)
if value == nil {
nt.shadowValidNull = true
}
nt.shadowInvalidNull = (value != nil)
})
return err
}

// Value return the value of this field
func (nt NullTimeDate) Value() (driver.Value, error) {
if nt.validNull {
if !nt.invalidNull {
return nil, nil
}
return nt.Time, nil
}

// IsDirty if the shadow value does not match the field value
func (nt *NullTimeDate) IsDirty() bool {
if nt.validNull && nt.shadowValidNull {
if !nt.invalidNull && !nt.shadowInvalidNull {
return false
} else if nt.validNull == false && nt.shadowValidNull == false {
} else if nt.invalidNull && nt.shadowInvalidNull {
return !nt.Time.Equal(nt.shadow.Time)
}
return true
Expand All @@ -185,7 +181,7 @@ func (nt NullTimeDate) IsSet() bool {
// ShadowValue return the initial value of this field
func (nt NullTimeDate) ShadowValue() (driver.Value, error) {
if nt.InitDone() {
if nt.shadowValidNull {
if !nt.shadowInvalidNull {
return nil, nil
}
return nt.shadow.Value()
Expand Down
21 changes: 21 additions & 0 deletions field/time_date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,27 @@ func TestTimeDate(t *testing.T) {
}

func TestNullTimeDate(t *testing.T) {
Convey("Unscanned", t, func() {
Convey("Value should be Null", func() {
ns := NullTimeDate{}
t, err := ns.Value()
So(err, ShouldBeNil)
So(t, ShouldBeNil)
})

Convey("IsDirty should be false", func() {
ns := NullTimeDate{}
So(ns.IsDirty(), ShouldBeFalse)
})

Convey("Marshal should provide json null", func() {
ns := NullTimeDate{}
v, err := ns.MarshalJSON()
So(err, ShouldBeNil)
So(string(v), ShouldEqual, "null")
})
})

Convey("Scan", t, func() {
Convey("Scan should load Time and Shadow field", func() {
ns := NullTimeDate{}
Expand Down
21 changes: 21 additions & 0 deletions field/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,27 @@ func TestTime(t *testing.T) {
}

func TestNullTime(t *testing.T) {
Convey("Unscanned", t, func() {
Convey("Value should be Null", func() {
ns := NullTime{}
t, err := ns.Value()
So(err, ShouldBeNil)
So(t, ShouldBeNil)
})

Convey("IsDirty should be false", func() {
ns := NullTime{}
So(ns.IsDirty(), ShouldBeFalse)
})

Convey("Marshal should provide json null", func() {
ns := NullTime{}
v, err := ns.MarshalJSON()
So(err, ShouldBeNil)
So(string(v), ShouldEqual, "null")
})
})

Convey("Scan", t, func() {
Convey("Scan should load Time and Shadow field", func() {
ns := NullTime{}
Expand Down
7 changes: 7 additions & 0 deletions field/time_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ func TestNullTimeTime(t *testing.T) {
ns := NullTimeTime{}
So(ns.IsDirty(), ShouldBeFalse)
})

Convey("Marshal should provide json null", func() {
ns := NullTimeTime{}
v, err := ns.MarshalJSON()
So(err, ShouldBeNil)
So(string(v), ShouldEqual, "null")
})
})

Convey("Scan", t, func() {
Expand Down