Skip to content

Commit

Permalink
remove point in providers slice
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong committed Feb 11, 2024
1 parent b17401e commit 7beb51e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
14 changes: 7 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Config struct {
delimiter string

values map[string]any
providers []*provider
providers []provider

onChanges map[string][]func(*Config)
onChangesMutex sync.RWMutex
Expand Down Expand Up @@ -84,13 +84,13 @@ func (c *Config) Load(loader Loader) error {
}
maps.Merge(c.values, values)

provider := &provider{
loader: loader,
values: make(map[string]any),
}
// Merged to empty map to convert to lower case.
maps.Merge(provider.values, values)
c.providers = append(c.providers, provider)
providerValues := make(map[string]any)
maps.Merge(providerValues, values)
c.providers = append(c.providers, provider{
loader: loader,
values: providerValues,
})

return nil
}
Expand Down
17 changes: 9 additions & 8 deletions provider/appconfig/appconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ func (a *AppConfig) Load() (map[string]any, error) {
}
a.token.Store(output.InitialConfigurationToken)
}
values, _, err := a.load(ctx)

return a.load(ctx)
return values, err
}

func (a *AppConfig) Watch(ctx context.Context, onChange func(map[string]any)) error {
Expand All @@ -107,7 +108,7 @@ func (a *AppConfig) Watch(ctx context.Context, onChange func(map[string]any)) er
for {
select {
case <-ticker.C:
values, err := a.load(ctx)
values, changed, err := a.load(ctx)
if err != nil {
a.logger.WarnContext(
ctx, "Error when reloading from AWS AppConfig",
Expand All @@ -120,7 +121,7 @@ func (a *AppConfig) Watch(ctx context.Context, onChange func(map[string]any)) er
continue
}

if values != nil {
if changed {
onChange(values)
}
case <-ctx.Done():
Expand All @@ -129,7 +130,7 @@ func (a *AppConfig) Watch(ctx context.Context, onChange func(map[string]any)) er
}
}

func (a *AppConfig) load(ctx context.Context) (map[string]any, error) {
func (a *AppConfig) load(ctx context.Context) (map[string]any, bool, error) {
ctx, cancel := context.WithTimeout(ctx, a.timeout)
defer cancel()

Expand All @@ -138,22 +139,22 @@ func (a *AppConfig) load(ctx context.Context) (map[string]any, error) {
}
output, err := a.client.GetLatestConfiguration(ctx, input)
if err != nil {
return nil, err
return nil, false, err
}
a.token.Store(output.NextPollConfigurationToken)

if len(output.Configuration) == 0 {
// It may return empty configuration data
// if the client already has the latest version.
return nil, nil //nolint:nilnil // Use nil to indicate no change
return nil, false, nil
}

var out map[string]any
if e := a.unmarshal(output.Configuration, &out); e != nil {
return nil, fmt.Errorf("unmarshal: %w", e)
return nil, false, fmt.Errorf("unmarshal: %w", e)
}

return out, nil
return out, true, nil
}

func (a *AppConfig) String() string {
Expand Down
2 changes: 1 addition & 1 deletion provider/pflag/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func WithDelimiter(delimiter string) Option {
// If it returns an nil/[]string{}/[]string{""}, the variable will be ignored.
//
// For example, with the default splitter, an flag name like "parent.child.key"
// // would be split into "parent", "child", and "key".
// would be split into "parent", "child", and "key".
func WithNameSplitter(splitter func(string) []string) Option {
return func(options *options) {
options.splitter = splitter
Expand Down
6 changes: 3 additions & 3 deletions watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (c *Config) Watch(ctx context.Context) error { //nolint:cyclop,funlen,gocog
panic("cannot watch change with nil context")
}

if hasWatcher := slices.ContainsFunc(c.providers, func(provider *provider) bool {
if hasWatcher := slices.ContainsFunc(c.providers, func(provider provider) bool {
_, ok := provider.loader.(Watcher)

return ok
Expand Down Expand Up @@ -97,8 +97,8 @@ func (c *Config) Watch(ctx context.Context) error { //nolint:cyclop,funlen,gocog
}()

errChan := make(chan error, len(c.providers))
for _, provider := range c.providers {
provider := provider
for i := range c.providers {
provider := &c.providers[i] // Use pointer for later modification.

if watcher, ok := provider.loader.(Watcher); ok {
waitGroup.Add(1)
Expand Down

0 comments on commit 7beb51e

Please sign in to comment.