storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/event/targetid.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2018 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package event 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "strings" 23 ) 24 25 // TargetID - holds identification and name strings of notification target. 26 type TargetID struct { 27 ID string 28 Name string 29 } 30 31 // String - returns string representation. 32 func (tid TargetID) String() string { 33 return tid.ID + ":" + tid.Name 34 } 35 36 // ToARN - converts to ARN. 37 func (tid TargetID) ToARN(region string) ARN { 38 return ARN{TargetID: tid, region: region} 39 } 40 41 // MarshalJSON - encodes to JSON data. 42 func (tid TargetID) MarshalJSON() ([]byte, error) { 43 return json.Marshal(tid.String()) 44 } 45 46 // UnmarshalJSON - decodes JSON data. 47 func (tid *TargetID) UnmarshalJSON(data []byte) error { 48 var s string 49 if err := json.Unmarshal(data, &s); err != nil { 50 return err 51 } 52 53 targetID, err := parseTargetID(s) 54 if err != nil { 55 return err 56 } 57 58 *tid = *targetID 59 return nil 60 } 61 62 // parseTargetID - parses string to TargetID. 63 func parseTargetID(s string) (*TargetID, error) { 64 tokens := strings.Split(s, ":") 65 if len(tokens) != 2 { 66 return nil, fmt.Errorf("invalid TargetID format '%v'", s) 67 } 68 69 return &TargetID{ 70 ID: tokens[0], 71 Name: tokens[1], 72 }, nil 73 }