github.com/cyverse/go-irodsclient@v0.13.2/irods/message/query_response.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  // IRODSMessageQueryResponse stores query result
    12  type IRODSMessageQueryResponse struct {
    13  	XMLName        xml.Name                `xml:"GenQueryOut_PI"`
    14  	RowCount       int                     `xml:"rowCnt"`
    15  	AttributeCount int                     `xml:"attriCnt"`
    16  	ContinueIndex  int                     `xml:"continueInx"`
    17  	TotalRowCount  int                     `xml:"totalRowCount"`
    18  	SQLResult      []IRODSMessageSQLResult `xml:"SqlResult_PI"`
    19  
    20  	// stores error result
    21  	Result int `xml:"-"`
    22  }
    23  
    24  // GetBytes returns byte array
    25  func (msg *IRODSMessageQueryResponse) GetBytes() ([]byte, error) {
    26  	xmlBytes, err := xml.Marshal(msg)
    27  	if err != nil {
    28  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    29  	}
    30  	return xmlBytes, nil
    31  }
    32  
    33  // CheckError returns error if server returned an error
    34  func (msg *IRODSMessageQueryResponse) CheckError() error {
    35  	if msg.Result < 0 {
    36  		return types.NewIRODSError(common.ErrorCode(msg.Result))
    37  	}
    38  	return nil
    39  }
    40  
    41  // FromBytes returns struct from bytes
    42  func (msg *IRODSMessageQueryResponse) 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  // FromMessage returns struct from IRODSMessage
    51  func (msg *IRODSMessageQueryResponse) FromMessage(msgIn *IRODSMessage) error {
    52  	if msgIn.Body == nil {
    53  		return xerrors.Errorf("empty message body")
    54  	}
    55  
    56  	err := msg.FromBytes(msgIn.Body.Message)
    57  	msg.Result = int(msgIn.Body.IntInfo)
    58  	if err != nil {
    59  		return xerrors.Errorf("failed to get irods message from message body")
    60  	}
    61  	return nil
    62  }