github.com/cyverse/go-irodsclient@v0.13.2/irods/message/get_descriptor_info_request.go (about) 1 package message 2 3 import ( 4 "encoding/base64" 5 "encoding/json" 6 "encoding/xml" 7 8 "github.com/cyverse/go-irodsclient/irods/common" 9 "golang.org/x/xerrors" 10 ) 11 12 // IRODSMessageGetDescriptorInfoRequest stores data object descriptor info. request 13 type IRODSMessageGetDescriptorInfoRequest struct { 14 FileDescriptor int `json:"fd"` 15 } 16 17 // NewIRODSMessageGetDescriptorInfoRequest creates a IRODSMessageDescriptorInfoRequest message 18 func NewIRODSMessageGetDescriptorInfoRequest(desc int) *IRODSMessageGetDescriptorInfoRequest { 19 return &IRODSMessageGetDescriptorInfoRequest{ 20 FileDescriptor: desc, 21 } 22 } 23 24 // GetBytes returns byte array 25 func (msg *IRODSMessageGetDescriptorInfoRequest) GetBytes() ([]byte, error) { 26 jsonBody, err := json.Marshal(msg) 27 if err != nil { 28 return nil, xerrors.Errorf("failed to marshal irods message to json: %w", err) 29 } 30 31 jsonBodyBin := base64.StdEncoding.EncodeToString(jsonBody) 32 33 binBytesBuf := IRODSMessageBinBytesBuf{ 34 Length: len(jsonBody), // use original data's length 35 Data: jsonBodyBin, 36 } 37 38 xmlBytes, err := xml.Marshal(binBytesBuf) 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 *IRODSMessageGetDescriptorInfoRequest) FromBytes(bytes []byte) error { 47 binBytesBuf := IRODSMessageBinBytesBuf{} 48 err := xml.Unmarshal(bytes, &binBytesBuf) 49 if err != nil { 50 return xerrors.Errorf("failed to marshal irods message to xml: %w", err) 51 } 52 53 jsonBody, err := base64.StdEncoding.DecodeString(binBytesBuf.Data) 54 if err != nil { 55 return xerrors.Errorf("failed to decode base64 data: %w", err) 56 } 57 58 err = json.Unmarshal(jsonBody, msg) 59 if err != nil { 60 return xerrors.Errorf("failed to unmarshal json to irods message: %w", err) 61 } 62 return nil 63 } 64 65 // GetMessage builds a message 66 func (msg *IRODSMessageGetDescriptorInfoRequest) GetMessage() (*IRODSMessage, error) { 67 bytes, err := msg.GetBytes() 68 if err != nil { 69 return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err) 70 } 71 72 msgBody := IRODSMessageBody{ 73 Type: RODS_MESSAGE_API_REQ_TYPE, 74 Message: bytes, 75 Error: nil, 76 Bs: nil, 77 IntInfo: int32(common.GET_FILE_DESCRIPTOR_INFO_APN), 78 } 79 80 msgHeader, err := msgBody.BuildHeader() 81 if err != nil { 82 return nil, xerrors.Errorf("failed to build header from irods message: %w", err) 83 } 84 85 return &IRODSMessage{ 86 Header: msgHeader, 87 Body: &msgBody, 88 }, nil 89 }