File tree Expand file tree Collapse file tree 5 files changed +239
-0
lines changed Expand file tree Collapse file tree 5 files changed +239
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ checksum: 0f7c5397b378b6024614c8edb8971e0c09a7b69f
4
4
root :
5
5
- retag
6
6
- protoc-gen-retag
7
+ - ormpb
7
8
deps :
8
9
- name : google/protobuf
9
10
url : /usr/local/include/google/protobuf
You can’t perform that action at this time.
0 commit comments