...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package config
15
16 import (
17 "context"
18 "net/http"
19
20 internal "github.com/go-kivik/kivik/v4/int/errors"
21 )
22
23
24
25
26 type Config interface {
27 All(context.Context) (map[string]map[string]string, error)
28 Section(context.Context, string) (map[string]string, error)
29 Key(context.Context, string, string) (string, error)
30 Reload(context.Context) error
31 }
32
33
34 type Writer interface {
35
36 SetKey(context.Context, string, string, string) (string, error)
37 Delete(context.Context, string, string) (string, error)
38 }
39
40 type defaultConfig struct {
41 conf map[string]map[string]string
42 }
43
44 var (
45 _ Config = (*defaultConfig)(nil)
46 _ Writer = (*defaultConfig)(nil)
47 )
48
49
50
51 func Default() Config {
52 return &defaultConfig{
53 conf: map[string]map[string]string{
54 "couchdb": {
55 "users_db_suffix": "_users",
56 },
57 },
58 }
59 }
60
61
62 func Map(conf map[string]map[string]string) Config {
63 return &defaultConfig{conf: conf}
64 }
65
66 func (c *defaultConfig) All(context.Context) (map[string]map[string]string, error) {
67 return c.conf, nil
68 }
69
70 func (c *defaultConfig) Section(_ context.Context, section string) (map[string]string, error) {
71 return c.conf[section], nil
72 }
73
74 func (c *defaultConfig) Key(_ context.Context, section, key string) (string, error) {
75 if v, ok := c.conf[section][key]; ok {
76 return v, nil
77 }
78 return "", &internal.Error{Status: http.StatusNotFound, Message: "unknown_config_value"}
79 }
80
81 func (c *defaultConfig) Reload(context.Context) error {
82 return nil
83 }
84
85 func (c *defaultConfig) SetKey(_ context.Context, section, key string, value string) (string, error) {
86 s, ok := c.conf[section]
87 if !ok {
88 s = map[string]string{}
89 c.conf[section] = s
90 }
91 old := s[key]
92 s[key] = value
93 return old, nil
94 }
95
96 func (c *defaultConfig) Delete(_ context.Context, section, key string) (string, error) {
97 if v, ok := c.conf[section][key]; ok {
98 delete(c.conf[section], key)
99 return v, nil
100 }
101 return "", &internal.Error{Status: http.StatusNotFound, Message: "unknown_config_value"}
102 }
103
View as plain text