Skip to content

Commit

Permalink
Update SetConfigValue to return old value
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Jun 17, 2019
1 parent e82ff56 commit e7e7d8a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
6 changes: 3 additions & 3 deletions config.go
Expand Up @@ -59,14 +59,14 @@ func (c *Client) ConfigValue(ctx context.Context, node, section, key string) (st
}

// SetConfigValue sets the server's config value on the specified node, creating
// the key if it doesn't exist.
// the key if it doesn't exist. It returns the old value.
//
// See http://docs.couchdb.org/en/stable/api/server/configuration.html#put--_node-node-name-_config-section-key
func (c *Client) SetConfigValue(ctx context.Context, node, section, key, value string) error {
func (c *Client) SetConfigValue(ctx context.Context, node, section, key, value string) (string, error) {
if configer, ok := c.driverClient.(driver.Configer); ok {
return configer.SetConfigValue(ctx, node, section, key, value)
}
return configNotImplemented
return "", configNotImplemented
}

// DeleteConfigKey deletes the configuration key and associated value from the
Expand Down
31 changes: 18 additions & 13 deletions config_test.go
Expand Up @@ -167,6 +167,7 @@ func TestSetConfigValue(t *testing.T) {
type tst struct {
client *Client
node, section, key, value string
expected string
status int
err string
}
Expand All @@ -178,40 +179,44 @@ func TestSetConfigValue(t *testing.T) {
})
tests.Add("error", tst{
client: &Client{driverClient: &mock.Configer{
SetConfigValueFunc: func(_ context.Context, _, _, _, _ string) error {
return errors.New("conf error")
SetConfigValueFunc: func(_ context.Context, _, _, _, _ string) (string, error) {
return "", errors.New("conf error")
},
}},
status: http.StatusInternalServerError,
err: "conf error",
})
tests.Add("success", tst{
client: &Client{driverClient: &mock.Configer{
SetConfigValueFunc: func(_ context.Context, node, section, key, value string) error {
SetConfigValueFunc: func(_ context.Context, node, section, key, value string) (string, error) {
if node != "foo" {
return errors.Errorf("Unexpected node: %s", node)
return "", errors.Errorf("Unexpected node: %s", node)
}
if section != "foo" {
return errors.Errorf("Unexpected section: %s", section)
return "", errors.Errorf("Unexpected section: %s", section)
}
if key != "bar" {
return errors.Errorf("Unexpected key: %s", key)
return "", errors.Errorf("Unexpected key: %s", key)
}
if value != "baz" {
return errors.Errorf("Unexpected value: %s", value)
return "", errors.Errorf("Unexpected value: %s", value)
}
return nil
return "old", nil
},
}},
node: "foo",
section: "foo",
key: "bar",
value: "baz",
node: "foo",
section: "foo",
key: "bar",
value: "baz",
expected: "old",
})

tests.Run(t, func(t *testing.T, test tst) {
err := test.client.SetConfigValue(context.Background(), test.node, test.section, test.key, test.value)
result, err := test.client.SetConfigValue(context.Background(), test.node, test.section, test.key, test.value)
testy.StatusError(t, test.err, test.status, err)
if d := diff.Interface(test.expected, result); d != nil {
t.Error(d)
}
})
}

Expand Down
2 changes: 1 addition & 1 deletion driver/config.go
Expand Up @@ -12,6 +12,6 @@ type Configer interface {
Config(ctx context.Context, node string) (Config, error)
ConfigSection(ctx context.Context, node, section string) (ConfigSection, error)
ConfigValue(ctx context.Context, node, section, key string) (string, error)
SetConfigValue(ctx context.Context, node, section, key, value string) error
SetConfigValue(ctx context.Context, node, section, key, value string) (string, error)
DeleteConfigKey(ctx context.Context, node, section, key string) error
}
4 changes: 2 additions & 2 deletions mock/client.go
Expand Up @@ -158,7 +158,7 @@ type Configer struct {
ConfigFunc func(context.Context, string) (driver.Config, error)
ConfigSectionFunc func(context.Context, string, string) (driver.ConfigSection, error)
ConfigValueFunc func(context.Context, string, string, string) (string, error)
SetConfigValueFunc func(context.Context, string, string, string, string) error
SetConfigValueFunc func(context.Context, string, string, string, string) (string, error)
DeleteConfigKeyFunc func(context.Context, string, string, string) error
}

Expand All @@ -180,7 +180,7 @@ func (c *Configer) ConfigValue(ctx context.Context, node, section, key string) (
}

// SetConfigValue calls c.SetConfigValueFunc
func (c *Configer) SetConfigValue(ctx context.Context, node, section, key, value string) error {
func (c *Configer) SetConfigValue(ctx context.Context, node, section, key, value string) (string, error) {
return c.SetConfigValueFunc(ctx, node, section, key, value)
}

Expand Down

0 comments on commit e7e7d8a

Please sign in to comment.