Skip to content

Commit

Permalink
reduce the external dependencies of the plugin module
Browse files Browse the repository at this point in the history
  • Loading branch information
kpacha committed Apr 23, 2018
1 parent ec4f26e commit ce3f0e2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
11 changes: 7 additions & 4 deletions plugin/plugin.go
Expand Up @@ -5,19 +5,22 @@ import (
"io/ioutil"
"plugin"
"strings"

"github.com/devopsfaith/krakend/config"
)

// Plugin is the interface of the loaded plugins
type Plugin interface {
Lookup(name string) (plugin.Symbol, error)
}

type PluginDefinition interface {
GetFolder() string
GetPattern() string
}

// Load loads all the plugins in pluginFolder with pattern in its filename.
// It returns the number of plugins loaded and an error if something goes wrong.
func Load(cfg config.Plugin, reg *Register) (int, error) {
plugins, err := scan(cfg.Folder, cfg.Pattern)
func Load(cfg PluginDefinition, reg *Register) (int, error) {
plugins, err := scan(cfg.GetFolder(), cfg.GetPattern())
if err != nil {
return 0, err
}
Expand Down
12 changes: 6 additions & 6 deletions plugin/plugin_test.go
Expand Up @@ -28,7 +28,7 @@ func TestLoad_ok(t *testing.T) {
content: plugin.Symbol(registrableDummy(1)),
}, nil
}
tot, err := Load(config.Plugin{Folder: tmpDir, Pattern: ".so"}, NewRegister())
tot, err := Load(&config.Plugin{Folder: tmpDir, Pattern: ".so"}, NewRegister())
if tot != 1 {
t.Error("unexpected number of plugins loaded:", tot)
}
Expand All @@ -40,7 +40,7 @@ func TestLoad_ok(t *testing.T) {

func TestLoad_noFolder(t *testing.T) {
expectedErr := "open unknown: no such file or directory"
tot, err := Load(config.Plugin{Folder: "unknown", Pattern: ""}, NewRegister())
tot, err := Load(&config.Plugin{Folder: "unknown", Pattern: ""}, NewRegister())
if tot != 0 {
t.Error("unexpected number of plugins loaded:", tot)
}
Expand All @@ -59,7 +59,7 @@ func TestLoad_emptyFolder(t *testing.T) {
t.Error("unexpected error:", err.Error())
return
}
tot, err := Load(config.Plugin{Folder: name, Pattern: ""}, NewRegister())
tot, err := Load(&config.Plugin{Folder: name, Pattern: ""}, NewRegister())
if tot != 0 {
t.Error("unexpected number of plugins loaded:", tot)
}
Expand All @@ -83,7 +83,7 @@ func TestLoad_noMatches(t *testing.T) {
}
f.Close()
defer os.RemoveAll(tmpDir)
tot, err := Load(config.Plugin{Folder: tmpDir, Pattern: ".so"}, NewRegister())
tot, err := Load(&config.Plugin{Folder: tmpDir, Pattern: ".so"}, NewRegister())
if tot != 0 {
t.Error("unexpected number of plugins loaded:", tot)
}
Expand All @@ -106,7 +106,7 @@ func TestLoad_erroredLoad(t *testing.T) {
}
f.Close()
defer os.RemoveAll(tmpDir)
tot, err := Load(config.Plugin{Folder: tmpDir, Pattern: ".so"}, NewRegister())
tot, err := Load(&config.Plugin{Folder: tmpDir, Pattern: ".so"}, NewRegister())
if tot != 0 {
t.Error("unexpected number of plugins loaded:", tot)
}
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestLoad_panicRecovered(t *testing.T) {
}
f.Close()
defer os.RemoveAll(tmpDir)
tot, err := Load(config.Plugin{Folder: tmpDir, Pattern: ".so"}, NewRegister())
tot, err := Load(&config.Plugin{Folder: tmpDir, Pattern: ".so"}, NewRegister())
if tot != 0 {
t.Error("unexpected number of plugins loaded:", tot)
}
Expand Down
11 changes: 8 additions & 3 deletions plugin/register.go
Expand Up @@ -14,11 +14,16 @@ const REGISTRABLE_VAR = "Registrable"

// Register contains all the registers required by the framework and the external modules
type Register struct {
Decoder *encoding.DecoderRegister
Decoder DecoderRegisterer
SD *sd.Register
External *register.Namespaced
}

type DecoderRegisterer interface {
Set(name string, dec func(bool) func(io.Reader, *map[string]interface{}) error) error
Get(name string) func(bool) func(io.Reader, *map[string]interface{}) error
}

// NewRegister returns a new register to be used by the plugin loader
func NewRegister() *Register {
return &Register{
Expand All @@ -39,12 +44,12 @@ func (r *Register) Register(p Plugin) error {
totalRegistrations := 0

if registrable, ok := x.(RegistrableDecoder); ok {
err = registrable.RegisterDecoder(r.Decoder.Register)
err = registrable.RegisterDecoder(r.Decoder.Set)
totalRegistrations++
}

if registrable, ok := x.(RegistrableExternal); ok {
err = registrable.RegisterExternal(r.External.Register)
err = registrable.RegisterExternal(r.External.Set)
totalRegistrations++
}

Expand Down
2 changes: 1 addition & 1 deletion plugin/tests/app/main.go
Expand Up @@ -14,7 +14,7 @@ const pluginName = "supu"
func main() {
register := plugin.NewRegister()

fmt.Println(plugin.Load(config.Plugin{
fmt.Println(plugin.Load(&config.Plugin{
Folder: "../plugins/",
Pattern: ".so",
}, register))
Expand Down

0 comments on commit ce3f0e2

Please sign in to comment.