blob: 821f59703ae6b047db821d2cb03ee9b08841ec21 [file] [log] [blame]
Abhay Kumar40252eb2025-10-13 13:25:53 +00001// 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 "fmt"
19
20type Marshaler interface {
21 Marshal() (data []byte, err error)
22}
23
24type Unmarshaler interface {
25 Unmarshal(data []byte) error
26}
27
28func 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
36func 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
42func MaybeUnmarshal(um Unmarshaler, data []byte) bool {
43 if err := um.Unmarshal(data); err != nil {
44 return false
45 }
46 return true
47}
48
49func GetBool(v *bool) (vv bool, set bool) {
50 if v == nil {
51 return false, false
52 }
53 return *v, true
54}
55
56func Boolp(b bool) *bool { return &b }