github.com/cyverse/go-irodsclient@v0.13.2/irods/message/modify_metadata_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 // IRODSMessageModifyMetadataRequest stores alter metadata request 13 type IRODSMessageModifyMetadataRequest struct { 14 XMLName xml.Name `xml:"ModAVUMetadataInp_PI"` 15 Operation string `xml:"arg0"` // add, adda, rm, rmw, rmi, cp, mod, set 16 ItemType string `xml:"arg1"` // -d, -D, -c, -C, -r, -R, -u, -U 17 ItemName string `xml:"arg2"` 18 AttrName string `xml:"arg3"` 19 AttrValue string `xml:"arg4"` 20 AttrUnits string `xml:"arg5"` 21 NewAttrName string `xml:"arg6"` // new attr name (for mod) 22 NewAttrValue string `xml:"arg7"` // new attr value (for mod) 23 NewAttrUnits string `xml:"arg8"` // new attr unit (for mod) 24 Arg9 string `xml:"arg9"` // unused 25 } 26 27 // NewIRODSMessageAddMetadataRequest creates a IRODSMessageModMetaRequest message for adding a metadata AVU on some item. 28 // metadata.AVUID is ignored 29 func NewIRODSMessageAddMetadataRequest(itemType types.IRODSMetaItemType, itemName string, metadata *types.IRODSMeta) *IRODSMessageModifyMetadataRequest { 30 request := &IRODSMessageModifyMetadataRequest{ 31 Operation: "add", 32 ItemType: string(itemType), 33 ItemName: itemName, 34 AttrName: metadata.Name, 35 AttrValue: metadata.Value, 36 AttrUnits: metadata.Units, 37 } 38 39 return request 40 } 41 42 // NewIRODSMessageReplaceMetadataRequest creates a IRODSMessageModMetaRequest message for replacing a metadata AVU. 43 // oldMetadata.AVUID and newMetadata.AVUID are ignored, the old AVU is queried by its name, value and unit. 44 func NewIRODSMessageReplaceMetadataRequest(itemType types.IRODSMetaItemType, itemName string, oldMetadata *types.IRODSMeta, newMetadata *types.IRODSMeta) *IRODSMessageModifyMetadataRequest { 45 request := &IRODSMessageModifyMetadataRequest{ 46 Operation: "mod", 47 ItemType: string(itemType), 48 ItemName: itemName, 49 AttrName: oldMetadata.Name, 50 AttrValue: oldMetadata.Value, 51 AttrUnits: oldMetadata.Units, 52 NewAttrName: newMetadata.Name, 53 NewAttrValue: newMetadata.Value, 54 NewAttrUnits: newMetadata.Units, 55 } 56 57 return request 58 } 59 60 // NewIRODSMessageRemoveMetadataRequest creates a IRODSMessageModMetaRequest message for removing a metadata AVU. 61 // metadata.AVUID is ignored, the AVU is queried by its name, value and unit. 62 func NewIRODSMessageRemoveMetadataRequest(itemType types.IRODSMetaItemType, itemName string, metadata *types.IRODSMeta) *IRODSMessageModifyMetadataRequest { 63 request := &IRODSMessageModifyMetadataRequest{ 64 Operation: "rm", 65 ItemType: string(itemType), 66 ItemName: itemName, 67 AttrName: metadata.Name, 68 AttrValue: metadata.Value, 69 AttrUnits: metadata.Units, 70 } 71 72 return request 73 } 74 75 // NewIRODSMessageRemoveMetadataByIDRequest creates a IRODSMessageModMetaRequest message for removing a metadata AVU by its AVUID. 76 func NewIRODSMessageRemoveMetadataByIDRequest(itemType types.IRODSMetaItemType, itemName string, AVUID int64) *IRODSMessageModifyMetadataRequest { 77 request := &IRODSMessageModifyMetadataRequest{ 78 Operation: "rmi", 79 ItemType: string(itemType), 80 ItemName: itemName, 81 AttrName: fmt.Sprintf("%d", AVUID), 82 } 83 84 return request 85 } 86 87 // NewIRODSMessageRemoveMetadataWildcardRequest creates a IRODSMessageModMetaRequest message for removing a metadata AVU by itemName and attributeValue. 88 func NewIRODSMessageRemoveMetadataWildcardRequest(itemType types.IRODSMetaItemType, itemName, attName string) *IRODSMessageModifyMetadataRequest { 89 request := &IRODSMessageModifyMetadataRequest{ 90 Operation: "rmw", 91 ItemType: string(itemType), 92 ItemName: itemName, 93 AttrName: attName, 94 AttrValue: "%", 95 AttrUnits: "%", 96 } 97 98 return request 99 } 100 101 // NewIRODSMessageSetMetadataRequest creates a IRODSMessageModMetaRequest message for changing the first metadata AVU on the given item with a matching attribute name to the given value an units. 102 // metadata.AVUID is ignored. 103 func NewIRODSMessageSetMetadataRequest(itemType types.IRODSMetaItemType, itemName string, metadata *types.IRODSMeta) *IRODSMessageModifyMetadataRequest { 104 request := &IRODSMessageModifyMetadataRequest{ 105 Operation: "set", 106 ItemType: string(itemType), 107 ItemName: itemName, 108 AttrName: metadata.Name, 109 AttrValue: metadata.Value, 110 AttrUnits: metadata.Units, 111 } 112 113 return request 114 } 115 116 // GetBytes returns byte array 117 func (msg *IRODSMessageModifyMetadataRequest) GetBytes() ([]byte, error) { 118 xmlBytes, err := xml.Marshal(msg) 119 if err != nil { 120 return nil, xerrors.Errorf("failed to marshal irods message to xml: %w", err) 121 } 122 return xmlBytes, nil 123 } 124 125 // FromBytes returns struct from bytes 126 func (msg *IRODSMessageModifyMetadataRequest) FromBytes(bytes []byte) error { 127 err := xml.Unmarshal(bytes, msg) 128 if err != nil { 129 return xerrors.Errorf("failed to unmarshal xml to irods message: %w", err) 130 } 131 return nil 132 } 133 134 // GetMessage builds a message 135 func (msg *IRODSMessageModifyMetadataRequest) GetMessage() (*IRODSMessage, error) { 136 bytes, err := msg.GetBytes() 137 if err != nil { 138 return nil, xerrors.Errorf("failed to get bytes from irods message: %w", err) 139 } 140 141 msgBody := IRODSMessageBody{ 142 Type: RODS_MESSAGE_API_REQ_TYPE, 143 Message: bytes, 144 Error: nil, 145 Bs: nil, 146 IntInfo: int32(common.MOD_AVU_METADATA_AN), 147 } 148 149 msgHeader, err := msgBody.BuildHeader() 150 if err != nil { 151 return nil, xerrors.Errorf("failed to build header from irods message: %w", err) 152 } 153 154 return &IRODSMessage{ 155 Header: msgHeader, 156 Body: &msgBody, 157 }, nil 158 }