-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.go
98 lines (77 loc) · 1.83 KB
/
redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package configuration
import (
"encoding/json"
"errors"
"fmt"
"os"
"strconv"
"github.com/cjlapao/common-go/guard"
)
type RedisConfigurationProvider struct {
connectionString string
client string
}
func NewRedisConfigurationProvider(connString string) *RedisConfigurationProvider {
result := RedisConfigurationProvider{
connectionString: connString,
}
return &result
}
func (ev RedisConfigurationProvider) UpsertKey(key string, value interface{}) error {
emptyKey := guard.EmptyOrNil(key, "key")
emptyValue := guard.EmptyOrNil(value, "value")
if emptyKey != nil {
return emptyKey
}
if emptyValue != nil {
return emptyValue
}
switch v := value.(type) {
case int:
os.Setenv(key, strconv.Itoa(v))
case float64:
os.Setenv(key, fmt.Sprintf("%v", v))
case bool:
os.Setenv(key, fmt.Sprintf("%v", v))
os.Setenv(key, fmt.Sprintf("%v", v))
case string:
os.Setenv(key, v)
default:
jsonBytes, err := json.Marshal(v)
if err == nil {
os.Setenv(key, string(jsonBytes))
}
}
return nil
}
func (ev RedisConfigurationProvider) UpsertKeys(values map[string]interface{}) []error {
errorArray := make([]error, 0)
if values == nil {
errorArray = append(errorArray, errors.New("array is nil"))
return errorArray
}
if len(values) > 0 {
for key, value := range values {
emptykey := guard.EmptyOrNil(key)
emptyValue := guard.EmptyOrNil(value)
if emptykey != nil {
errorArray = append(errorArray, emptykey)
}
if emptyValue != nil {
errorArray = append(errorArray, emptyValue)
}
ev.UpsertKey(key, value)
}
return errorArray
}
return nil
}
func (ev RedisConfigurationProvider) Get(key string) interface{} {
return os.Getenv(key)
}
func (ev RedisConfigurationProvider) Clear(key string) {
emptyKey := guard.EmptyOrNil(key, "key")
if emptyKey == nil {
os.Setenv(key, "")
}
}