Invalidate context.setFlags cache on modification.

This commit is contained in:
Péter Szilágyi 2017-01-11 14:38:54 +02:00
parent 8ef3805c9d
commit acc622e5fb
No known key found for this signature in database
GPG Key ID: E9AE538CEDF8293D
2 changed files with 6 additions and 0 deletions

View File

@ -39,11 +39,13 @@ func (c *Context) NumFlags() int {
// Set sets a context flag to a value.
func (c *Context) Set(name, value string) error {
c.setFlags = nil
return c.flagSet.Set(name, value)
}
// GlobalSet sets a context flag to a value on the global flagset
func (c *Context) GlobalSet(name, value string) error {
globalContext(c).setFlags = nil
return globalContext(c).flagSet.Set(name, value)
}

View File

@ -375,8 +375,10 @@ func TestContext_Set(t *testing.T) {
set.Int("int", 5, "an int")
c := NewContext(nil, set, nil)
expect(t, c.IsSet("int"), false)
c.Set("int", "1")
expect(t, c.Int("int"), 1)
expect(t, c.IsSet("int"), true)
}
func TestContext_GlobalSet(t *testing.T) {
@ -393,7 +395,9 @@ func TestContext_GlobalSet(t *testing.T) {
expect(t, c.Int("int"), 1)
expect(t, c.GlobalInt("int"), 5)
expect(t, c.GlobalIsSet("int"), false)
c.GlobalSet("int", "1")
expect(t, c.Int("int"), 1)
expect(t, c.GlobalInt("int"), 1)
expect(t, c.GlobalIsSet("int"), true)
}