github.com/cosmos/cosmos-sdk@v0.50.10/x/gov/types/v1/content.go (about)

     1  package v1
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/cosmos/gogoproto/proto"
     7  
     8  	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
     9  	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
    10  	"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
    11  )
    12  
    13  // NewLegacyContent creates a new MsgExecLegacyContent from a legacy Content
    14  // interface.
    15  func NewLegacyContent(content v1beta1.Content, authority string) (*MsgExecLegacyContent, error) {
    16  	msg, ok := content.(proto.Message)
    17  	if !ok {
    18  		return nil, fmt.Errorf("%T does not implement proto.Message", content)
    19  	}
    20  
    21  	any, err := codectypes.NewAnyWithValue(msg)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	return NewMsgExecLegacyContent(any, authority), nil
    27  }
    28  
    29  // LegacyContentFromMessage extracts the legacy Content interface from a
    30  // MsgExecLegacyContent.
    31  func LegacyContentFromMessage(msg *MsgExecLegacyContent) (v1beta1.Content, error) {
    32  	content, ok := msg.Content.GetCachedValue().(v1beta1.Content)
    33  	if !ok {
    34  		return nil, sdkerrors.ErrInvalidType.Wrapf("expected %T, got %T", (*v1beta1.Content)(nil), msg.Content.GetCachedValue())
    35  	}
    36  
    37  	return content, nil
    38  }