18cb9257f9
Previously, to fetch data out of the `*config.Configuration` type, a reference to a `Fetcher` was used, a-la: ``` cfg.Env.Get(...) ``` This is quite convenient, however, it forces the LFS client to implement several methods more than once. Consider the interface: ``` type Fetcher interface { Get(key string) (val string) Bool(key string, def bool) (val bool) // et. al. } ``` In order to return typed information from a configuration instance, _each_ `Fetcher` must implement its own `N` methods for `Int`, `Bool`, etc. To remedy this, the `Environment` type was introduced. It instead _has_ a `Fetcher`, and defines its own type conversions, like so: ``` type Environment struct { f Fetcher } func (e *Environment) Bool(key string, def bool) (val bool) { } func (e *Environment) Int(key string, def int) (val int) { } // et. al. ``` Now, the `config.Configuration` type holds a reference to an `Environment`, and all type conversion methods are defined only once, saving time, and enforcing consistency across multiple sources.
11 lines
351 B
Go
11 lines
351 B
Go
package config
|
|
|
|
// Fetcher provides an interface to get typed information out of a configuration
|
|
// "source". These sources could be the OS enviornment, a .gitconfig, or even
|
|
// just a `map`.
|
|
type Fetcher interface {
|
|
// Get returns the string value assosicated with a given key, or an
|
|
// empty string if none exists.
|
|
Get(key string) (val string)
|
|
}
|