storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/lifecycle/tag.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2019 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 lifecycle
    18  
    19  import (
    20  	"encoding/xml"
    21  	"io"
    22  	"unicode/utf8"
    23  )
    24  
    25  // Tag - a tag for a lifecycle configuration Rule filter.
    26  type Tag struct {
    27  	XMLName xml.Name `xml:"Tag"`
    28  	Key     string   `xml:"Key,omitempty"`
    29  	Value   string   `xml:"Value,omitempty"`
    30  }
    31  
    32  var (
    33  	errInvalidTagKey   = Errorf("The TagKey you have provided is invalid")
    34  	errInvalidTagValue = Errorf("The TagValue you have provided is invalid")
    35  
    36  	errDuplicatedXMLTag = Errorf("duplicated XML Tag")
    37  	errUnknownXMLTag    = Errorf("unknown XML Tag")
    38  )
    39  
    40  // UnmarshalXML - decodes XML data.
    41  func (tag *Tag) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
    42  	var keyAlreadyParsed, valueAlreadyParsed bool
    43  	for {
    44  		// Read tokens from the XML document in a stream.
    45  		t, err := d.Token()
    46  		if err != nil {
    47  			if err == io.EOF {
    48  				break
    49  			}
    50  			return err
    51  		}
    52  
    53  		switch se := t.(type) {
    54  		case xml.StartElement:
    55  			var s string
    56  			if err = d.DecodeElement(&s, &se); err != nil {
    57  				return err
    58  			}
    59  			switch se.Name.Local {
    60  			case "Key":
    61  				if keyAlreadyParsed {
    62  					return errDuplicatedXMLTag
    63  				}
    64  				tag.Key = s
    65  				keyAlreadyParsed = true
    66  			case "Value":
    67  				if valueAlreadyParsed {
    68  					return errDuplicatedXMLTag
    69  				}
    70  				tag.Value = s
    71  				valueAlreadyParsed = true
    72  			default:
    73  				return errUnknownXMLTag
    74  			}
    75  		}
    76  	}
    77  
    78  	return nil
    79  }
    80  
    81  func (tag Tag) String() string {
    82  	return tag.Key + "=" + tag.Value
    83  }
    84  
    85  // IsEmpty returns whether this tag is empty or not.
    86  func (tag Tag) IsEmpty() bool {
    87  	return tag.Key == ""
    88  }
    89  
    90  // Validate checks this tag.
    91  func (tag Tag) Validate() error {
    92  	if len(tag.Key) == 0 || utf8.RuneCountInString(tag.Key) > 128 {
    93  		return errInvalidTagKey
    94  	}
    95  
    96  	if utf8.RuneCountInString(tag.Value) > 256 {
    97  		return errInvalidTagValue
    98  	}
    99  
   100  	return nil
   101  }