fix: slice envs

This commit is contained in:
Lakhan Samani
2022-05-31 08:14:03 +05:30
parent c61c3024ec
commit cf8762b7a0
35 changed files with 557 additions and 317 deletions

View File

@@ -1,6 +1,8 @@
package inmemory
import "sync"
import (
"sync"
)
// EnvStore struct to store the env variables
type EnvStore struct {
@@ -23,6 +25,7 @@ func (e *EnvStore) UpdateStore(store map[string]interface{}) {
func (e *EnvStore) GetStore() map[string]interface{} {
e.mutex.Lock()
defer e.mutex.Unlock()
return e.store
}

View File

@@ -1,6 +1,7 @@
package inmemory
import (
"fmt"
"strings"
)
@@ -15,8 +16,8 @@ func (c *provider) ClearStore() error {
// GetUserSessions returns all the user session token from the in-memory store.
func (c *provider) GetUserSessions(userId string) map[string]string {
// c.mutex.Lock()
// defer c.mutex.Unlock()
c.mutex.Lock()
defer c.mutex.Unlock()
res := map[string]string{}
for k, v := range c.stateStore {
split := strings.Split(v, "@")
@@ -30,8 +31,8 @@ func (c *provider) GetUserSessions(userId string) map[string]string {
// DeleteAllUserSession deletes all the user sessions from in-memory store.
func (c *provider) DeleteAllUserSession(userId string) error {
// c.mutex.Lock()
// defer c.mutex.Unlock()
c.mutex.Lock()
defer c.mutex.Unlock()
sessions := c.GetUserSessions(userId)
for k := range sessions {
c.RemoveState(k)
@@ -91,24 +92,17 @@ func (c *provider) UpdateEnvVariable(key string, value interface{}) error {
// GetStringStoreEnvVariable to get the env variable from string store object
func (c *provider) GetStringStoreEnvVariable(key string) (string, error) {
res := c.envStore.Get(key)
return res.(string), nil
if res == nil {
return "", nil
}
return fmt.Sprintf("%v", res), nil
}
// GetBoolStoreEnvVariable to get the env variable from bool store object
func (c *provider) GetBoolStoreEnvVariable(key string) (bool, error) {
res := c.envStore.Get(key)
if res == nil {
return false, nil
}
return res.(bool), nil
}
// GetSliceStoreEnvVariable to get the env variable from slice store object
func (c *provider) GetSliceStoreEnvVariable(key string) ([]string, error) {
res := c.envStore.Get(key)
data := res.([]interface{})
var resSlice []string
for _, v := range data {
resSlice = append(resSlice, v.(string))
}
return resSlice, nil
}

View File

@@ -27,6 +27,4 @@ type Provider interface {
GetStringStoreEnvVariable(key string) (string, error)
// GetBoolStoreEnvVariable to get the bool env variable from env store
GetBoolStoreEnvVariable(key string) (bool, error)
// GetSliceStoreEnvVariable to get the string slice env variable from env store
GetSliceStoreEnvVariable(key string) ([]string, error)
}

View File

@@ -148,15 +148,3 @@ func (c *provider) GetBoolStoreEnvVariable(key string) (bool, error) {
return res, nil
}
// GetSliceStoreEnvVariable to get the string slice env variable from env store
func (c *provider) GetSliceStoreEnvVariable(key string) ([]string, error) {
var res []string
err := c.store.Get(c.ctx, envStorePrefix+key).Scan(&res)
if err != nil {
log.Debug("error getting token from redis store: ", err)
return nil, err
}
return res, nil
}