github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/common/configtx/util.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package configtx
    18  
    19  import (
    20  	cb "github.com/hyperledger/fabric/protos/common"
    21  
    22  	"github.com/golang/protobuf/proto"
    23  )
    24  
    25  // UnmarshalConfig attempts to unmarshal bytes to a *cb.Config
    26  func UnmarshalConfig(data []byte) (*cb.Config, error) {
    27  	config := &cb.Config{}
    28  	err := proto.Unmarshal(data, config)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	return config, nil
    33  }
    34  
    35  // UnmarshalConfig attempts to unmarshal bytes to a *cb.Config
    36  func UnmarshalConfigOrPanic(data []byte) *cb.Config {
    37  	result, err := UnmarshalConfig(data)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	return result
    42  }
    43  
    44  // UnmarshalConfigUpdate attempts to unmarshal bytes to a *cb.ConfigUpdate
    45  func UnmarshalConfigUpdate(data []byte) (*cb.ConfigUpdate, error) {
    46  	configUpdate := &cb.ConfigUpdate{}
    47  	err := proto.Unmarshal(data, configUpdate)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return configUpdate, nil
    52  }
    53  
    54  // UnmarshalConfigUpdate attempts to unmarshal bytes to a *cb.ConfigUpdate or panics
    55  func UnmarshalConfigUpdateOrPanic(data []byte) *cb.ConfigUpdate {
    56  	result, err := UnmarshalConfigUpdate(data)
    57  	if err != nil {
    58  		panic(err)
    59  	}
    60  	return result
    61  }
    62  
    63  // UnmarshalConfigUpdateEnvelope attempts to unmarshal bytes to a *cb.ConfigUpdate
    64  func UnmarshalConfigUpdateEnvelope(data []byte) (*cb.ConfigUpdateEnvelope, error) {
    65  	configUpdateEnvelope := &cb.ConfigUpdateEnvelope{}
    66  	err := proto.Unmarshal(data, configUpdateEnvelope)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	return configUpdateEnvelope, nil
    71  }
    72  
    73  // UnmarshalConfigUpdateEnvelope attempts to unmarshal bytes to a *cb.ConfigUpdateEnvelope or panics
    74  func UnmarshalConfigUpdateEnvelopeOrPanic(data []byte) *cb.ConfigUpdateEnvelope {
    75  	result, err := UnmarshalConfigUpdateEnvelope(data)
    76  	if err != nil {
    77  		panic(err)
    78  	}
    79  	return result
    80  }
    81  
    82  // UnmarshalConfigEnvelope attempts to unmarshal bytes to a *cb.ConfigEnvelope
    83  func UnmarshalConfigEnvelope(data []byte) (*cb.ConfigEnvelope, error) {
    84  	configEnv := &cb.ConfigEnvelope{}
    85  	err := proto.Unmarshal(data, configEnv)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	return configEnv, nil
    90  }
    91  
    92  // UnmarshalConfigEnvelope attempts to unmarshal bytes to a *cb.ConfigEnvelope or panics
    93  func UnmarshalConfigEnvelopeOrPanic(data []byte) *cb.ConfigEnvelope {
    94  	result, err := UnmarshalConfigEnvelope(data)
    95  	if err != nil {
    96  		panic(err)
    97  	}
    98  	return result
    99  }