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