github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/dispatcher/protobuf.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package dispatcher
     8  
     9  import (
    10  	"github.com/golang/protobuf/proto"
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  // Protobuf defines the subset of protobuf lifecycle needs and allows
    15  // for injection of mocked marshaling errors.
    16  type Protobuf interface {
    17  	Marshal(msg proto.Message) (marshaled []byte, err error)
    18  	Unmarshal(marshaled []byte, msg proto.Message) error
    19  }
    20  
    21  // ProtobufImpl is the standard implementation to use for Protobuf
    22  type ProtobufImpl struct{}
    23  
    24  // Marshal passes through to proto.Marshal
    25  func (p ProtobufImpl) Marshal(msg proto.Message) ([]byte, error) {
    26  	res, err := proto.Marshal(msg)
    27  	return res, errors.WithStack(err)
    28  }
    29  
    30  // Unmarshal passes through to proto.Unmarshal
    31  func (p ProtobufImpl) Unmarshal(marshaled []byte, msg proto.Message) error {
    32  	return errors.WithStack(proto.Unmarshal(marshaled, msg))
    33  }