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