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