Skip to content

Commit

Permalink
fix timestamp types
Browse files Browse the repository at this point in the history
  • Loading branch information
usk81 committed Jan 2, 2018
1 parent d329402 commit 1d76075
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 42 deletions.
4 changes: 2 additions & 2 deletions json_marshal_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func BenchmarkMarshalJSONTimestampMS(b *testing.B) {
func BenchmarkMarshalJSONTimestampNano(b *testing.B) {
x := TimestampNano{
ValidFlag: true,
Time: time.Now(),
time: time.Now(),
}
for i := 0; i < b.N; i++ {
x.MarshalJSON()
Expand All @@ -88,7 +88,7 @@ func BenchmarkMarshalJSONTimestampNano(b *testing.B) {
func BenchmarkMarshalJSONTimestamp(b *testing.B) {
x := Timestamp{
ValidFlag: true,
Time: time.Now(),
time: time.Now(),
}
for i := 0; i < b.N; i++ {
x.MarshalJSON()
Expand Down
40 changes: 34 additions & 6 deletions type_timestamp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generic

import (
"database/sql/driver"
"encoding/json"
"strconv"
"time"
Expand All @@ -9,42 +10,69 @@ import (
// Timestamp is a wrapped time type structure
type Timestamp struct {
ValidFlag
Time time.Time
time time.Time
}

// Value returns Time.Time, but if Time.ValidFlag is false, returns nil.
func (v Timestamp) Value() interface{} {
func (v Timestamp) Value() (driver.Value, error) {
if !v.Valid() {
return nil
return nil, nil
}
return v.Time
return v.time, nil
}

// Scan implements the sql.Scanner interface.
func (v *Timestamp) Scan(x interface{}) (err error) {
v.Time, v.ValidFlag, err = asTimestamp(x)
v.time, v.ValidFlag, err = asTimestamp(x)
if err != nil {
v.ValidFlag = false
return err
}
return
}

// Weak returns timestamp, but if Timestamp.ValidFlag is false, returns nil.
func (v Timestamp) Weak() interface{} {
i, _ := v.Value()
return i
}

// Set sets a specified value.
func (v *Timestamp) Set(x interface{}) (err error) {
return v.Scan(x)
}

// String implements the Stringer interface.
func (v Timestamp) String() string {
return strconv.FormatInt(v.Int64(), 10)
}

// Int return int value
func (v Timestamp) Int() int {
return int(v.Int64())
}

// Int64 return int64 value
func (v Timestamp) Int64() int64 {
if !v.Valid() || v.time.Unix() == 0 {
return 0
}
return v.time.Unix()
}

// MarshalJSON implements the json.Marshaler interface.
func (v Timestamp) MarshalJSON() ([]byte, error) {
if !v.Valid() {
return nullBytes, nil
}
return []byte(strconv.FormatInt(v.Time.Unix(), 10)), nil
return []byte(strconv.FormatInt(v.time.Unix(), 10)), nil
}

// UnmarshalJSON implements the json.Unmarshaler interface.
func (v *Timestamp) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return nil
}
var in interface{}
if err := json.Unmarshal(data, &in); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion type_timestamp_milliseconds.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type TimestampMS struct {
time time.Time
}

// Value returns Time.Time, but if TimestampMS.ValidFlag is false, returns nil.
// Value returns timestamp with milliseconds, but if TimestampMS.ValidFlag is false, returns nil.
func (v TimestampMS) Value() (driver.Value, error) {
if !v.Valid() {
return nil, nil
Expand Down
4 changes: 2 additions & 2 deletions type_timestamp_milliseconds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"
)

func TestTimestampJsonMarshal(t *testing.T) {
func TestTimestampMSJsonMarshal(t *testing.T) {
v := time.Now()
tm := TimestampMS{
ValidFlag: true,
Expand All @@ -23,7 +23,7 @@ func TestTimestampJsonMarshal(t *testing.T) {
}
}

func TestTimestampJsonMarshalValidFalse(t *testing.T) {
func TestTimestampMSJsonMarshalValidFalse(t *testing.T) {
tm := TimestampMS{
ValidFlag: false,
time: time.Now(),
Expand Down
42 changes: 35 additions & 7 deletions type_timestamp_nanoseconds.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generic

import (
"database/sql/driver"
"encoding/json"
"strconv"
"time"
Expand All @@ -9,42 +10,69 @@ import (
// TimestampNano is a wrapped time type structure
type TimestampNano struct {
ValidFlag
Time time.Time
time time.Time
}

// Value returns Time.Time, but if Time.ValidFlag is false, returns nil.
func (v TimestampNano) Value() interface{} {
// Value returns timestamp with nanoseconds, but if TimestampNano.ValidFlag is false, returns nil.
func (v TimestampNano) Value() (driver.Value, error) {
if !v.Valid() {
return nil
return nil, nil
}
return v.Time
return v.time.UnixNano(), nil
}

// Scan implements the sql.Scanner interface.
func (v *TimestampNano) Scan(x interface{}) (err error) {
v.Time, v.ValidFlag, err = asTimestampNanoseconds(x)
v.time, v.ValidFlag, err = asTimestampNanoseconds(x)
if err != nil {
v.ValidFlag = false
return err
}
return
}

// Weak returns timestamp with nano seconds, but if TimestampNano.ValidFlag is false, returns nil.
func (v TimestampNano) Weak() interface{} {
i, _ := v.Value()
return i
}

// Set sets a specified value.
func (v *TimestampNano) Set(x interface{}) (err error) {
return v.Scan(x)
}

// String implements the Stringer interface.
func (v TimestampNano) String() string {
return strconv.FormatInt(v.Int64(), 10)
}

// Int return int value
func (v TimestampNano) Int() int {
return int(v.Int64())
}

// Int64 return int64 value
func (v TimestampNano) Int64() int64 {
if !v.Valid() || v.time.UnixNano() == 0 {
return 0
}
return v.time.UnixNano()
}

// MarshalJSON implements the json.Marshaler interface.
func (v TimestampNano) MarshalJSON() ([]byte, error) {
if !v.Valid() {
return nullBytes, nil
}
return []byte(strconv.FormatInt(v.Time.UnixNano(), 10)), nil
return []byte(strconv.FormatInt(v.time.UnixNano(), 10)), nil
}

// UnmarshalJSON implements the json.Unmarshaler interface.
func (v *TimestampNano) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return nil
}
var in interface{}
if err := json.Unmarshal(data, &in); err != nil {
return err
Expand Down
Loading

0 comments on commit 1d76075

Please sign in to comment.