github.com/cyverse/go-irodsclient@v0.13.2/irods/message/create_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  // IRODSMessageCreateDataObjectRequest stores data object creation request
    12  type IRODSMessageCreateDataObjectRequest IRODSMessageDataObjectRequest
    13  
    14  // NewIRODSMessageCreateDataObjectRequest creates a IRODSMessageCreateDataObjectRequest message
    15  func NewIRODSMessageCreateDataObjectRequest(path string, resource string, mode types.FileOpenMode, force bool) *IRODSMessageCreateDataObjectRequest {
    16  	flag := mode.GetFlag()
    17  	request := &IRODSMessageCreateDataObjectRequest{
    18  		Path:          path,
    19  		CreateMode:    0644,
    20  		OpenFlags:     flag,
    21  		Offset:        0,
    22  		Size:          -1,
    23  		Threads:       0,
    24  		OperationType: 0,
    25  		KeyVals: IRODSMessageSSKeyVal{
    26  			Length: 0,
    27  		},
    28  	}
    29  
    30  	request.KeyVals.Add(string(common.DATA_TYPE_KW), string(types.GENERIC_DT))
    31  
    32  	if len(resource) > 0 {
    33  		request.KeyVals.Add(string(common.DEST_RESC_NAME_KW), resource)
    34  	}
    35  
    36  	if force {
    37  		request.KeyVals.Add(string(common.FORCE_FLAG_KW), "")
    38  	}
    39  
    40  	return request
    41  }
    42  
    43  // NewIRODSMessageCreateobjRequestWithKeyVals creates a IRODSMessageCreateobjRequest message with given keyvals
    44  func NewIRODSMessageCreateobjRequestWithKeyVals(path string, resource string, mode types.FileOpenMode, force bool, keyvals map[string]string) *IRODSMessageCreateDataObjectRequest {
    45  	flag := mode.GetFlag()
    46  	request := &IRODSMessageCreateDataObjectRequest{
    47  		Path:          path,
    48  		CreateMode:    0644,
    49  		OpenFlags:     flag,
    50  		Offset:        0,
    51  		Size:          -1,
    52  		Threads:       0,
    53  		OperationType: 0,
    54  		KeyVals: IRODSMessageSSKeyVal{
    55  			Length: 0,
    56  		},
    57  	}
    58  
    59  	// if data type is not set
    60  	if _, ok := keyvals[string(common.DATA_TYPE_KW)]; !ok {
    61  		request.KeyVals.Add(string(common.DATA_TYPE_KW), string(types.GENERIC_DT))
    62  	}
    63  
    64  	if len(resource) > 0 {
    65  		request.KeyVals.Add(string(common.DEST_RESC_NAME_KW), resource)
    66  	}
    67  
    68  	if force {
    69  		request.KeyVals.Add(string(common.FORCE_FLAG_KW), "")
    70  	}
    71  
    72  	for key, val := range keyvals {
    73  		request.KeyVals.Add(key, val)
    74  	}
    75  
    76  	return request
    77  }
    78  
    79  // AddKeyVal adds a key-value pair
    80  func (msg *IRODSMessageCreateDataObjectRequest) AddKeyVal(key common.KeyWord, val string) {
    81  	msg.KeyVals.Add(string(key), val)
    82  }
    83  
    84  // GetBytes returns byte array
    85  func (msg *IRODSMessageCreateDataObjectRequest) GetBytes() ([]byte, error) {
    86  	xmlBytes, err := xml.Marshal(msg)
    87  	if err != nil {
    88  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    89  	}
    90  	return xmlBytes, nil
    91  }
    92  
    93  // FromBytes returns struct from bytes
    94  func (msg *IRODSMessageCreateDataObjectRequest) FromBytes(bytes []byte) error {
    95  	err := xml.Unmarshal(bytes, msg)
    96  	if err != nil {
    97  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    98  	}
    99  	return nil
   100  }
   101  
   102  // GetMessage builds a message
   103  func (msg *IRODSMessageCreateDataObjectRequest) GetMessage() (*IRODSMessage, error) {
   104  	bytes, err := msg.GetBytes()
   105  	if err != nil {
   106  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
   107  	}
   108  
   109  	msgBody := IRODSMessageBody{
   110  		Type:    RODS_MESSAGE_API_REQ_TYPE,
   111  		Message: bytes,
   112  		Error:   nil,
   113  		Bs:      nil,
   114  		IntInfo: int32(common.DATA_OBJ_CREATE_AN),
   115  	}
   116  
   117  	msgHeader, err := msgBody.BuildHeader()
   118  	if err != nil {
   119  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
   120  	}
   121  
   122  	return &IRODSMessage{
   123  		Header: msgHeader,
   124  		Body:   &msgBody,
   125  	}, nil
   126  }