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