2019-10-31 21:05:50 +03:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-11-01 20:39:45 +03:00
|
|
|
var _ Config = &memConfig{}
|
|
|
|
|
2019-10-31 21:05:50 +03:00
|
|
|
type memConfig struct {
|
|
|
|
config map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMemConfig(config map[string]string) *memConfig {
|
|
|
|
return &memConfig{config: config}
|
|
|
|
}
|
|
|
|
|
2019-11-01 20:39:45 +03:00
|
|
|
func (mc *memConfig) StoreString(key, value string) error {
|
2019-10-31 21:05:50 +03:00
|
|
|
mc.config[key] = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-01 20:39:45 +03:00
|
|
|
func (mc *memConfig) StoreBool(key string, value bool) error {
|
|
|
|
return mc.StoreString(key, strconv.FormatBool(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *memConfig) StoreTimestamp(key string, value time.Time) error {
|
|
|
|
return mc.StoreString(key, strconv.Itoa(int(value.Unix())))
|
|
|
|
}
|
|
|
|
|
2019-10-31 21:05:50 +03:00
|
|
|
func (mc *memConfig) ReadAll(keyPrefix string) (map[string]string, error) {
|
|
|
|
result := make(map[string]string)
|
|
|
|
for key, val := range mc.config {
|
|
|
|
if strings.HasPrefix(key, keyPrefix) {
|
|
|
|
result[key] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *memConfig) ReadString(key string) (string, error) {
|
|
|
|
// unlike git, the mock can only store one value for the same key
|
|
|
|
val, ok := mc.config[key]
|
|
|
|
if !ok {
|
|
|
|
return "", ErrNoConfigEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *memConfig) ReadBool(key string) (bool, error) {
|
|
|
|
// unlike git, the mock can only store one value for the same key
|
|
|
|
val, ok := mc.config[key]
|
|
|
|
if !ok {
|
|
|
|
return false, ErrNoConfigEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
return strconv.ParseBool(val)
|
|
|
|
}
|
|
|
|
|
2019-11-03 18:46:51 +03:00
|
|
|
func (mc *memConfig) ReadTimestamp(key string) (time.Time, error) {
|
2019-10-31 21:05:50 +03:00
|
|
|
value, err := mc.ReadString(key)
|
|
|
|
if err != nil {
|
2019-11-03 18:46:51 +03:00
|
|
|
return time.Time{}, err
|
2019-10-31 21:05:50 +03:00
|
|
|
}
|
2019-11-03 18:46:51 +03:00
|
|
|
|
2019-10-31 21:05:50 +03:00
|
|
|
timestamp, err := strconv.Atoi(value)
|
|
|
|
if err != nil {
|
2019-11-03 18:46:51 +03:00
|
|
|
return time.Time{}, err
|
2019-10-31 21:05:50 +03:00
|
|
|
}
|
|
|
|
|
2019-11-03 18:46:51 +03:00
|
|
|
return time.Unix(int64(timestamp), 0), nil
|
2019-10-31 21:05:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// RmConfigs remove all key/value pair matching the key prefix
|
|
|
|
func (mc *memConfig) RemoveAll(keyPrefix string) error {
|
|
|
|
for key := range mc.config {
|
|
|
|
if strings.HasPrefix(key, keyPrefix) {
|
|
|
|
delete(mc.config, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|