From 892c4842f9eae7b1392f48b9bec0e13cf76d9284 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Sat, 27 Jul 2024 03:49:51 +0300 Subject: [PATCH] refactor: decrease indentation in api.ReplacePlugin (#3194) --- api/option.go | 25 +++++++++++++------------ api/option_test.go | 4 ++++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/api/option.go b/api/option.go index d376193dfef..344aa819c1b 100644 --- a/api/option.go +++ b/api/option.go @@ -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 } } diff --git a/api/option_test.go b/api/option_test.go index d84da902fdc..148937bab3c 100644 --- a/api/option_test.go +++ b/api/option_test.go @@ -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) {