Files
threshold/internal/db/timestamp/scanners.go
T
Michael Gaffney 5bdd8a46ea Extract timestamp scanners into reusable package (#91)
* Refactor: move Timestamp from static/store to db/timestamp

* Refactor: convert remaining Timestamps to timestamp.Timestamp

* Use new db timestamp
2020-06-01 11:46:50 -04:00

34 lines
707 B
Go

package timestamp
import (
"database/sql/driver"
"errors"
"fmt"
"time"
"github.com/golang/protobuf/ptypes"
)
// Scan implements sql.Scanner for protobuf Timestamp.
func (ts *Timestamp) Scan(value interface{}) error {
switch t := value.(type) {
case time.Time:
var err error
ts.Timestamp, err = ptypes.TimestampProto(t) // google proto version
if err != nil {
return fmt.Errorf("error converting the timestamp: %w", err)
}
default:
return errors.New("Not a protobuf Timestamp")
}
return nil
}
// Scan implements driver.Valuer for protobuf Timestamp.
func (ts *Timestamp) Value() (driver.Value, error) {
if ts == nil {
return nil, nil
}
return ptypes.Timestamp(ts.Timestamp)
}