Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Notifier and DiffCache public #227

Merged
merged 2 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ffclient
import (
"context"
"errors"
"github.com/thomaspoignant/go-feature-flag/ffnotifier"
"log"
"time"

Expand Down Expand Up @@ -73,7 +74,7 @@ func (c *Config) GetRetriever() (Retriever, error) {
// // ...
// }
type NotifierConfig interface {
GetNotifier(config Config) (notifier.Notifier, error)
GetNotifier(config Config) (ffnotifier.Notifier, error)
}

// WebhookConfig is the configuration of your webhook.
Expand Down Expand Up @@ -133,7 +134,7 @@ type WebhookConfig struct {
}

// GetNotifier convert the configuration in a Notifier struct
func (w *WebhookConfig) GetNotifier(config Config) (notifier.Notifier, error) {
func (w *WebhookConfig) GetNotifier(config Config) (ffnotifier.Notifier, error) {
url := w.EndpointURL

// remove this if when EndpointURL will be removed
Expand All @@ -153,7 +154,7 @@ type SlackNotifier struct {
}

// GetNotifier convert the configuration in a Notifier struct
func (w *SlackNotifier) GetNotifier(config Config) (notifier.Notifier, error) {
func (w *SlackNotifier) GetNotifier(config Config) (ffnotifier.Notifier, error) {
notifier := notifier.NewSlackNotifier(config.Logger, internal.DefaultHTTPClient(), w.SlackWebhookURL)
return &notifier, nil
}
27 changes: 27 additions & 0 deletions docs/notifier/custom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Custom Notifier
To create a custom notifier you must have a `struct` that implements the [`ffnotifier.notifier`](https://pkg.go.dev/github.com/thomaspoignant/go-feature-flag/ffnotifier/notifier) interface.

```go linenums="1"
import (
"fmt"
ffclient "github.com/thomaspoignant/go-feature-flag"
"github.com/thomaspoignant/go-feature-flag/ffnotifier"
"sync"
)

// Your config object to create a new Notifier
type CustomNotifierConfig struct{}

// GetNotifier returns a notfier that implement ffnotifier.Notifier
func (c *CustomNotifier) GetNotifier(config ffclient.Config) (ffnotifier.Notifier, error) {
return &CustomNotifier{}, nil
}

type CustomNotifier struct{}
func (w *CustomNotifier) Notify(cache ffnotifier.DiffCache, waitGroup *sync.WaitGroup) {
defer waitGroup.Done() // don't forget this line, if you don't have it you can break your notifications

// ...
// do whatever you want here
}
```
2 changes: 1 addition & 1 deletion internal/model/diff_cache.go → ffnotifier/diff_cache.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package model
package ffnotifier

import (
"github.com/thomaspoignant/go-feature-flag/internal/flag"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package model_test
package ffnotifier_test

import (
"github.com/stretchr/testify/assert"
"github.com/thomaspoignant/go-feature-flag/ffnotifier"
"github.com/thomaspoignant/go-feature-flag/internal/flag"
flagv1 "github.com/thomaspoignant/go-feature-flag/internal/flagv1"
"github.com/thomaspoignant/go-feature-flag/internal/model"
"github.com/thomaspoignant/go-feature-flag/testutils/testconvert"
"testing"
)
Expand All @@ -13,7 +13,7 @@ func TestDiffCache_HasDiff(t *testing.T) {
type fields struct {
Deleted map[string]flag.Flag
Added map[string]flag.Flag
Updated map[string]model.DiffUpdated
Updated map[string]ffnotifier.DiffUpdated
}
tests := []struct {
name string
Expand All @@ -30,7 +30,7 @@ func TestDiffCache_HasDiff(t *testing.T) {
fields: fields{
Deleted: map[string]flag.Flag{},
Added: map[string]flag.Flag{},
Updated: map[string]model.DiffUpdated{},
Updated: map[string]ffnotifier.DiffUpdated{},
},
want: false,
},
Expand All @@ -46,7 +46,7 @@ func TestDiffCache_HasDiff(t *testing.T) {
},
},
Added: map[string]flag.Flag{},
Updated: map[string]model.DiffUpdated{},
Updated: map[string]ffnotifier.DiffUpdated{},
},
want: true,
},
Expand All @@ -62,7 +62,7 @@ func TestDiffCache_HasDiff(t *testing.T) {
},
},
Deleted: map[string]flag.Flag{},
Updated: map[string]model.DiffUpdated{},
Updated: map[string]ffnotifier.DiffUpdated{},
},
want: true,
},
Expand All @@ -71,7 +71,7 @@ func TestDiffCache_HasDiff(t *testing.T) {
fields: fields{
Added: map[string]flag.Flag{},
Deleted: map[string]flag.Flag{},
Updated: map[string]model.DiffUpdated{
Updated: map[string]ffnotifier.DiffUpdated{
"flag": {
Before: &flagv1.FlagData{
Percentage: testconvert.Float64(100),
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestDiffCache_HasDiff(t *testing.T) {
Default: testconvert.Interface(true),
},
},
Updated: map[string]model.DiffUpdated{
Updated: map[string]ffnotifier.DiffUpdated{
"flag": {
Before: &flagv1.FlagData{
Percentage: testconvert.Float64(100),
Expand All @@ -131,7 +131,7 @@ func TestDiffCache_HasDiff(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := model.DiffCache{
d := ffnotifier.DiffCache{
Deleted: tt.fields.Deleted,
Added: tt.fields.Added,
Updated: tt.fields.Updated,
Expand Down
9 changes: 9 additions & 0 deletions ffnotifier/notifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ffnotifier

import (
"sync"
)

type Notifier interface {
Notify(cache DiffCache, waitGroup *sync.WaitGroup)
}
7 changes: 3 additions & 4 deletions internal/cache/cache_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package cache_test

import (
"github.com/stretchr/testify/assert"
"github.com/thomaspoignant/go-feature-flag/ffnotifier"
"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/notifier"
)

func Test_FlagCacheNotInit(t *testing.T) {
Expand Down Expand Up @@ -164,7 +163,7 @@ disable = false`),

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fCache := cache.New(cache.NewNotificationService([]notifier.Notifier{}))
fCache := cache.New(cache.NewNotificationService([]ffnotifier.Notifier{}))
err := fCache.UpdateCache(tt.args.loadedFlags, tt.flagFormat)
if tt.wantErr {
assert.Error(t, err, "UpdateCache() error = %v, wantErr %v", err, tt.wantErr)
Expand Down Expand Up @@ -274,7 +273,7 @@ test-flag2:

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fCache := cache.New(cache.NewNotificationService([]notifier.Notifier{}))
fCache := cache.New(cache.NewNotificationService([]ffnotifier.Notifier{}))
_ = fCache.UpdateCache(tt.args.loadedFlags, tt.flagFormat)

allFlags, err := fCache.AllFlags()
Expand Down
16 changes: 7 additions & 9 deletions internal/cache/notification_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,25 @@ package cache

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

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

type Service interface {
Close()
Notify(oldCache map[string]flag.Flag, newCache map[string]flag.Flag)
}

func NewNotificationService(notifiers []notifier.Notifier) Service {
func NewNotificationService(notifiers []ffnotifier.Notifier) Service {
return &notificationService{
Notifiers: notifiers,
waitGroup: &sync.WaitGroup{},
}
}

type notificationService struct {
Notifiers []notifier.Notifier
Notifiers []ffnotifier.Notifier
waitGroup *sync.WaitGroup
}

Expand All @@ -42,11 +40,11 @@ func (c *notificationService) Close() {

// getDifferences is checking what are the difference in the updated cache.
func (c *notificationService) getDifferences(
oldCache map[string]flag.Flag, newCache map[string]flag.Flag) model.DiffCache {
diff := model.DiffCache{
oldCache map[string]flag.Flag, newCache map[string]flag.Flag) ffnotifier.DiffCache {
diff := ffnotifier.DiffCache{
Deleted: map[string]flag.Flag{},
Added: map[string]flag.Flag{},
Updated: map[string]model.DiffUpdated{},
Updated: map[string]ffnotifier.DiffUpdated{},
}
for key := range oldCache {
newFlag, inNewCache := newCache[key]
Expand All @@ -57,7 +55,7 @@ func (c *notificationService) getDifferences(
}

if !cmp.Equal(oldCache[key], newCache[key]) {
diff.Updated[key] = model.DiffUpdated{
diff.Updated[key] = ffnotifier.DiffUpdated{
Before: oldFlag,
After: newFlag,
}
Expand Down
20 changes: 9 additions & 11 deletions internal/cache/notification_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ package cache

import (
"github.com/stretchr/testify/assert"
"github.com/thomaspoignant/go-feature-flag/ffnotifier"
"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"

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

func Test_notificationService_getDifferences(t *testing.T) {
type fields struct {
Notifiers []notifier.Notifier
Notifiers []ffnotifier.Notifier
}
type args struct {
oldCache map[string]flag.Flag
Expand All @@ -24,7 +22,7 @@ func Test_notificationService_getDifferences(t *testing.T) {
name string
fields fields
args args
want model.DiffCache
want ffnotifier.DiffCache
}{
{
name: "Delete flag",
Expand Down Expand Up @@ -52,7 +50,7 @@ func Test_notificationService_getDifferences(t *testing.T) {
},
},
},
want: model.DiffCache{
want: ffnotifier.DiffCache{
Deleted: map[string]flag.Flag{
"test-flag2": &flagv1.FlagData{
Percentage: testconvert.Float64(100),
Expand All @@ -62,7 +60,7 @@ func Test_notificationService_getDifferences(t *testing.T) {
},
},
Added: map[string]flag.Flag{},
Updated: map[string]model.DiffUpdated{},
Updated: map[string]ffnotifier.DiffUpdated{},
},
},
{
Expand Down Expand Up @@ -91,7 +89,7 @@ func Test_notificationService_getDifferences(t *testing.T) {
},
},
},
want: model.DiffCache{
want: ffnotifier.DiffCache{
Added: map[string]flag.Flag{
"test-flag2": &flagv1.FlagData{
Percentage: testconvert.Float64(100),
Expand All @@ -101,7 +99,7 @@ func Test_notificationService_getDifferences(t *testing.T) {
},
},
Deleted: map[string]flag.Flag{},
Updated: map[string]model.DiffUpdated{},
Updated: map[string]ffnotifier.DiffUpdated{},
},
},
{
Expand All @@ -124,10 +122,10 @@ func Test_notificationService_getDifferences(t *testing.T) {
},
},
},
want: model.DiffCache{
want: ffnotifier.DiffCache{
Added: map[string]flag.Flag{},
Deleted: map[string]flag.Flag{},
Updated: map[string]model.DiffUpdated{
Updated: map[string]ffnotifier.DiffUpdated{
"test-flag": {
Before: &flagv1.FlagData{
Percentage: testconvert.Float64(100),
Expand Down
11 changes: 0 additions & 11 deletions internal/notifier/notifier.go

This file was deleted.

4 changes: 2 additions & 2 deletions internal/notifier/notifier_log.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package notifier

import (
"github.com/thomaspoignant/go-feature-flag/ffnotifier"
"log"
"sync"

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

type LogNotifier struct {
Logger *log.Logger
}

func (c *LogNotifier) Notify(diff model.DiffCache, wg *sync.WaitGroup) {
func (c *LogNotifier) Notify(diff ffnotifier.DiffCache, wg *sync.WaitGroup) {
defer wg.Done()
for key := range diff.Deleted {
fflog.Printf(c.Logger, "flag %v removed\n", key)
Expand Down
Loading