github.com/cyverse/go-irodsclient@v0.13.2/irods/message/truncate_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  // IRODSMessageTruncateDataObjectRequest stores data object truncation request
    11  type IRODSMessageTruncateDataObjectRequest IRODSMessageDataObjectRequest
    12  
    13  // NewIRODSMessageTruncateDataObjectRequest creates a IRODSMessageTruncateDataObjectRequest message
    14  func NewIRODSMessageTruncateDataObjectRequest(path string, size int64) *IRODSMessageTruncateDataObjectRequest {
    15  	request := &IRODSMessageTruncateDataObjectRequest{
    16  		Path:          path,
    17  		CreateMode:    0,
    18  		OpenFlags:     0,
    19  		Offset:        0,
    20  		Size:          size,
    21  		Threads:       0,
    22  		OperationType: 0,
    23  		KeyVals: IRODSMessageSSKeyVal{
    24  			Length: 0,
    25  		},
    26  	}
    27  
    28  	return request
    29  }
    30  
    31  // AddKeyVal adds a key-value pair
    32  func (msg *IRODSMessageTruncateDataObjectRequest) AddKeyVal(key common.KeyWord, val string) {
    33  	msg.KeyVals.Add(string(key), val)
    34  }
    35  
    36  // GetBytes returns byte array
    37  func (msg *IRODSMessageTruncateDataObjectRequest) GetBytes() ([]byte, error) {
    38  	xmlBytes, err := xml.Marshal(msg)
    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 *IRODSMessageTruncateDataObjectRequest) FromBytes(bytes []byte) error {
    47  	err := xml.Unmarshal(bytes, msg)
    48  	if err != nil {
    49  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    50  	}
    51  	return nil
    52  }
    53  
    54  // GetMessage builds a message
    55  func (msg *IRODSMessageTruncateDataObjectRequest) GetMessage() (*IRODSMessage, error) {
    56  	bytes, err := msg.GetBytes()
    57  	if err != nil {
    58  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    59  	}
    60  
    61  	msgBody := IRODSMessageBody{
    62  		Type:    RODS_MESSAGE_API_REQ_TYPE,
    63  		Message: bytes,
    64  		Error:   nil,
    65  		Bs:      nil,
    66  		IntInfo: int32(common.DATA_OBJ_TRUNCATE_AN),
    67  	}
    68  
    69  	msgHeader, err := msgBody.BuildHeader()
    70  	if err != nil {
    71  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
    72  	}
    73  
    74  	return &IRODSMessage{
    75  		Header: msgHeader,
    76  		Body:   &msgBody,
    77  	}, nil
    78  }