mirror of
https://github.com/MichaelMure/git-bug.git
synced 2024-12-15 18:23:08 +03:00
88ad7e606f
- allow the creation of arbitrary Lamport clocks, freeing the way to new entities and removing Bug specific (upper layer) code. - generalize the memory-only and persisted Lamport clocks behind a common interface - rework the tests to provide reusable testing code for a Repo, a Clock, a Config, opening a path to add a new Repo implementation more easily - test previously untested components with those new tests Note: one problem found during this endeavor is that `identity.Version` also need to store one time + Lamport time for each other Entity (Bug, config, PR ...). This could possibly done without breaking change but it would be much easier to wait for https://github.com/MichaelMure/git-bug-migration to happen.
95 lines
1.9 KiB
Go
95 lines
1.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var _ Config = &MemConfig{}
|
|
|
|
type MemConfig struct {
|
|
config map[string]string
|
|
}
|
|
|
|
func NewMemConfig() *MemConfig {
|
|
return &MemConfig{
|
|
config: make(map[string]string),
|
|
}
|
|
}
|
|
|
|
func (mc *MemConfig) StoreString(key, value string) error {
|
|
mc.config[key] = value
|
|
return nil
|
|
}
|
|
|
|
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())))
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func (mc *MemConfig) ReadTimestamp(key string) (time.Time, error) {
|
|
value, err := mc.ReadString(key)
|
|
if err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
|
|
timestamp, err := strconv.Atoi(value)
|
|
if err != nil {
|
|
return time.Time{}, err
|
|
}
|
|
|
|
return time.Unix(int64(timestamp), 0), nil
|
|
}
|
|
|
|
// RmConfigs remove all key/value pair matching the key prefix
|
|
func (mc *MemConfig) RemoveAll(keyPrefix string) error {
|
|
found := false
|
|
for key := range mc.config {
|
|
if strings.HasPrefix(key, keyPrefix) {
|
|
delete(mc.config, key)
|
|
found = true
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
return fmt.Errorf("section not found")
|
|
}
|
|
|
|
return nil
|
|
}
|