blob: 53167ffa51cad21a3715847f8acede1e18811842 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// Copyright 2015 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Package pbutil defines interfaces for handling Protocol Buffer objects.
16package pbutil
17
18import "github.com/coreos/pkg/capnslog"
19
20var (
21 plog = capnslog.NewPackageLogger("go.etcd.io/etcd", "pkg/pbutil")
22)
23
24type Marshaler interface {
25 Marshal() (data []byte, err error)
26}
27
28type Unmarshaler interface {
29 Unmarshal(data []byte) error
30}
31
32func MustMarshal(m Marshaler) []byte {
33 d, err := m.Marshal()
34 if err != nil {
35 plog.Panicf("marshal should never fail (%v)", err)
36 }
37 return d
38}
39
40func MustUnmarshal(um Unmarshaler, data []byte) {
41 if err := um.Unmarshal(data); err != nil {
42 plog.Panicf("unmarshal should never fail (%v)", err)
43 }
44}
45
46func MaybeUnmarshal(um Unmarshaler, data []byte) bool {
47 if err := um.Unmarshal(data); err != nil {
48 return false
49 }
50 return true
51}
52
53func GetBool(v *bool) (vv bool, set bool) {
54 if v == nil {
55 return false, false
56 }
57 return *v, true
58}
59
60func Boolp(b bool) *bool { return &b }