forked from s7techlab/cckit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmust.go
56 lines (47 loc) · 1.1 KB
/
must.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package testing
import (
"encoding/json"
"time"
"github.com/s7techlab/cckit/convert"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
)
// MustProtoMarshal marshals proto.Message, panics if error
func MustProtoMarshal(pb proto.Message) []byte {
bb, err := proto.Marshal(pb)
if err != nil {
panic(err)
}
return bb
}
func MustJSONMarshal(val interface{}) []byte {
bb, err := json.Marshal(val)
if err != nil {
panic(err)
}
return bb
}
// MustProtoUnmarshal unmarshals proto.Message, panics if error
func MustProtoUnmarshal(bb []byte, pm proto.Message) proto.Message {
p := proto.Clone(pm)
if err := proto.Unmarshal(bb, p); err != nil {
panic(err)
}
return p
}
// MustProtoTimestamp, creates proto.Timestamp, panics if error
func MustProtoTimestamp(t time.Time) *timestamp.Timestamp {
ts, err := ptypes.TimestampProto(t)
if err != nil {
panic(err)
}
return ts
}
func MustConvertFromBytes(bb []byte, target interface{}) interface{} {
v, err := convert.FromBytes(bb, target)
if err != nil {
panic(err)
}
return v
}