github.com/cyverse/go-irodsclient@v0.13.2/irods/message/close_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 // IRODSMessageCloseDataObjectRequest stores data object close request 11 type IRODSMessageCloseDataObjectRequest IRODSMessageOpenedDataObjectRequest 12 13 // NewIRODSMessageCloseDataObjectRequest creates a IRODSMessageCloseDataObjectRequest message 14 func NewIRODSMessageCloseDataObjectRequest(desc int) *IRODSMessageCloseDataObjectRequest { 15 request := &IRODSMessageCloseDataObjectRequest{ 16 FileDescriptor: desc, 17 Size: 0, 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 *IRODSMessageCloseDataObjectRequest) AddKeyVal(key common.KeyWord, val string) { 32 msg.KeyVals.Add(string(key), val) 33 } 34 35 // GetBytes returns byte array 36 func (msg *IRODSMessageCloseDataObjectRequest) 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 *IRODSMessageCloseDataObjectRequest) 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 *IRODSMessageCloseDataObjectRequest) 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_CLOSE_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 }