Skip to content

Commit

Permalink
Add default value for FeatureEnabledForID (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Burnett authored Aug 15, 2017
1 parent 7e31e94 commit 320bd90
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
4 changes: 3 additions & 1 deletion snapshot/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ type IFace interface {
// the 0-100 value for the given feature. Use this method for "sticky" features
// @param key supplies the feature key to lookup.
// @param id supplies the ID to use in the CRC check.
FeatureEnabledForID(key string, id uint64) bool
// @param defaultValue supplies the default value that will be used if either the feature key
// does not exist or it is not a bool.
FeatureEnabledForID(key string, id uint64, defaultValue bool) bool

// Fetch raw runtime data based on key.
// @param key supplies the key to fetch.
Expand Down
4 changes: 4 additions & 0 deletions snapshot/nil.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func (n *Nil) FeatureEnabled(key string, defaultValue uint64) bool {
return defaultRandomGenerator.Random()%100 < min(defaultValue, 100)
}

func (n *Nil) FeatureEnabledForID(key string, id uint64, defaultValue bool) bool {
return defaultValue
}

func (n *Nil) Get(key string) string { return "" }

func (n *Nil) GetInteger(key string, defaultValue uint64) uint64 {
Expand Down
6 changes: 2 additions & 4 deletions snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,14 @@ func (s *Snapshot) FeatureEnabled(key string, defaultValue uint64) bool {

// FeatureEnabledForID checks that the crc32 of the id and key's byte value falls within the mod of
// the 0-100 value for the given feature. Use this method for "sticky" features
func (s *Snapshot) FeatureEnabledForID(key string, id uint64) bool {
func (s *Snapshot) FeatureEnabledForID(key string, id uint64, defaultValue bool) bool {
if e, ok := s.Entries()[key]; ok {
if e.Uint64Valid {
return withinPercentile(id, uint32(e.Uint64Value), key)
}

return false
}

return false
return defaultValue
}

func (s *Snapshot) Get(key string) string {
Expand Down
6 changes: 3 additions & 3 deletions snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ func TestSnapshot_FeatureEnabledForID(t *testing.T) {
key := "test"
ss := NewMock()
ss.SetUInt64(key, 100)
assert.True(t, ss.FeatureEnabledForID(key, 1))
assert.True(t, ss.FeatureEnabledForID(key, 1, true))

ss.SetUInt64(key, 0)
assert.False(t, ss.FeatureEnabledForID(key, 1))
assert.False(t, ss.FeatureEnabledForID(key, 1, true))

enabled := 0
for i := 1; i < 101; i++ {
ss.SetUInt64(key, uint64(i))
if ss.FeatureEnabledForID(key, uint64(i)) {
if ss.FeatureEnabledForID(key, uint64(i), true) {
enabled++
}
}
Expand Down

0 comments on commit 320bd90

Please sign in to comment.