-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package automod | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type FlagStore interface { | ||
Get(ctx context.Context, key string) ([]string, error) | ||
Add(ctx context.Context, key string, flags []string) error | ||
Remove(ctx context.Context, key string, flags []string) error | ||
} | ||
|
||
type MemFlagStore struct { | ||
Data map[string][]string | ||
} | ||
|
||
func NewMemFlagStore() MemFlagStore { | ||
return MemFlagStore{ | ||
Data: make(map[string][]string), | ||
} | ||
} | ||
|
||
func (s MemFlagStore) Get(ctx context.Context, key string) ([]string, error) { | ||
v, ok := s.Data[key] | ||
if !ok { | ||
return []string{}, nil | ||
} | ||
return v, nil | ||
} | ||
|
||
func (s MemFlagStore) Add(ctx context.Context, key string, flags []string) error { | ||
v, ok := s.Data[key] | ||
if !ok { | ||
v = []string{} | ||
} | ||
for _, f := range flags { | ||
v = append(v, f) | ||
} | ||
v = dedupeStrings(v) | ||
s.Data[key] = v | ||
return nil | ||
} | ||
|
||
// does not error if flags not in set | ||
func (s MemFlagStore) Remove(ctx context.Context, key string, flags []string) error { | ||
if len(flags) == 0 { | ||
return nil | ||
} | ||
v, ok := s.Data[key] | ||
if !ok { | ||
v = []string{} | ||
} | ||
m := make(map[string]bool, len(v)) | ||
for _, f := range v { | ||
m[f] = true | ||
} | ||
for _, f := range flags { | ||
delete(m, f) | ||
} | ||
out := []string{} | ||
for f, _ := range m { | ||
out = append(out, f) | ||
} | ||
s.Data[key] = out | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package automod | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFlagStoreBasics(t *testing.T) { | ||
assert := assert.New(t) | ||
ctx := context.Background() | ||
|
||
fs := NewMemFlagStore() | ||
|
||
l, err := fs.Get(ctx, "test1") | ||
assert.NoError(err) | ||
assert.Empty(l) | ||
|
||
assert.NoError(fs.Add(ctx, "test1", []string{"red", "green"})) | ||
assert.NoError(fs.Add(ctx, "test1", []string{"red", "blue"})) | ||
l, err = fs.Get(ctx, "test1") | ||
assert.NoError(err) | ||
assert.Equal(3, len(l)) | ||
|
||
assert.NoError(fs.Remove(ctx, "test1", []string{"red", "blue"})) | ||
l, err = fs.Get(ctx, "test1") | ||
assert.NoError(err) | ||
assert.Equal([]string{"green"}, l) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package automod | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
var redisFlagsPrefix string = "flags/" | ||
|
||
type RedisFlagStore struct { | ||
Client *redis.Client | ||
} | ||
|
||
func NewRedisFlagStore(redisURL string) (*RedisFlagStore, error) { | ||
opt, err := redis.ParseURL(redisURL) | ||
if err != nil { | ||
return nil, err | ||
} | ||
rdb := redis.NewClient(opt) | ||
// check redis connection | ||
_, err = rdb.Ping(context.TODO()).Result() | ||
if err != nil { | ||
return nil, err | ||
} | ||
rcs := RedisFlagStore{ | ||
Client: rdb, | ||
} | ||
return &rcs, nil | ||
} | ||
|
||
func (s *RedisFlagStore) Get(ctx context.Context, key string) ([]string, error) { | ||
rkey := redisFlagsPrefix + key | ||
l, err := s.Client.SMembers(ctx, rkey).Result() | ||
if err == redis.Nil { | ||
return []string{}, nil | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
return l, nil | ||
} | ||
|
||
func (s *RedisFlagStore) Add(ctx context.Context, key string, flags []string) error { | ||
if len(flags) == 0 { | ||
return nil | ||
} | ||
l := []interface{}{} | ||
for _, v := range flags { | ||
l = append(l, v) | ||
} | ||
rkey := redisFlagsPrefix + key | ||
return s.Client.SAdd(ctx, rkey, l...).Err() | ||
} | ||
|
||
func (s *RedisFlagStore) Remove(ctx context.Context, key string, flags []string) error { | ||
if len(flags) == 0 { | ||
return nil | ||
} | ||
l := []interface{}{} | ||
for _, v := range flags { | ||
l = append(l, v) | ||
} | ||
rkey := redisFlagsPrefix + key | ||
return s.Client.SRem(ctx, rkey, l...).Err() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package automod | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRedisFlagStoreBasics(t *testing.T) { | ||
t.Skip("live test, need redis running locally") | ||
assert := assert.New(t) | ||
ctx := context.Background() | ||
|
||
fs, err := NewRedisFlagStore("redis://localhost:6379/0") | ||
if err != nil { | ||
t.Fail() | ||
} | ||
|
||
l, err := fs.Get(ctx, "test1") | ||
assert.NoError(err) | ||
assert.Empty(l) | ||
|
||
assert.NoError(fs.Add(ctx, "test1", []string{"red", "green"})) | ||
assert.NoError(fs.Add(ctx, "test1", []string{"red", "blue"})) | ||
l, err = fs.Get(ctx, "test1") | ||
assert.NoError(err) | ||
assert.Equal(3, len(l)) | ||
|
||
assert.NoError(fs.Remove(ctx, "test1", []string{"red", "blue", "orange"})) | ||
l, err = fs.Get(ctx, "test1") | ||
assert.NoError(err) | ||
assert.Equal([]string{"green"}, l) | ||
assert.NoError(fs.Remove(ctx, "test1", []string{"green"})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters