github.com/cyverse/go-irodsclient@v0.13.2/irods/message/version.go (about)

     1  package message
     2  
     3  import (
     4  	"encoding/xml"
     5  
     6  	"github.com/cyverse/go-irodsclient/irods/common"
     7  	"github.com/cyverse/go-irodsclient/irods/types"
     8  	"golang.org/x/xerrors"
     9  )
    10  
    11  const (
    12  	// RODS_MESSAGE_VERSION_TYPE is a message type for version
    13  	RODS_MESSAGE_VERSION_TYPE MessageType = "RODS_VERSION"
    14  )
    15  
    16  // IRODSMessageVersion stores version message
    17  type IRODSMessageVersion struct {
    18  	XMLName        xml.Name `xml:"Version_PI"`
    19  	Status         int      `xml:"status"`
    20  	ReleaseVersion string   `xml:"relVersion"`
    21  	APIVersion     string   `xml:"apiVersion"`
    22  	ReconnectPort  int      `xml:"reconnPort"`
    23  	ReconnectAddr  string   `xml:"reconnAddr"`
    24  	Cookie         int      `xml:"cookie"`
    25  }
    26  
    27  // GetBytes returns byte array
    28  func (msg *IRODSMessageVersion) GetBytes() ([]byte, error) {
    29  	xmlBytes, err := xml.Marshal(msg)
    30  	if err != nil {
    31  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    32  	}
    33  	return xmlBytes, nil
    34  }
    35  
    36  // FromBytes returns struct from bytes
    37  func (msg *IRODSMessageVersion) FromBytes(bytes []byte) error {
    38  	err := xml.Unmarshal(bytes, msg)
    39  	if err != nil {
    40  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    41  	}
    42  	return nil
    43  }
    44  
    45  // CheckError returns error if server returned an error
    46  func (msg *IRODSMessageVersion) CheckError() error {
    47  	if msg.Status < 0 {
    48  		return types.NewIRODSError(common.ErrorCode(msg.Status))
    49  	}
    50  	return nil
    51  }
    52  
    53  // GetVersion creates IRODSVersion
    54  func (msg *IRODSMessageVersion) GetVersion() *types.IRODSVersion {
    55  	return &types.IRODSVersion{
    56  		ReleaseVersion: msg.ReleaseVersion,
    57  		APIVersion:     msg.APIVersion,
    58  		ReconnectPort:  msg.ReconnectPort,
    59  		ReconnectAddr:  msg.ReconnectAddr,
    60  		Cookie:         msg.Cookie,
    61  	}
    62  }
    63  
    64  // GetMessage builds a message
    65  func (msg *IRODSMessageVersion) GetMessage() (*IRODSMessage, error) {
    66  	bytes, err := msg.GetBytes()
    67  	if err != nil {
    68  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    69  	}
    70  
    71  	msgBody := IRODSMessageBody{
    72  		Type:    RODS_MESSAGE_VERSION_TYPE,
    73  		Message: bytes,
    74  		Error:   nil,
    75  		Bs:      nil,
    76  		IntInfo: 0,
    77  	}
    78  
    79  	msgHeader, err := msgBody.BuildHeader()
    80  	if err != nil {
    81  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
    82  	}
    83  
    84  	return &IRODSMessage{
    85  		Header: msgHeader,
    86  		Body:   &msgBody,
    87  	}, nil
    88  }
    89  
    90  // FromMessage returns struct from IRODSMessage
    91  func (msg *IRODSMessageVersion) FromMessage(msgIn *IRODSMessage) error {
    92  	if msgIn.Body == nil {
    93  		return xerrors.Errorf("empty message body")
    94  	}
    95  
    96  	err := msg.FromBytes(msgIn.Body.Message)
    97  	if err != nil {
    98  		return xerrors.Errorf("failed to get irods message from message body")
    99  	}
   100  	return nil
   101  }