| 1 |
// 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. |
| 16 |
package pbutil |
| 17 |
|
| 18 |
import "github.com/coreos/pkg/capnslog" |
| 19 |
|
| 20 |
var ( |
| 21 |
plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "pkg/pbutil") |
| 22 |
) |
| 23 |
|
| 24 |
type Marshaler interface { |
| 25 |
Marshal() (data []byte, err error) |
| 26 |
} |
| 27 |
|
| 28 |
type Unmarshaler interface { |
| 29 |
Unmarshal(data []byte) error |
| 30 |
} |
| 31 |
|
| 32 |
func 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 |
|
| 40 |
func 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 |
|
| 46 |
func MaybeUnmarshal(um Unmarshaler, data []byte) bool { |
| 47 |
if err := um.Unmarshal(data); err != nil { |
| 48 |
return false |
| 49 |
} |
| 50 |
return true |
| 51 |
} |
| 52 |
|
| 53 |
func GetBool(v *bool) (vv bool, set bool) { |
| 54 |
if v == nil { |
| 55 |
return false, false |
| 56 |
} |
| 57 |
return *v, true |
| 58 |
} |
| 59 |
|
| 60 |
func Boolp(b bool) *bool { return &b } |