...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package pbutil
17
18 import "fmt"
19
20 type Marshaler interface {
21 Marshal() (data []byte, err error)
22 }
23
24 type Unmarshaler interface {
25 Unmarshal(data []byte) error
26 }
27
28 func MustMarshal(m Marshaler) []byte {
29 d, err := m.Marshal()
30 if err != nil {
31 panic(fmt.Sprintf("marshal should never fail (%v)", err))
32 }
33 return d
34 }
35
36 func MustUnmarshal(um Unmarshaler, data []byte) {
37 if err := um.Unmarshal(data); err != nil {
38 panic(fmt.Sprintf("unmarshal should never fail (%v)", err))
39 }
40 }
41
42 func MaybeUnmarshal(um Unmarshaler, data []byte) bool {
43 if err := um.Unmarshal(data); err != nil {
44 return false
45 }
46 return true
47 }
48
49 func GetBool(v *bool) (vv bool, set bool) {
50 if v == nil {
51 return false, false
52 }
53 return *v, true
54 }
55
56 func Boolp(b bool) *bool { return &b }
57
View as plain text