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