Sanitization of API.
parent
575ec34ef7
commit
be84e9ddcc
|
@ -19,16 +19,20 @@ type cacheEntry struct {
|
|||
contents interface{}
|
||||
}
|
||||
|
||||
// A Config is a dynamic configuration key lookup mechanism.
|
||||
type Config struct {
|
||||
cache map[string]cacheEntry
|
||||
buildFileList func(map[string]string) []string
|
||||
l sync.Mutex
|
||||
}
|
||||
|
||||
func New() *Config {
|
||||
// New takes a file list builder and returns a new instance of Config.
|
||||
//
|
||||
// The instance is ready to use.
|
||||
func New(f func(map[string]string) []string) *Config {
|
||||
var c Config
|
||||
c.cache = make(map[string]cacheEntry)
|
||||
c.l.Lock() // Lock until we have a proper file list builder
|
||||
c.buildFileList = f
|
||||
return &c
|
||||
}
|
||||
|
||||
|
@ -77,15 +81,6 @@ func (c *Config) lookupvar(key, path string) interface{} {
|
|||
return c.cache[path].contents.(map[string]interface{})[key]
|
||||
}
|
||||
|
||||
// SetFileListBuilder registers file list builder function.
|
||||
//
|
||||
// Registered function takes context (key-value map[string]string) as the only
|
||||
// argument and return a slice of strings - file paths.
|
||||
func (c *Config) SetFileListBuilder(f func(map[string]string) []string) {
|
||||
c.buildFileList = f
|
||||
c.l.Unlock()
|
||||
}
|
||||
|
||||
// Lookup searches for requested configuration key in file list built using
|
||||
// context.
|
||||
func (c *Config) Lookup(context map[string]string, key string) interface{} {
|
||||
|
|
|
@ -237,12 +237,10 @@ func TestLookupvarConditions(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
var c *Config = New()
|
||||
var c2 *Config = New()
|
||||
var c *Config = New(configLookupHelper)
|
||||
var c2 *Config = New(complicatedLookupHelper)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
c.SetFileListBuilder(configLookupHelper)
|
||||
c2.SetFileListBuilder(complicatedLookupHelper)
|
||||
log.SetOutput(ioutil.Discard)
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue