github.com/cyverse/go-irodsclient@v0.13.2/irods/message/unlock_data_object_request.go (about)

     1  package message
     2  
     3  import (
     4  	"encoding/xml"
     5  	"fmt"
     6  
     7  	"github.com/cyverse/go-irodsclient/irods/common"
     8  	"github.com/cyverse/go-irodsclient/irods/types"
     9  	"golang.org/x/xerrors"
    10  )
    11  
    12  // IRODSMessageUnlockDataObjectRequest stores data object unlock request
    13  type IRODSMessageUnlockDataObjectRequest IRODSMessageDataObjectRequest
    14  
    15  // NewIRODSMessageUnlockDataObjectRequest creates a IRODSMessageUnlockDataObjectRequest message
    16  func NewIRODSMessageUnlockDataObjectRequest(path string, lockDesc int) *IRODSMessageUnlockDataObjectRequest {
    17  	request := &IRODSMessageUnlockDataObjectRequest{
    18  		Path:          path,
    19  		CreateMode:    0,
    20  		OpenFlags:     0,
    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.LOCK_TYPE_KW), string(types.DataObjectLockTypeUnlock))
    31  	request.KeyVals.Add(string(common.LOCK_FD_KW), fmt.Sprintf("%d", lockDesc))
    32  
    33  	return request
    34  }
    35  
    36  // AddKeyVal adds a key-value pair
    37  func (msg *IRODSMessageUnlockDataObjectRequest) AddKeyVal(key common.KeyWord, val string) {
    38  	msg.KeyVals.Add(string(key), val)
    39  }
    40  
    41  // GetBytes returns byte array
    42  func (msg *IRODSMessageUnlockDataObjectRequest) GetBytes() ([]byte, error) {
    43  	xmlBytes, err := xml.Marshal(msg)
    44  	if err != nil {
    45  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    46  	}
    47  	return xmlBytes, nil
    48  }
    49  
    50  // FromBytes returns struct from bytes
    51  func (msg *IRODSMessageUnlockDataObjectRequest) FromBytes(bytes []byte) error {
    52  	err := xml.Unmarshal(bytes, msg)
    53  	if err != nil {
    54  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    55  	}
    56  	return nil
    57  }
    58  
    59  // GetMessage builds a message
    60  func (msg *IRODSMessageUnlockDataObjectRequest) GetMessage() (*IRODSMessage, error) {
    61  	bytes, err := msg.GetBytes()
    62  	if err != nil {
    63  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    64  	}
    65  
    66  	msgBody := IRODSMessageBody{
    67  		Type:    RODS_MESSAGE_API_REQ_TYPE,
    68  		Message: bytes,
    69  		Error:   nil,
    70  		Bs:      nil,
    71  		IntInfo: int32(common.DATA_OBJ_UNLOCK_AN),
    72  	}
    73  
    74  	msgHeader, err := msgBody.BuildHeader()
    75  	if err != nil {
    76  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
    77  	}
    78  
    79  	return &IRODSMessage{
    80  		Header: msgHeader,
    81  		Body:   &msgBody,
    82  	}, nil
    83  }