github.com/cyverse/go-irodsclient@v0.13.2/irods/message/extract_struct_file_request.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  // IRODSMessageExtractStructFileRequest stores struct file extraction request
    12  type IRODSMessageExtractStructFileRequest struct {
    13  	XMLName          xml.Name             `xml:"StructFileExtAndRegInp_PI"`
    14  	Path             string               `xml:"objPath"`
    15  	TargetCollection string               `xml:"collection"`
    16  	OperationType    int                  `xml:"oprType"`
    17  	Flags            int                  `xml:"flags"` // unused
    18  	KeyVals          IRODSMessageSSKeyVal `xml:"KeyValPair_PI"`
    19  }
    20  
    21  // NewIRODSMessageExtractStructFileRequest creates a IRODSMessageExtractstructfileRequest message
    22  func NewIRODSMessageExtractStructFileRequest(path string, targetCollection string, resource string, dataType types.DataType, force bool, bulkReg bool) *IRODSMessageExtractStructFileRequest {
    23  	request := &IRODSMessageExtractStructFileRequest{
    24  		Path:             path,
    25  		TargetCollection: targetCollection,
    26  		OperationType:    0,
    27  		Flags:            0,
    28  		KeyVals: IRODSMessageSSKeyVal{
    29  			Length: 0,
    30  		},
    31  	}
    32  
    33  	if len(dataType) > 0 {
    34  		request.KeyVals.Add(string(common.DATA_TYPE_KW), string(dataType))
    35  	}
    36  
    37  	if len(resource) > 0 {
    38  		request.KeyVals.Add(string(common.DEST_RESC_NAME_KW), resource)
    39  	}
    40  
    41  	if force {
    42  		request.KeyVals.Add(string(common.FORCE_FLAG_KW), "")
    43  	}
    44  
    45  	if bulkReg {
    46  		request.KeyVals.Add(string(common.BULK_OPR_KW), "")
    47  	}
    48  
    49  	return request
    50  }
    51  
    52  // AddKeyVal adds a key-value pair
    53  func (msg *IRODSMessageExtractStructFileRequest) AddKeyVal(key common.KeyWord, val string) {
    54  	msg.KeyVals.Add(string(key), val)
    55  }
    56  
    57  // GetBytes returns byte array
    58  func (msg *IRODSMessageExtractStructFileRequest) GetBytes() ([]byte, error) {
    59  	xmlBytes, err := xml.Marshal(msg)
    60  	if err != nil {
    61  		return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err)
    62  	}
    63  	return xmlBytes, nil
    64  }
    65  
    66  // FromBytes returns struct from bytes
    67  func (msg *IRODSMessageExtractStructFileRequest) FromBytes(bytes []byte) error {
    68  	err := xml.Unmarshal(bytes, msg)
    69  	if err != nil {
    70  		return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err)
    71  	}
    72  	return nil
    73  }
    74  
    75  // GetMessage builds a message
    76  func (msg *IRODSMessageExtractStructFileRequest) GetMessage() (*IRODSMessage, error) {
    77  	bytes, err := msg.GetBytes()
    78  	if err != nil {
    79  		return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err)
    80  	}
    81  
    82  	msgBody := IRODSMessageBody{
    83  		Type:    RODS_MESSAGE_API_REQ_TYPE,
    84  		Message: bytes,
    85  		Error:   nil,
    86  		Bs:      nil,
    87  		IntInfo: int32(common.STRUCT_FILE_EXT_AND_REG_AN),
    88  	}
    89  
    90  	msgHeader, err := msgBody.BuildHeader()
    91  	if err != nil {
    92  		return nil, xerrors.Errorf("failed to build header from irods message: %w", err)
    93  	}
    94  
    95  	return &IRODSMessage{
    96  		Header: msgHeader,
    97  		Body:   &msgBody,
    98  	}, nil
    99  }