Add LookupString and LookupStringSlice to config.

Allows for cleaner lookups in places that need it.
configurable-file-paths
Robert Gerus 2015-11-18 13:57:55 +01:00
parent 3f3683dade
commit 0d9aa21cbf
1 changed files with 45 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
"io/ioutil"
"log"
"os"
"sort"
"sync"
"time"
)
@ -100,3 +101,47 @@ func Lookup(context map[string]string, key string) interface{} {
log.Println("Context:", context, "Key", key, "not found")
return nil
}
// LookupString is analogous to Lookup(), but does the cast to string.
func LookupString(context map[string]string, key string) string {
var value interface{}
c.l.Lock()
defer c.l.Unlock()
for _, fpath := range c.buildFileList(context) {
log.Println("Context:", context, "Looking up", key, "in", fpath)
value = lookupvar(key, fpath)
if value != nil {
log.Println("Context:", context, "Found", key, value)
return value.(string)
}
}
log.Println("Context:", context, "Key", key, "not found")
return ""
}
// LookupStringSlice is analogous to Lookup(), but does the cast to []string
func LookupStringSlice(context map[string]string, key string) (retval []string) {
var value []interface{}
c.l.Lock()
defer c.l.Unlock()
for _, fpath := range c.buildFileList(context) {
log.Println("Context:", context, "Looking up", key, "in", fpath)
value = lookupvar(key, fpath).([]interface{})
if value != nil {
log.Println("Context:", context, "Found", key, value)
for _, s := range value {
retval = append(retval, s.(string))
}
sort.Strings(retval)
return retval
}
}
log.Println("Context:", context, "Key", key, "not found")
return []string{""}
}