github.com/cyverse/go-irodsclient@v0.13.2/irods/message/checksum_response.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  // IRODSMessageChecksumResponse stores data object checksum response
    12  type IRODSMessageChecksumResponse struct {
    13  	Checksum string `xml:"myStr"`
    14  	// stores error return
    15  	Result int `xml:"-"`
    16  }
    17  
    18  type STRI_PI struct {
    19  }
    20  
    21  // GetBytes returns byte array
    22  func (msg *IRODSMessageChecksumResponse) GetBytes() ([]byte, error) {
    23  	xmlBytes, err := xml.Marshal(msg)
    24  	if err != nil {
    25  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    26  	}
    27  	return xmlBytes, nil
    28  }
    29  
    30  // CheckError returns error if server returned an error
    31  func (msg *IRODSMessageChecksumResponse) CheckError() error {
    32  	if len(msg.Checksum) == 0 {
    33  		return xerrors.Errorf("checksum not present in response message")
    34  	}
    35  
    36  	if msg.Result < 0 {
    37  		return types.NewIRODSError(common.ErrorCode(msg.Result))
    38  	}
    39  
    40  	return nil
    41  }
    42  
    43  // FromBytes returns struct from bytes
    44  func (msg *IRODSMessageChecksumResponse) FromBytes(bytes []byte) error {
    45  	err := xml.Unmarshal(bytes, msg)
    46  	if err != nil {
    47  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    48  	}
    49  	return nil
    50  }
    51  
    52  // FromMessage returns struct from IRODSMessage
    53  func (msg *IRODSMessageChecksumResponse) FromMessage(msgIn *IRODSMessage) error {
    54  	if msgIn.Body == nil {
    55  		return xerrors.Errorf("empty message body")
    56  	}
    57  
    58  	err := msg.FromBytes(msgIn.Body.Message)
    59  	msg.Result = int(msgIn.Body.IntInfo)
    60  	if err != nil {
    61  		return xerrors.Errorf("failed to get irods message from message body")
    62  	}
    63  	return err
    64  }