Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions featureflag/featureflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type Client interface {
Variation(key, defaultVal, userID string, attrs ...Attr) string
VariationUser(key string, defaultVal string, user lduser.User) string

Int(key string, defaultVal int, userID string, attrs ...Attr) int
IntUser(key string, defaultVal int, user lduser.User) int

AllEnabledFlags(key string) []string
AllEnabledFlagsUser(key string, user lduser.User) []string
}
Expand Down Expand Up @@ -91,6 +94,19 @@ func (c *ldClient) VariationUser(key string, defaultVal string, user lduser.User
return res
}

func (c *ldClient) Int(key string, defaultValue int, userID string, attrs ...Attr) int {
return c.IntUser(key, defaultValue, c.userWithAttrs(userID, attrs))
}

func (c *ldClient) IntUser(key string, defaultVal int, user lduser.User) int {
res, err := c.IntVariation(key, user, defaultVal)
if err != nil {
c.log.WithError(err).WithField("key", key).Error("Failed to load feature flag")
}
// DefaultValue will be returned if IntVariation returns an error
return res
}

func (c *ldClient) AllEnabledFlags(key string) []string {
return c.AllEnabledFlagsUser(key, lduser.NewUser(key))
}
Expand Down
8 changes: 8 additions & 0 deletions featureflag/featureflag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestOfflineClient(t *testing.T) {

require.False(t, client.Enabled("notset", "12345"))
require.Equal(t, "foobar", client.Variation("notset", "foobar", "12345"))
require.Equal(t, 3, client.Int("noset", 3, "12345"))
}

func TestMockClient(t *testing.T) {
Expand All @@ -35,6 +36,10 @@ func TestMockClient(t *testing.T) {
"FOO": "BAR",
"BLAH": "FOOBAR",
},
IntVars: map[string]int{
"FOO": 4,
"BLAH": 7,
},
}

require.True(t, mock.Enabled("FOO", "12345"))
Expand All @@ -43,6 +48,9 @@ func TestMockClient(t *testing.T) {

require.Equal(t, "BAR", mock.Variation("FOO", "DFLT", "12345"))
require.Equal(t, "DFLT", mock.Variation("FOOBAR", "DFLT", "12345"))

require.Equal(t, 4, mock.Int("FOO", 2, "12345"))
require.Equal(t, 2, mock.Int("FOOBAR", 2, "12345"))
}

func TestAllEnabledFlags(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions featureflag/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
type MockClient struct {
BoolVars map[string]bool
StringVars map[string]string
IntVars map[string]int
}

var _ Client = MockClient{}
Expand All @@ -31,6 +32,18 @@ func (c MockClient) VariationUser(key string, defaultVal string, _ lduser.User)
return res
}

func (c MockClient) Int(key string, defaultVal int, userID string, _ ...Attr) int {
return c.IntUser(key, defaultVal, lduser.NewUser(userID))
}

func (c MockClient) IntUser(key string, defaultVal int, _ lduser.User) int {
res, ok := c.IntVars[key]
if !ok {
return defaultVal
}
return res
}

func (c MockClient) AllEnabledFlags(key string) []string {
return c.AllEnabledFlagsUser(key, lduser.NewUser(key))
}
Expand Down