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

     1  /*
     2   * MinIO Cloud Storage, (C) 2020 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 replication
    18  
    19  import (
    20  	"encoding/xml"
    21  )
    22  
    23  // And - a tag to combine a prefix and multiple tags for replication configuration rule.
    24  type And struct {
    25  	XMLName xml.Name `xml:"And" json:"And"`
    26  	Prefix  string   `xml:"Prefix,omitempty" json:"Prefix,omitempty"`
    27  	Tags    []Tag    `xml:"Tag,omitempty" json:"Tag,omitempty"`
    28  }
    29  
    30  var errDuplicateTagKey = Errorf("Duplicate Tag Keys are not allowed")
    31  
    32  // isEmpty returns true if Tags field is null
    33  func (a And) isEmpty() bool {
    34  	return len(a.Tags) == 0 && a.Prefix == ""
    35  }
    36  
    37  // Validate - validates the And field
    38  func (a And) Validate() error {
    39  	if a.ContainsDuplicateTag() {
    40  		return errDuplicateTagKey
    41  	}
    42  	for _, t := range a.Tags {
    43  		if err := t.Validate(); err != nil {
    44  			return err
    45  		}
    46  	}
    47  	return nil
    48  }
    49  
    50  // ContainsDuplicateTag - returns true if duplicate keys are present in And
    51  func (a And) ContainsDuplicateTag() bool {
    52  	x := make(map[string]struct{}, len(a.Tags))
    53  
    54  	for _, t := range a.Tags {
    55  		if _, has := x[t.Key]; has {
    56  			return true
    57  		}
    58  		x[t.Key] = struct{}{}
    59  	}
    60  
    61  	return false
    62  }