github.com/project-88388/tendermint-v0.34.14-terra.2@v1.0.0/types/encoding_helper.go (about)

     1  package types
     2  
     3  import (
     4  	gogotypes "github.com/gogo/protobuf/types"
     5  
     6  	"github.com/tendermint/tendermint/libs/bytes"
     7  )
     8  
     9  // cdcEncode returns nil if the input is nil, otherwise returns
    10  // proto.Marshal(<type>Value{Value: item})
    11  func cdcEncode(item interface{}) []byte {
    12  	if item != nil && !isTypedNil(item) && !isEmpty(item) {
    13  		switch item := item.(type) {
    14  		case string:
    15  			i := gogotypes.StringValue{
    16  				Value: item,
    17  			}
    18  			bz, err := i.Marshal()
    19  			if err != nil {
    20  				return nil
    21  			}
    22  			return bz
    23  		case int64:
    24  			i := gogotypes.Int64Value{
    25  				Value: item,
    26  			}
    27  			bz, err := i.Marshal()
    28  			if err != nil {
    29  				return nil
    30  			}
    31  			return bz
    32  		case bytes.HexBytes:
    33  			i := gogotypes.BytesValue{
    34  				Value: item,
    35  			}
    36  			bz, err := i.Marshal()
    37  			if err != nil {
    38  				return nil
    39  			}
    40  			return bz
    41  		default:
    42  			return nil
    43  		}
    44  	}
    45  
    46  	return nil
    47  }