Skip to content

Commit

Permalink
Refactor organisation of code for Flag (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaspoignant authored Jul 21, 2021
1 parent 023290f commit cbcdf1d
Show file tree
Hide file tree
Showing 28 changed files with 557 additions and 549 deletions.
18 changes: 9 additions & 9 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/thomaspoignant/go-feature-flag/internal/flag"
flagv1 "github.com/thomaspoignant/go-feature-flag/internal/flagv1"
"gopkg.in/yaml.v3"
"strings"
"sync"

"github.com/pelletier/go-toml"

"github.com/thomaspoignant/go-feature-flag/internal/model"
)

type Cache interface {
UpdateCache(loadedFlags []byte, fileFormat string) error
Close()
GetFlag(key string) (model.Flag, error)
GetFlag(key string) (flag.Flag, error)
AllFlags() (FlagsCache, error)
}

Expand All @@ -28,7 +28,7 @@ type cacheImpl struct {

func New(notificationService Service) Cache {
return &cacheImpl{
flagsCache: make(map[string]model.FlagData),
flagsCache: make(map[string]flagv1.FlagData),
mutex: sync.RWMutex{},
notificationService: notificationService,
}
Expand Down Expand Up @@ -72,20 +72,20 @@ func (c *cacheImpl) Close() {
}
}

func (c *cacheImpl) GetFlag(key string) (model.Flag, error) {
func (c *cacheImpl) GetFlag(key string) (flag.Flag, error) {
c.mutex.RLock()
defer c.mutex.RUnlock()

if c.flagsCache == nil {
return &model.FlagData{}, errors.New("impossible to read the flag before the initialisation")
return &flagv1.FlagData{}, errors.New("impossible to read the flag before the initialisation")
}

flag, ok := c.flagsCache[key]
f, ok := c.flagsCache[key]
if !ok {
return &model.FlagData{}, fmt.Errorf("flag [%v] does not exists", key)
return &flagv1.FlagData{}, fmt.Errorf("flag [%v] does not exists", key)
}

return &flag, nil
return &f, nil
}

func (c *cacheImpl) AllFlags() (FlagsCache, error) {
Expand Down
37 changes: 12 additions & 25 deletions internal/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package cache_test
import (
"github.com/stretchr/testify/assert"
"github.com/thomaspoignant/go-feature-flag/internal/cache"
flagv1 "github.com/thomaspoignant/go-feature-flag/internal/flagv1"
"github.com/thomaspoignant/go-feature-flag/testutils/testconvert"
"testing"

"github.com/thomaspoignant/go-feature-flag/internal/model"
"github.com/thomaspoignant/go-feature-flag/internal/notifier"
)

Expand Down Expand Up @@ -58,7 +58,7 @@ disable = false`)
tests := []struct {
name string
args args
expected map[string]model.FlagData
expected map[string]flagv1.FlagData
wantErr bool
flagFormat string
}{
Expand All @@ -68,7 +68,7 @@ disable = false`)
args: args{
loadedFlags: yamlFile,
},
expected: map[string]model.FlagData{
expected: map[string]flagv1.FlagData{
"test-flag": {
Disable: nil,
Rule: testconvert.String("key eq \"random-key\""),
Expand Down Expand Up @@ -101,7 +101,7 @@ disable = false`)
loadedFlags: jsonFile,
},
flagFormat: "json",
expected: map[string]model.FlagData{
expected: map[string]flagv1.FlagData{
"test-flag": {
Rule: testconvert.String("key eq \"random-key\""),
Percentage: testconvert.Float64(100),
Expand Down Expand Up @@ -134,13 +134,14 @@ disable = false`)
loadedFlags: tomlFile,
},
flagFormat: "toml",
expected: map[string]model.FlagData{
expected: map[string]flagv1.FlagData{
"test-flag": {
Rule: testconvert.String("key eq \"random-key\""),
Percentage: testconvert.Float64(100),
True: testconvert.Interface(true),
False: testconvert.Interface(false),
Default: testconvert.Interface(false),
Disable: testconvert.Bool(false),
},
},
wantErr: false,
Expand Down Expand Up @@ -173,14 +174,7 @@ disable = false`),
// If no error we compare with expected
for key, expected := range tt.expected {
got, _ := fCache.GetFlag(key)
assert.Equal(t, expected.GetRule(), got.GetRule())
assert.Equal(t, expected.GetPercentage(), got.GetPercentage())
assert.Equal(t, expected.GetTrue(), got.GetTrue())
assert.Equal(t, expected.GetFalse(), got.GetFalse())
assert.Equal(t, expected.GetDefault(), got.GetDefault())
assert.Equal(t, expected.GetTrackEvents(), got.GetTrackEvents())
assert.Equal(t, expected.GetDisable(), got.GetDisable())
assert.Equal(t, expected.GetRollout(), got.GetRollout())
assert.Equal(t, &expected, got) // nolint
}
fCache.Close()
})
Expand All @@ -203,7 +197,7 @@ func Test_AllFlags(t *testing.T) {
tests := []struct {
name string
args args
expected map[string]model.FlagData
expected map[string]flagv1.FlagData
wantErr bool
flagFormat string
}{
Expand All @@ -213,7 +207,7 @@ func Test_AllFlags(t *testing.T) {
args: args{
loadedFlags: yamlFile,
},
expected: map[string]model.FlagData{
expected: map[string]flagv1.FlagData{
"test-flag": {
Disable: nil,
Rule: testconvert.String("key eq \"random-key\""),
Expand Down Expand Up @@ -246,7 +240,7 @@ test-flag2:
trackEvents: false
`),
},
expected: map[string]model.FlagData{
expected: map[string]flagv1.FlagData{
"test-flag": {
Disable: nil,
Rule: testconvert.String("key eq \"random-key\""),
Expand All @@ -273,7 +267,7 @@ test-flag2:
args: args{
loadedFlags: []byte(``),
},
expected: map[string]model.FlagData{},
expected: map[string]flagv1.FlagData{},
wantErr: true,
},
}
Expand All @@ -293,14 +287,7 @@ test-flag2:
// If no error we compare with expected
for key, expected := range tt.expected {
got := allFlags[key]
assert.Equal(t, expected.GetRule(), got.GetRule())
assert.Equal(t, expected.GetPercentage(), got.GetPercentage())
assert.Equal(t, expected.GetTrue(), got.GetTrue())
assert.Equal(t, expected.GetFalse(), got.GetFalse())
assert.Equal(t, expected.GetDefault(), got.GetDefault())
assert.Equal(t, expected.GetTrackEvents(), got.GetTrackEvents())
assert.Equal(t, expected.GetDisable(), got.GetDisable())
assert.Equal(t, expected.GetRollout(), got.GetRollout())
assert.Equal(t, expected, got)
}
fCache.Close()
})
Expand Down
4 changes: 2 additions & 2 deletions internal/cache/flag_cache.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cache

import model "github.com/thomaspoignant/go-feature-flag/internal/model"
import flagv1 "github.com/thomaspoignant/go-feature-flag/internal/flagv1"

type FlagsCache map[string]model.FlagData
type FlagsCache map[string]flagv1.FlagData

func (fc FlagsCache) Copy() FlagsCache {
copyCache := make(FlagsCache)
Expand Down
6 changes: 3 additions & 3 deletions internal/cache/flag_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package cache_test

import (
"github.com/stretchr/testify/assert"
"github.com/thomaspoignant/go-feature-flag/internal/model"
"github.com/thomaspoignant/go-feature-flag/testutils/testconvert"
"github.com/thomaspoignant/go-feature-flag/internal/cache"
flagv1 "github.com/thomaspoignant/go-feature-flag/internal/flagv1"
"github.com/thomaspoignant/go-feature-flag/testutils/testconvert"
"testing"
)

Expand All @@ -16,7 +16,7 @@ func TestFlagsCache_Copy(t *testing.T) {
{
name: "Copy with values",
fc: cache.FlagsCache{
"test": model.FlagData{
"test": flagv1.FlagData{
Disable: testconvert.Bool(false),
Rule: testconvert.String("key eq \"toto\""),
Percentage: testconvert.Float64(20),
Expand Down
9 changes: 5 additions & 4 deletions internal/cache/notification_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cache

import (
"github.com/google/go-cmp/cmp"
"github.com/thomaspoignant/go-feature-flag/internal/flag"
"sync"

"github.com/thomaspoignant/go-feature-flag/internal/model"
Expand Down Expand Up @@ -43,8 +44,8 @@ func (c *notificationService) Close() {
func (c *notificationService) getDifferences(
oldCache FlagsCache, newCache FlagsCache) model.DiffCache {
diff := model.DiffCache{
Deleted: map[string]model.Flag{},
Added: map[string]model.Flag{},
Deleted: map[string]flag.Flag{},
Added: map[string]flag.Flag{},
Updated: map[string]model.DiffUpdated{},
}
for key := range oldCache {
Expand All @@ -65,8 +66,8 @@ func (c *notificationService) getDifferences(

for key := range newCache {
if _, inOldCache := oldCache[key]; !inOldCache {
flag := newCache[key]
diff.Added[key] = &flag
f := newCache[key]
diff.Added[key] = &f
}
}
return diff
Expand Down
38 changes: 20 additions & 18 deletions internal/cache/notification_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cache

import (
"github.com/stretchr/testify/assert"
"github.com/thomaspoignant/go-feature-flag/internal/flag"
flagv1 "github.com/thomaspoignant/go-feature-flag/internal/flagv1"
"github.com/thomaspoignant/go-feature-flag/testutils/testconvert"
"sync"
"testing"
Expand All @@ -28,21 +30,21 @@ func Test_notificationService_getDifferences(t *testing.T) {
name: "Delete flag",
args: args{
oldCache: FlagsCache{
"test-flag": model.FlagData{
"test-flag": flagv1.FlagData{
Percentage: testconvert.Float64(100),
True: testconvert.Interface(true),
False: testconvert.Interface(false),
Default: testconvert.Interface(false),
},
"test-flag2": model.FlagData{
"test-flag2": flagv1.FlagData{
Percentage: testconvert.Float64(100),
True: testconvert.Interface(true),
False: testconvert.Interface(false),
Default: testconvert.Interface(false),
},
},
newCache: FlagsCache{
"test-flag": model.FlagData{
"test-flag": flagv1.FlagData{
Percentage: testconvert.Float64(100),
True: testconvert.Interface(true),
False: testconvert.Interface(false),
Expand All @@ -51,37 +53,37 @@ func Test_notificationService_getDifferences(t *testing.T) {
},
},
want: model.DiffCache{
Deleted: map[string]model.Flag{
"test-flag2": &model.FlagData{
Deleted: map[string]flag.Flag{
"test-flag2": &flagv1.FlagData{
Percentage: testconvert.Float64(100),
True: testconvert.Interface(true),
False: testconvert.Interface(false),
Default: testconvert.Interface(false),
},
},
Added: map[string]model.Flag{},
Added: map[string]flag.Flag{},
Updated: map[string]model.DiffUpdated{},
},
},
{
name: "Added flag",
args: args{
oldCache: FlagsCache{
"test-flag": model.FlagData{
"test-flag": flagv1.FlagData{
Percentage: testconvert.Float64(100),
True: testconvert.Interface(true),
False: testconvert.Interface(false),
Default: testconvert.Interface(false),
},
},
newCache: FlagsCache{
"test-flag": model.FlagData{
"test-flag": flagv1.FlagData{
Percentage: testconvert.Float64(100),
True: testconvert.Interface(true),
False: testconvert.Interface(false),
Default: testconvert.Interface(false),
},
"test-flag2": model.FlagData{
"test-flag2": flagv1.FlagData{
Percentage: testconvert.Float64(100),
True: testconvert.Interface(true),
False: testconvert.Interface(false),
Expand All @@ -90,31 +92,31 @@ func Test_notificationService_getDifferences(t *testing.T) {
},
},
want: model.DiffCache{
Added: map[string]model.Flag{
"test-flag2": &model.FlagData{
Added: map[string]flag.Flag{
"test-flag2": &flagv1.FlagData{
Percentage: testconvert.Float64(100),
True: testconvert.Interface(true),
False: testconvert.Interface(false),
Default: testconvert.Interface(false),
},
},
Deleted: map[string]model.Flag{},
Deleted: map[string]flag.Flag{},
Updated: map[string]model.DiffUpdated{},
},
},
{
name: "Updated flag",
args: args{
oldCache: FlagsCache{
"test-flag": model.FlagData{
"test-flag": flagv1.FlagData{
Percentage: testconvert.Float64(100),
True: testconvert.Interface(true),
False: testconvert.Interface(false),
Default: testconvert.Interface(false),
},
},
newCache: FlagsCache{
"test-flag": model.FlagData{
"test-flag": flagv1.FlagData{
Percentage: testconvert.Float64(100),
True: testconvert.Interface(true),
False: testconvert.Interface(false),
Expand All @@ -123,17 +125,17 @@ func Test_notificationService_getDifferences(t *testing.T) {
},
},
want: model.DiffCache{
Added: map[string]model.Flag{},
Deleted: map[string]model.Flag{},
Added: map[string]flag.Flag{},
Deleted: map[string]flag.Flag{},
Updated: map[string]model.DiffUpdated{
"test-flag": {
Before: &model.FlagData{
Before: &flagv1.FlagData{
Percentage: testconvert.Float64(100),
True: testconvert.Interface(true),
False: testconvert.Interface(false),
Default: testconvert.Interface(false),
},
After: &model.FlagData{
After: &flagv1.FlagData{
Percentage: testconvert.Float64(100),
True: testconvert.Interface(true),
False: testconvert.Interface(false),
Expand Down
Loading

0 comments on commit cbcdf1d

Please sign in to comment.