Skip to content

Commit

Permalink
Add sql.Scanner and driver.Valuer support to integer jsontime
Browse files Browse the repository at this point in the history
  • Loading branch information
hifi committed Jan 11, 2024
1 parent 48dfc4d commit 9532f99
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions jsontime/integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
package jsontime

import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"time"
)

Expand All @@ -25,6 +29,9 @@ func parseTime(data []byte, unixConv func(int64) time.Time, into *time.Time) err
return nil
}

var _ sql.Scanner = &UnixMilli{}
var _ driver.Valuer = UnixMilli{}

type UnixMilli struct {
time.Time
}
Expand All @@ -40,6 +47,32 @@ func (um *UnixMilli) UnmarshalJSON(data []byte) error {
return parseTime(data, time.UnixMilli, &um.Time)
}

func (um UnixMilli) Value() (driver.Value, error) {
return time.Time(um.Time).UnixMilli(), nil
}

func (um *UnixMilli) Scan(src any) error {
switch v := src.(type) {
case int:
um.Time = time.UnixMilli(int64(v))
case int8:
um.Time = time.UnixMilli(int64(v))
case int16:
um.Time = time.UnixMilli(int64(v))
case int32:
um.Time = time.UnixMilli(int64(v))
case int64:
um.Time = time.UnixMilli(v)
default:
return fmt.Errorf("%w: %T is not an integer", errors.ErrUnsupported, src)

Check failure on line 67 in jsontime/integer.go

View workflow job for this annotation

GitHub Actions / build (1.20)

undefined: errors.ErrUnsupported

Check failure on line 67 in jsontime/integer.go

View workflow job for this annotation

GitHub Actions / build (1.20)

undefined: errors.ErrUnsupported
}

return nil
}

var _ sql.Scanner = &UnixMicro{}
var _ driver.Valuer = UnixMicro{}

type UnixMicro struct {
time.Time
}
Expand All @@ -55,6 +88,32 @@ func (um *UnixMicro) UnmarshalJSON(data []byte) error {
return parseTime(data, time.UnixMicro, &um.Time)
}

func (um UnixMicro) Value() (driver.Value, error) {
return time.Time(um.Time).UnixMicro(), nil
}

func (um *UnixMicro) Scan(src any) error {
switch v := src.(type) {
case int:
um.Time = time.UnixMicro(int64(v))
case int8:
um.Time = time.UnixMicro(int64(v))
case int16:
um.Time = time.UnixMicro(int64(v))
case int32:
um.Time = time.UnixMicro(int64(v))
case int64:
um.Time = time.UnixMicro(v)
default:
return fmt.Errorf("%w: %T is not an integer", errors.ErrUnsupported, src)

Check failure on line 108 in jsontime/integer.go

View workflow job for this annotation

GitHub Actions / build (1.20)

undefined: errors.ErrUnsupported

Check failure on line 108 in jsontime/integer.go

View workflow job for this annotation

GitHub Actions / build (1.20)

undefined: errors.ErrUnsupported
}

return nil
}

var _ sql.Scanner = &UnixNano{}
var _ driver.Valuer = UnixNano{}

type UnixNano struct {
time.Time
}
Expand All @@ -72,6 +131,29 @@ func (un *UnixNano) UnmarshalJSON(data []byte) error {
}, &un.Time)
}

func (un UnixNano) Value() (driver.Value, error) {
return time.Time(un.Time).UnixNano(), nil
}

func (un *UnixNano) Scan(src any) error {
switch v := src.(type) {
case int:
un.Time = time.Unix(0, int64(v))
case int8:
un.Time = time.Unix(0, int64(v))
case int16:
un.Time = time.Unix(0, int64(v))
case int32:
un.Time = time.Unix(0, int64(v))
case int64:
un.Time = time.Unix(0, v)
default:
return fmt.Errorf("%w: %T is not an integer", errors.ErrUnsupported, src)

Check failure on line 151 in jsontime/integer.go

View workflow job for this annotation

GitHub Actions / build (1.20)

undefined: errors.ErrUnsupported

Check failure on line 151 in jsontime/integer.go

View workflow job for this annotation

GitHub Actions / build (1.20)

undefined: errors.ErrUnsupported
}

return nil
}

type Unix struct {
time.Time
}
Expand All @@ -83,8 +165,34 @@ func (u Unix) MarshalJSON() ([]byte, error) {
return json.Marshal(u.Unix())
}

var _ sql.Scanner = &Unix{}
var _ driver.Valuer = Unix{}

func (u *Unix) UnmarshalJSON(data []byte) error {
return parseTime(data, func(i int64) time.Time {
return time.Unix(i, 0)
}, &u.Time)
}

func (u Unix) Value() (driver.Value, error) {
return time.Time(u.Time).Unix(), nil
}

func (u *Unix) Scan(src any) error {
switch v := src.(type) {
case int:
u.Time = time.Unix(int64(v), 0)
case int8:
u.Time = time.Unix(int64(v), 0)
case int16:
u.Time = time.Unix(int64(v), 0)
case int32:
u.Time = time.Unix(int64(v), 0)
case int64:
u.Time = time.Unix(v, 0)
default:
return fmt.Errorf("%w: %T is not an integer", errors.ErrUnsupported, src)

Check failure on line 194 in jsontime/integer.go

View workflow job for this annotation

GitHub Actions / build (1.20)

undefined: errors.ErrUnsupported

Check failure on line 194 in jsontime/integer.go

View workflow job for this annotation

GitHub Actions / build (1.20)

undefined: errors.ErrUnsupported
}

return nil
}

0 comments on commit 9532f99

Please sign in to comment.