storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/bucket/policy/condition/valueset.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 condition
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  )
    23  
    24  // ValueSet - unique list of values.
    25  type ValueSet map[Value]struct{}
    26  
    27  // Add - adds given value to value set.
    28  func (set ValueSet) Add(value Value) {
    29  	set[value] = struct{}{}
    30  }
    31  
    32  // ToSlice converts ValueSet to a slice of Value
    33  func (set ValueSet) ToSlice() []Value {
    34  	var values []Value
    35  	for k := range set {
    36  		values = append(values, k)
    37  	}
    38  	return values
    39  }
    40  
    41  // MarshalJSON - encodes ValueSet to JSON data.
    42  func (set ValueSet) MarshalJSON() ([]byte, error) {
    43  	var values []Value
    44  	for k := range set {
    45  		values = append(values, k)
    46  	}
    47  
    48  	if len(values) == 0 {
    49  		return nil, fmt.Errorf("invalid value set %v", set)
    50  	}
    51  
    52  	return json.Marshal(values)
    53  }
    54  
    55  // UnmarshalJSON - decodes JSON data.
    56  func (set *ValueSet) UnmarshalJSON(data []byte) error {
    57  	var v Value
    58  	if err := json.Unmarshal(data, &v); err == nil {
    59  		*set = make(ValueSet)
    60  		set.Add(v)
    61  		return nil
    62  	}
    63  
    64  	var values []Value
    65  	if err := json.Unmarshal(data, &values); err != nil {
    66  		return err
    67  	}
    68  
    69  	if len(values) < 1 {
    70  		return fmt.Errorf("invalid value")
    71  	}
    72  
    73  	*set = make(ValueSet)
    74  	for _, v = range values {
    75  		if _, found := (*set)[v]; found {
    76  			return fmt.Errorf("duplicate value found '%v'", v)
    77  		}
    78  
    79  		set.Add(v)
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  // Clone clones ValueSet structure
    86  func (set ValueSet) Clone() ValueSet {
    87  	return NewValueSet(set.ToSlice()...)
    88  }
    89  
    90  // NewValueSet - returns new value set containing given values.
    91  func NewValueSet(values ...Value) ValueSet {
    92  	set := make(ValueSet)
    93  
    94  	for _, value := range values {
    95  		set.Add(value)
    96  	}
    97  
    98  	return set
    99  }