github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/fs/config/configmap/configmap.go (about)

     1  // Package configmap provides an abstraction for reading and writing config
     2  package configmap
     3  
     4  // Getter provides an interface to get config items
     5  type Getter interface {
     6  	// Get should get an item with the key passed in and return
     7  	// the value. If the item is found then it should return true,
     8  	// otherwise false.
     9  	Get(key string) (value string, ok bool)
    10  }
    11  
    12  // Setter provides an interface to set config items
    13  type Setter interface {
    14  	// Set should set an item into persistent config store.
    15  	Set(key, value string)
    16  }
    17  
    18  // Mapper provides an interface to read and write config
    19  type Mapper interface {
    20  	Getter
    21  	Setter
    22  }
    23  
    24  // Map provides a wrapper around multiple Setter and
    25  // Getter interfaces.
    26  type Map struct {
    27  	setters []Setter
    28  	getters []Getter
    29  }
    30  
    31  // New returns an empty Map
    32  func New() *Map {
    33  	return &Map{}
    34  }
    35  
    36  // AddGetter appends a getter onto the end of the getters
    37  func (c *Map) AddGetter(getter Getter) *Map {
    38  	c.getters = append(c.getters, getter)
    39  	return c
    40  }
    41  
    42  // AddGetters appends multiple getters onto the end of the getters
    43  func (c *Map) AddGetters(getters ...Getter) *Map {
    44  	c.getters = append(c.getters, getters...)
    45  	return c
    46  }
    47  
    48  // AddSetter appends a setter onto the end of the setters
    49  func (c *Map) AddSetter(setter Setter) *Map {
    50  	c.setters = append(c.setters, setter)
    51  	return c
    52  }
    53  
    54  // Get gets an item with the key passed in and return the value from
    55  // the first getter. If the item is found then it returns true,
    56  // otherwise false.
    57  func (c *Map) Get(key string) (value string, ok bool) {
    58  	for _, do := range c.getters {
    59  		value, ok = do.Get(key)
    60  		if ok {
    61  			return value, ok
    62  		}
    63  	}
    64  	return "", false
    65  }
    66  
    67  // Set sets an item into all the stored setters.
    68  func (c *Map) Set(key, value string) {
    69  	for _, do := range c.setters {
    70  		do.Set(key, value)
    71  	}
    72  }
    73  
    74  // Simple is a simple Mapper for testing
    75  type Simple map[string]string
    76  
    77  // Get the value
    78  func (c Simple) Get(key string) (value string, ok bool) {
    79  	value, ok = c[key]
    80  	return value, ok
    81  }
    82  
    83  // Set the value
    84  func (c Simple) Set(key, value string) {
    85  	c[key] = value
    86  }