github.com/cyverse/go-irodsclient@v0.13.2/irods/message/end_transaction_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  // IRODSMessageEndTransactionRequest stores collection creation request
    11  type IRODSMessageEndTransactionRequest struct {
    12  	XMLName  xml.Name `xml:"endTransactionInp_PI"`
    13  	Action   string   `xml:"arg0"`
    14  	Argument string   `xml:"arg1"` // unused
    15  }
    16  
    17  // NewIRODSMessageEndTransactionRequest creates a IRODSMessageEndTransactionRequest message
    18  func NewIRODSMessageEndTransactionRequest(commit bool) *IRODSMessageEndTransactionRequest {
    19  	var action string
    20  
    21  	if commit {
    22  		action = "commit"
    23  	} else {
    24  		action = "rollback"
    25  	}
    26  
    27  	return &IRODSMessageEndTransactionRequest{
    28  		Action: action,
    29  	}
    30  }
    31  
    32  // GetBytes returns byte array
    33  func (msg *IRODSMessageEndTransactionRequest) 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 *IRODSMessageEndTransactionRequest) 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 *IRODSMessageEndTransactionRequest) 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.END_TRANSACTION_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  }