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

     1  package message
     2  
     3  import (
     4  	"encoding/xml"
     5  
     6  	"github.com/cyverse/go-irodsclient/irods/common"
     7  	"golang.org/x/xerrors"
     8  )
     9  
    10  // IRODSMessageWriteDataObjectRequest stores data object read request
    11  // type IRODSMessageWriteDataObjectRequest IRODSMessageOpenedDataObjectRequest
    12  type IRODSMessageWriteDataObjectRequest struct {
    13  	IRODSMessageOpenedDataObjectRequest
    14  	Data []byte `xml:"-"`
    15  }
    16  
    17  // NewIRODSMessageWriteDataObjectRequest creates a IRODSMessageWriteDataObjectRequest message
    18  func NewIRODSMessageWriteDataObjectRequest(desc int, data []byte) *IRODSMessageWriteDataObjectRequest {
    19  	request := &IRODSMessageWriteDataObjectRequest{
    20  		IRODSMessageOpenedDataObjectRequest: IRODSMessageOpenedDataObjectRequest{
    21  			FileDescriptor: desc,
    22  			Size:           int64(len(data)),
    23  			Whence:         0,
    24  			OperationType:  0,
    25  			Offset:         0,
    26  			BytesWritten:   0,
    27  			KeyVals: IRODSMessageSSKeyVal{
    28  				Length: 0,
    29  			},
    30  		},
    31  		Data: data,
    32  	}
    33  
    34  	return request
    35  }
    36  
    37  // AddKeyVal adds a key-value pair
    38  func (msg *IRODSMessageWriteDataObjectRequest) AddKeyVal(key common.KeyWord, val string) {
    39  	msg.KeyVals.Add(string(key), val)
    40  }
    41  
    42  // GetBytes returns byte array
    43  func (msg *IRODSMessageWriteDataObjectRequest) GetBytes() ([]byte, error) {
    44  	xmlBytes, err := xml.Marshal(msg)
    45  	if err != nil {
    46  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    47  	}
    48  	return xmlBytes, nil
    49  }
    50  
    51  // FromBytes returns struct from bytes
    52  func (msg *IRODSMessageWriteDataObjectRequest) FromBytes(bytes []byte) error {
    53  	err := xml.Unmarshal(bytes, msg)
    54  	if err != nil {
    55  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    56  	}
    57  	return nil
    58  }
    59  
    60  // GetMessage builds a message
    61  func (msg *IRODSMessageWriteDataObjectRequest) GetMessage() (*IRODSMessage, error) {
    62  	bytes, err := msg.GetBytes()
    63  	if err != nil {
    64  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    65  	}
    66  
    67  	msgBody := IRODSMessageBody{
    68  		Type:    RODS_MESSAGE_API_REQ_TYPE,
    69  		Message: bytes,
    70  		Error:   nil,
    71  		Bs:      msg.Data,
    72  		IntInfo: int32(common.DATA_OBJ_WRITE_AN),
    73  	}
    74  
    75  	msgHeader, err := msgBody.BuildHeader()
    76  	if err != nil {
    77  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
    78  	}
    79  
    80  	return &IRODSMessage{
    81  		Header: msgHeader,
    82  		Body:   &msgBody,
    83  	}, nil
    84  }