Skip to content

Commit

Permalink
Define a type for onChange func instead of func pointer (#4656)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Jan 8, 2022
1 parent 52f8669 commit 2317957
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## 🛑 Breaking changes 🛑

- Define a type `WatcherFunc` for onChange func instead of func pointer (#4656)

## v0.42.0 Beta

## 🛑 Breaking changes 🛑
Expand Down
2 changes: 1 addition & 1 deletion config/configmapprovider/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewEnv(envName string) Provider {
}
}

func (emp *envMapProvider) Retrieve(_ context.Context, _ func(*ChangeEvent)) (Retrieved, error) {
func (emp *envMapProvider) Retrieve(context.Context, WatcherFunc) (Retrieved, error) {
if emp.envName == "" {
return nil, errors.New("config environment not specified")
}
Expand Down
2 changes: 1 addition & 1 deletion config/configmapprovider/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewFile(fileName string) Provider {
}
}

func (fmp *fileMapProvider) Retrieve(_ context.Context, _ func(*ChangeEvent)) (Retrieved, error) {
func (fmp *fileMapProvider) Retrieve(context.Context, WatcherFunc) (Retrieved, error) {
if fmp.fileName == "" {
return nil, errors.New("config file not specified")
}
Expand Down
2 changes: 1 addition & 1 deletion config/configmapprovider/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewProperties(properties []string) Provider {
}
}

func (pmp *propertiesMapProvider) Retrieve(_ context.Context, onChange func(*ChangeEvent)) (Retrieved, error) {
func (pmp *propertiesMapProvider) Retrieve(context.Context, WatcherFunc) (Retrieved, error) {
if len(pmp.properties) == 0 {
return NewRetrieved(func(ctx context.Context) (*config.Map, error) {
return config.NewMap(), nil
Expand Down
10 changes: 6 additions & 4 deletions config/configmapprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ type Provider interface {
// contains the value to be injected in the configuration and the corresponding watcher that
// will be used to monitor for updates of the retrieved value.
//
// onChange callback is called when the config changes. onChange may be called from
// a different go routine. After onChange is called Retrieved.Get should be called
// watcher callback is called when the config changes. watcher may be called from
// a different go routine. After watcher is called Retrieved.Get should be called
// to get the new config. See description of Retrieved for more details.
// onChange may be nil, which indicates that the caller is not interested in
// watcher may be nil, which indicates that the caller is not interested in
// knowing about the changes.
//
// If ctx is cancelled should return immediately with an error.
// Should never be called concurrently with itself or with Shutdown.
Retrieve(ctx context.Context, onChange func(*ChangeEvent)) (Retrieved, error)
Retrieve(ctx context.Context, watcher WatcherFunc) (Retrieved, error)

// Shutdown signals that the configuration for which this Provider was used to
// retrieve values is no longer in use and the Provider should close and release
Expand All @@ -62,6 +62,8 @@ type Provider interface {
Shutdown(ctx context.Context) error
}

type WatcherFunc func(*ChangeEvent)

// ChangeEvent describes the particular change event that happened with the config.
// TODO: see if this can be eliminated.
type ChangeEvent struct {
Expand Down
18 changes: 9 additions & 9 deletions service/config_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ type mockProvider struct {
errS error
}

func (m *mockProvider) Retrieve(_ context.Context, onChange func(*configmapprovider.ChangeEvent)) (configmapprovider.Retrieved, error) {
func (m *mockProvider) Retrieve(_ context.Context, watcher configmapprovider.WatcherFunc) (configmapprovider.Retrieved, error) {
if m.errR != nil {
return nil, m.errR
}
if m.ret == nil {
return &fakeRetrieved{}, nil
}
m.ret.onChange = onChange
m.ret.watcher = watcher
return m.ret, nil
}

Expand All @@ -63,16 +63,16 @@ func (ecu *errConfigUnmarshaler) Unmarshal(*config.Map, component.Factories) (*c

type fakeRetrieved struct {
configmapprovider.Retrieved
retM *config.Map
errG error
errW error
errC error
onChange func(event *configmapprovider.ChangeEvent)
retM *config.Map
errG error
errW error
errC error
watcher configmapprovider.WatcherFunc
}

func (er *fakeRetrieved) Get(context.Context) (*config.Map, error) {
if er.onChange != nil {
er.onChange(&configmapprovider.ChangeEvent{Error: er.errW})
if er.watcher != nil {
er.watcher(&configmapprovider.ChangeEvent{Error: er.errW})
}
if er.errG != nil {
return nil, er.errG
Expand Down

0 comments on commit 2317957

Please sign in to comment.