Skip to content

Commit 892c484

Browse files
authored
refactor: decrease indentation in api.ReplacePlugin (#3194)
1 parent d1682f7 commit 892c484

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

‎api/option.go‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@ func PrependPlugin(p plugin.Plugin) Option {
2929
// ReplacePlugin replaces any existing plugin with a matching plugin name
3030
func ReplacePlugin(p plugin.Plugin) Option {
3131
return func(cfg *config.Config, plugins *[]plugin.Plugin) {
32-
if plugins != nil {
33-
found := false
34-
ps := *plugins
35-
for i, o := range ps {
36-
if p.Name() == o.Name() {
37-
ps[i] = p
38-
found = true
39-
}
40-
}
41-
if !found {
42-
ps = append(ps, p)
32+
if plugins == nil {
33+
return
34+
}
35+
found := false
36+
ps := *plugins
37+
for i, o := range ps {
38+
if p.Name() == o.Name() {
39+
ps[i] = p
40+
found = true
4341
}
44-
*plugins = ps
4542
}
43+
if !found {
44+
ps = append(ps, p)
45+
}
46+
*plugins = ps
4647
}
4748
}

‎api/option_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func TestReplacePlugin(t *testing.T) {
5353
require.EqualValues(t, resolvergen.New(), pg[1])
5454
require.EqualValues(t, expectedPlugin, pg[2])
5555
})
56+
57+
t.Run("do nothing if plugins is nil", func(t *testing.T) {
58+
ReplacePlugin(&testPlugin{})(config.DefaultConfig(), nil)
59+
})
5660
}
5761

5862
func TestPrependPlugin(t *testing.T) {

0 commit comments

Comments
 (0)