github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/bucket/replication/and.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package replication
    19  
    20  import (
    21  	"encoding/xml"
    22  )
    23  
    24  // And - a tag to combine a prefix and multiple tags for replication configuration rule.
    25  type And struct {
    26  	XMLName xml.Name `xml:"And" json:"And"`
    27  	Prefix  string   `xml:"Prefix,omitempty" json:"Prefix,omitempty"`
    28  	Tags    []Tag    `xml:"Tag,omitempty" json:"Tag,omitempty"`
    29  }
    30  
    31  var errDuplicateTagKey = Errorf("Duplicate Tag Keys are not allowed")
    32  
    33  // isEmpty returns true if Tags field is null
    34  func (a And) isEmpty() bool {
    35  	return len(a.Tags) == 0 && a.Prefix == ""
    36  }
    37  
    38  // Validate - validates the And field
    39  func (a And) Validate() error {
    40  	if a.ContainsDuplicateTag() {
    41  		return errDuplicateTagKey
    42  	}
    43  	for _, t := range a.Tags {
    44  		if err := t.Validate(); err != nil {
    45  			return err
    46  		}
    47  	}
    48  	return nil
    49  }
    50  
    51  // ContainsDuplicateTag - returns true if duplicate keys are present in And
    52  func (a And) ContainsDuplicateTag() bool {
    53  	x := make(map[string]struct{}, len(a.Tags))
    54  
    55  	for _, t := range a.Tags {
    56  		if _, has := x[t.Key]; has {
    57  			return true
    58  		}
    59  		x[t.Key] = struct{}{}
    60  	}
    61  
    62  	return false
    63  }