Skip to content
Snippets Groups Projects
Commit 139d9f2f authored by thijsheijden's avatar thijsheijden
Browse files

Added mock key value store

Also changed around some of the naming.
parent 28c98724
No related branches found
No related tags found
No related merge requests found
package keyvaluedriver package keyvaluedriver
// KeyValueStore is an interface for a key value storage // KeyValueStoreInterface is an interface for a key value storage
type KeyValueStore interface { type KeyValueStoreInterface interface {
Get(key *string) *string Get(key *string) *string
Set(key *string, value interface{}) error Set(key *string, value interface{}) error
} }
...@@ -9,18 +9,18 @@ import ( ...@@ -9,18 +9,18 @@ import (
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
) )
// RedisDriver models the redis driver // KeyValueDriver models the redis driver
type RedisDriver struct { type KeyValueDriver struct {
client *redis.Client client *redis.Client
} }
// NewRedisDriver creates and returns a redis driver // NewRedisDriver creates and returns a redis driver
func NewRedisDriver() *RedisDriver { func NewRedisDriver() *KeyValueDriver {
return &RedisDriver{} return &KeyValueDriver{}
} }
// Start starts the redis driver // Start starts the redis driver
func (d *RedisDriver) Start() { func (d *KeyValueDriver) Start() {
// Grab the redis host and port from environment vars // Grab the redis host and port from environment vars
redisAddress := os.Getenv("REDIS_ADDRESS") redisAddress := os.Getenv("REDIS_ADDRESS")
// redisPassword := os.Getenv("REDIS_PASSWORD") // redisPassword := os.Getenv("REDIS_PASSWORD")
...@@ -35,13 +35,13 @@ func (d *RedisDriver) Start() { ...@@ -35,13 +35,13 @@ func (d *RedisDriver) Start() {
} }
// Get retrieves the value from the redis store that belongs to the given key // Get retrieves the value from the redis store that belongs to the given key
func (d *RedisDriver) Get(key *string) *string { func (d *KeyValueDriver) Get(key *string) *string {
value := d.client.Get(context.Background(), *key).Val() value := d.client.Get(context.Background(), *key).Val()
return &value return &value
} }
// Set sets the key value pair in the redis store // Set sets the key value pair in the redis store
func (d *RedisDriver) Set(key *string, value interface{}) error { func (d *KeyValueDriver) Set(key *string, value interface{}) error {
status := d.client.Set(context.Background(), *key, value, 0) status := d.client.Set(context.Background(), *key, value, 0)
return status.Err() return status.Err()
} }
package mockkeyvaluedriver
// A KeyValueStore implements methods to set key-value data (mock)
type KeyValueStore struct {
data map[string]interface{}
}
// CreateKeyValueStore creates a key value store driver (mock)
func CreateKeyValueStore() *KeyValueStore {
return &KeyValueStore{
data: make(map[string]interface{}),
}
}
// Set sets a key to a value in the key value store. Expects a non-pointer as value. (mock)
func (kvs *KeyValueStore) Set(key *string, value interface{}) error {
kvs.data[*key] = value
return nil
}
// Get gets the value for the supplied key from the key value store (mock)
func (kvs *KeyValueStore) Get(key *string) interface{} {
return kvs.data[*key]
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment