Skip to content

Commit d8399c4

Browse files
authored
feat: Add support for IntVariation feature flags (#333)
1 parent f64374c commit d8399c4

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

featureflag/featureflag.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ type Client interface {
2121
Variation(key, defaultVal, userID string, attrs ...Attr) string
2222
VariationUser(key string, defaultVal string, user lduser.User) string
2323

24+
Int(key string, defaultVal int, userID string, attrs ...Attr) int
25+
IntUser(key string, defaultVal int, user lduser.User) int
26+
2427
AllEnabledFlags(key string) []string
2528
AllEnabledFlagsUser(key string, user lduser.User) []string
2629
}
@@ -91,6 +94,19 @@ func (c *ldClient) VariationUser(key string, defaultVal string, user lduser.User
9194
return res
9295
}
9396

97+
func (c *ldClient) Int(key string, defaultValue int, userID string, attrs ...Attr) int {
98+
return c.IntUser(key, defaultValue, c.userWithAttrs(userID, attrs))
99+
}
100+
101+
func (c *ldClient) IntUser(key string, defaultVal int, user lduser.User) int {
102+
res, err := c.IntVariation(key, user, defaultVal)
103+
if err != nil {
104+
c.log.WithError(err).WithField("key", key).Error("Failed to load feature flag")
105+
}
106+
// DefaultValue will be returned if IntVariation returns an error
107+
return res
108+
}
109+
94110
func (c *ldClient) AllEnabledFlags(key string) []string {
95111
return c.AllEnabledFlagsUser(key, lduser.NewUser(key))
96112
}

featureflag/featureflag_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestOfflineClient(t *testing.T) {
2323

2424
require.False(t, client.Enabled("notset", "12345"))
2525
require.Equal(t, "foobar", client.Variation("notset", "foobar", "12345"))
26+
require.Equal(t, 3, client.Int("noset", 3, "12345"))
2627
}
2728

2829
func TestMockClient(t *testing.T) {
@@ -35,6 +36,10 @@ func TestMockClient(t *testing.T) {
3536
"FOO": "BAR",
3637
"BLAH": "FOOBAR",
3738
},
39+
IntVars: map[string]int{
40+
"FOO": 4,
41+
"BLAH": 7,
42+
},
3843
}
3944

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

4449
require.Equal(t, "BAR", mock.Variation("FOO", "DFLT", "12345"))
4550
require.Equal(t, "DFLT", mock.Variation("FOOBAR", "DFLT", "12345"))
51+
52+
require.Equal(t, 4, mock.Int("FOO", 2, "12345"))
53+
require.Equal(t, 2, mock.Int("FOOBAR", 2, "12345"))
4654
}
4755

4856
func TestAllEnabledFlags(t *testing.T) {

featureflag/mock.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
type MockClient struct {
88
BoolVars map[string]bool
99
StringVars map[string]string
10+
IntVars map[string]int
1011
}
1112

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

35+
func (c MockClient) Int(key string, defaultVal int, userID string, _ ...Attr) int {
36+
return c.IntUser(key, defaultVal, lduser.NewUser(userID))
37+
}
38+
39+
func (c MockClient) IntUser(key string, defaultVal int, _ lduser.User) int {
40+
res, ok := c.IntVars[key]
41+
if !ok {
42+
return defaultVal
43+
}
44+
return res
45+
}
46+
3447
func (c MockClient) AllEnabledFlags(key string) []string {
3548
return c.AllEnabledFlagsUser(key, lduser.NewUser(key))
3649
}

0 commit comments

Comments
 (0)