Skip to content

Commit 8eb986b

Browse files
committed
fix: add timestamp
1 parent 2aee7dc commit 8eb986b

File tree

5 files changed

+239
-0
lines changed

5 files changed

+239
-0
lines changed

ormpb/orm.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ormpb
2+
3+
import (
4+
"database/sql"
5+
"database/sql/driver"
6+
"time"
7+
8+
"google.golang.org/protobuf/types/known/timestamppb"
9+
)
10+
11+
func New(t time.Time) *Timestamp {
12+
return &Timestamp{Timestamp: timestamppb.New(t)}
13+
}
14+
15+
func Now() *Timestamp {
16+
return &Timestamp{Timestamp: timestamppb.Now()}
17+
}
18+
19+
func (x *Timestamp) Scan(src interface{}) error {
20+
nullTime := &sql.NullTime{}
21+
if err := nullTime.Scan(src); err != nil {
22+
return err
23+
}
24+
25+
x.Timestamp = timestamppb.New(nullTime.Time)
26+
return nil
27+
}
28+
29+
// Value implements driver.Valuer interface and returns string format of Time.
30+
func (x *Timestamp) Value() (driver.Value, error) {
31+
return x.Timestamp.AsTime(), nil
32+
}
33+
34+
// MarshalJSON implements json.Marshaler to convert Time to json serialization.
35+
func (x *Timestamp) MarshalJSON() ([]byte, error) {
36+
return x.Timestamp.AsTime().MarshalJSON()
37+
}
38+
39+
// UnmarshalJSON implements json.Unmarshaler to deserialize json data.
40+
func (x *Timestamp) UnmarshalJSON(data []byte) error {
41+
// ignore null
42+
if string(data) == "null" {
43+
return nil
44+
}
45+
46+
var t = time.Time{}
47+
if err := t.UnmarshalJSON(data); err != nil {
48+
return err
49+
}
50+
51+
x.Timestamp = timestamppb.New(t)
52+
return nil
53+
}

ormpb/orm.pb.go

Lines changed: 154 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ormpb/orm.proto

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
syntax = "proto3";
2+
3+
package orm;
4+
5+
import "google/protobuf/descriptor.proto";
6+
import "google/protobuf/timestamp.proto";
7+
8+
option go_package = "github.com/pubgo/protobuild/ormpb;ormpb";
9+
10+
message Timestamp {
11+
google.protobuf.Timestamp timestamp = 1;
12+
}

ormpb/orm_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ormpb
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pubgo/xerror"
7+
)
8+
9+
func TestName(t *testing.T) {
10+
var n = Now()
11+
var d, err = n.MarshalJSON()
12+
xerror.Panic(err)
13+
t.Log(string(d))
14+
15+
var n1 = Now()
16+
xerror.Panic(n1.UnmarshalJSON(d))
17+
t.Log(n.Timestamp.AsTime())
18+
xerror.Assert(n.String() != n1.String(), "")
19+
}

protobuf.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ checksum: 0f7c5397b378b6024614c8edb8971e0c09a7b69f
44
root:
55
- retag
66
- protoc-gen-retag
7+
- ormpb
78
deps:
89
- name: google/protobuf
910
url: /usr/local/include/google/protobuf

0 commit comments

Comments
 (0)