1
0
Fork 0

feat(kv): constants and generic methods.

This commit is contained in:
Fernandez Ludovic 2018-01-03 15:57:36 +01:00 committed by Traefiker
parent 61ecb4cd18
commit 6573634012
3 changed files with 228 additions and 10 deletions

View file

@ -26,8 +26,12 @@ func (p *Provider) buildConfiguration() *types.Configuration {
"List": p.list,
"ListServers": p.listServers,
"Get": p.get,
"GetBool": p.getBool,
"GetInt": p.getInt,
"GetInt64": p.getInt64,
"SplitGet": p.splitGet,
"Last": p.last,
"Has": p.has,
// Backend functions
"getSticky": p.getSticky,
@ -143,6 +147,41 @@ func (p *Provider) getBool(defaultValue bool, keyParts ...string) bool {
return value
}
func (p *Provider) has(keyParts ...string) bool {
value := p.get("", keyParts...)
return len(value) > 0
}
func (p *Provider) getInt(defaultValue int, keyParts ...string) int {
rawValue := p.get("", keyParts...)
if len(rawValue) == 0 {
return defaultValue
}
value, err := strconv.Atoi(rawValue)
if err != nil {
log.Errorf("Invalid value for %v: %s", keyParts, rawValue)
return defaultValue
}
return value
}
func (p *Provider) getInt64(defaultValue int64, keyParts ...string) int64 {
rawValue := p.get("", keyParts...)
if len(rawValue) == 0 {
return defaultValue
}
value, err := strconv.ParseInt(rawValue, 10, 64)
if err != nil {
log.Errorf("Invalid value for %v: %s", keyParts, rawValue)
return defaultValue
}
return value
}
func (p *Provider) list(keyParts ...string) []string {
rootKey := strings.Join(keyParts, "")