blob: 318482fa0c92855c00052060ce1aea32d5120f18 [file] [log] [blame]
khenaidoocfee5f42018-07-19 22:47:38 -04001package kvstore
2
3import (
4 "fmt"
5 "time"
6)
7
8// GetDuration converts a timeout value from int to duration. If the timeout value is
9// either not set of -ve then we default KV timeout (configurable) is used.
10func GetDuration(timeout int) time.Duration {
11 if timeout <= 0 {
12 return defaultKVGetTimeout * time.Second
13 }
14 return time.Duration(timeout) * time.Second
15}
16
17// ToString converts an interface value to a string. The interface should either be of
18// a string type or []byte. Otherwise, an error is returned.
19func ToString(value interface{}) (string, error) {
20 switch t := value.(type) {
21 case []byte:
22 return string(value.([]byte)), nil
23 case string:
24 return value.(string), nil
25 default:
26 return "", fmt.Errorf("unexpected-type-%T", t)
27 }
28}
29
30// ToByte converts an interface value to a []byte. The interface should either be of
31// a string type or []byte. Otherwise, an error is returned.
32func ToByte(value interface{}) ([]byte, error) {
33 switch t := value.(type) {
34 case []byte:
35 return value.([]byte), nil
36 case string:
37 return []byte(value.(string)), nil
38 default:
39 return nil, fmt.Errorf("unexpected-type-%T", t)
40 }
41}