diff --git a/snapshot/iface.go b/snapshot/iface.go index d5bf523..5ce982c 100644 --- a/snapshot/iface.go +++ b/snapshot/iface.go @@ -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. diff --git a/snapshot/nil.go b/snapshot/nil.go index 90d2d8c..a4af12e 100644 --- a/snapshot/nil.go +++ b/snapshot/nil.go @@ -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 { diff --git a/snapshot/snapshot.go b/snapshot/snapshot.go index 39e9dd8..20ce451 100644 --- a/snapshot/snapshot.go +++ b/snapshot/snapshot.go @@ -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 { diff --git a/snapshot/snapshot_test.go b/snapshot/snapshot_test.go index cabe2e9..6bd30e6 100644 --- a/snapshot/snapshot_test.go +++ b/snapshot/snapshot_test.go @@ -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++ } }