github.com/minio/mc@v0.0.0-20240507152021-646712d5e5fb/cmd/arg-kvs.go (about)

     1  // Copyright (c) 2015-2022 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 cmd
    19  
    20  // argKV - is a shorthand of each key value.
    21  type argKV struct {
    22  	Key   string `json:"key"`
    23  	Value string `json:"value"`
    24  }
    25  
    26  // argKVS - is a shorthand for some wrapper functions
    27  // to operate on list of key values.
    28  type argKVS []argKV
    29  
    30  // Empty - return if kv is empty
    31  func (kvs argKVS) Empty() bool {
    32  	return len(kvs) == 0
    33  }
    34  
    35  // Set sets a value, if not sets a default value.
    36  func (kvs *argKVS) Set(key, value string) {
    37  	for i, kv := range *kvs {
    38  		if kv.Key == key {
    39  			(*kvs)[i] = argKV{
    40  				Key:   key,
    41  				Value: value,
    42  			}
    43  			return
    44  		}
    45  	}
    46  	*kvs = append(*kvs, argKV{
    47  		Key:   key,
    48  		Value: value,
    49  	})
    50  }
    51  
    52  // Get - returns the value of a key, if not found returns empty.
    53  func (kvs argKVS) Get(key string) string {
    54  	v, ok := kvs.Lookup(key)
    55  	if ok {
    56  		return v
    57  	}
    58  	return ""
    59  }
    60  
    61  // Lookup - lookup a key in a list of argKVS
    62  func (kvs argKVS) Lookup(key string) (string, bool) {
    63  	for _, kv := range kvs {
    64  		if kv.Key == key {
    65  			return kv.Value, true
    66  		}
    67  	}
    68  	return "", false
    69  }