config.go

 1package repository
 2
 3import (
 4	"strconv"
 5	"time"
 6)
 7
 8// Config represent the common function interacting with the repository config storage
 9type Config interface {
10	// Store writes a single key/value pair in the config
11	StoreString(key, value string) error
12
13	// Store writes a key and timestamp value to the config
14	StoreTimestamp(key string, value time.Time) error
15
16	// Store writes a key and boolean value to the config
17	StoreBool(key string, value bool) error
18
19	// ReadAll reads all key/value pair matching the key prefix
20	ReadAll(keyPrefix string) (map[string]string, error)
21
22	// ReadBool read a single boolean value from the config
23	// Return ErrNoConfigEntry or ErrMultipleConfigEntry if
24	// there is zero or more than one entry for this key
25	ReadBool(key string) (bool, error)
26
27	// ReadBool read a single string value from the config
28	// Return ErrNoConfigEntry or ErrMultipleConfigEntry if
29	// there is zero or more than one entry for this key
30	ReadString(key string) (string, error)
31
32	// ReadTimestamp read a single timestamp value from the config
33	// Return ErrNoConfigEntry or ErrMultipleConfigEntry if
34	// there is zero or more than one entry for this key
35	ReadTimestamp(key string) (time.Time, error)
36
37	// RemoveAll removes all key/value pair matching the key prefix
38	RemoveAll(keyPrefix string) error
39}
40
41func ParseTimestamp(s string) (time.Time, error) {
42	timestamp, err := strconv.Atoi(s)
43	if err != nil {
44		return time.Time{}, err
45	}
46
47	return time.Unix(int64(timestamp), 0), nil
48}