...

Source file src/go.etcd.io/etcd/pkg/v3/pbutil/pbutil.go

Documentation: go.etcd.io/etcd/pkg/v3/pbutil

     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 "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