Skip to content

Commit

Permalink
add PrependPlugin (#1839)
Browse files Browse the repository at this point in the history
* add PrependPlugin

related: #1838

* added test for PrependPlugin
  • Loading branch information
Code-Hex authored Jan 24, 2022
1 parent 972878a commit 7cefef2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ func AddPlugin(p plugin.Plugin) Option {
}
}

// PrependPlugin prepends plugin any existing plugins
func PrependPlugin(p plugin.Plugin) Option {
return func(cfg *config.Config, plugins *[]plugin.Plugin) {
*plugins = append([]plugin.Plugin{p}, *plugins...)
}
}

// ReplacePlugin replaces any existing plugin with a matching plugin name
func ReplacePlugin(p plugin.Plugin) Option {
return func(cfg *config.Config, plugins *[]plugin.Plugin) {
Expand Down
13 changes: 13 additions & 0 deletions api/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,16 @@ func TestReplacePlugin(t *testing.T) {
require.EqualValues(t, expectedPlugin, pg[2])
})
}

func TestPrependPlugin(t *testing.T) {
modelgenPlugin := modelgen.New()
pg := []plugin.Plugin{
modelgenPlugin,
}

expectedPlugin := &testPlugin{}
PrependPlugin(expectedPlugin)(config.DefaultConfig(), &pg)

require.EqualValues(t, expectedPlugin, pg[0])
require.EqualValues(t, modelgenPlugin, pg[1])
}

0 comments on commit 7cefef2

Please sign in to comment.