https://github.com/tendermint/tendermint
Raw File
Tip revision: bd6398203975a7b869107b53437db5eaa62f620f authored by William Banfield on 26 January 2022, 16:56:21 UTC
Merge remote-tracking branch 'origin/master' into wb/use-buffered-channel-test-state-full-round
Tip revision: bd63982
encoding_helper.go
package types

import (
	gogotypes "github.com/gogo/protobuf/types"

	"github.com/tendermint/tendermint/libs/bytes"
)

// cdcEncode returns nil if the input is nil, otherwise returns
// proto.Marshal(<type>Value{Value: item})
func cdcEncode(item interface{}) []byte {
	if item != nil && !isTypedNil(item) && !isEmpty(item) {
		switch item := item.(type) {
		case string:
			i := gogotypes.StringValue{
				Value: item,
			}
			bz, err := i.Marshal()
			if err != nil {
				return nil
			}
			return bz
		case int64:
			i := gogotypes.Int64Value{
				Value: item,
			}
			bz, err := i.Marshal()
			if err != nil {
				return nil
			}
			return bz
		case bytes.HexBytes:
			i := gogotypes.BytesValue{
				Value: item,
			}
			bz, err := i.Marshal()
			if err != nil {
				return nil
			}
			return bz
		default:
			return nil
		}
	}

	return nil
}
back to top