Skip to content

Commit

Permalink
refactor: decrease indentation in api.ReplacePlugin (#3194)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Jul 27, 2024
1 parent d1682f7 commit 892c484
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
25 changes: 13 additions & 12 deletions api/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@ func PrependPlugin(p plugin.Plugin) Option {
// ReplacePlugin replaces any existing plugin with a matching plugin name
func ReplacePlugin(p plugin.Plugin) Option {
return func(cfg *config.Config, plugins *[]plugin.Plugin) {
if plugins != nil {
found := false
ps := *plugins
for i, o := range ps {
if p.Name() == o.Name() {
ps[i] = p
found = true
}
}
if !found {
ps = append(ps, p)
if plugins == nil {
return
}
found := false
ps := *plugins
for i, o := range ps {
if p.Name() == o.Name() {
ps[i] = p
found = true
}
*plugins = ps
}
if !found {
ps = append(ps, p)
}
*plugins = ps
}
}
4 changes: 4 additions & 0 deletions api/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func TestReplacePlugin(t *testing.T) {
require.EqualValues(t, resolvergen.New(), pg[1])
require.EqualValues(t, expectedPlugin, pg[2])
})

t.Run("do nothing if plugins is nil", func(t *testing.T) {
ReplacePlugin(&testPlugin{})(config.DefaultConfig(), nil)
})
}

func TestPrependPlugin(t *testing.T) {
Expand Down

0 comments on commit 892c484

Please sign in to comment.