Skip to content

Commit c4c2ac0

Browse files
committed
plugin: retrieve non-pointer interface before casting into plugin Interface
Fixes #149 Signed-off-by: Romain Beuque <romain.beuque@ovhcloud.com>
1 parent b3dc0c9 commit c4c2ac0

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

pkg/plugins/plugins.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io/ioutil"
66
"plugin"
7+
"reflect"
78
"strings"
89

910
"github.com/ovh/configstore"
@@ -53,10 +54,15 @@ type InitializerPlugin interface {
5354
// InitializersFromFolder loads initialization plugins compiled as .so files
5455
// from a folder, runs them on a received pointer to a Service
5556
func InitializersFromFolder(path string, service *Service) error {
56-
return loadPlugins(path, func(fileName string, p plugin.Symbol) error {
57-
plug, ok := p.(InitializerPlugin)
57+
return loadPlugins(path, func(fileName string, pluginSymbol plugin.Symbol) error {
58+
reflectvalue := reflect.ValueOf(pluginSymbol)
59+
if reflectvalue.Kind() != reflect.Ptr {
60+
return fmt.Errorf("failed to load Plugin from %s: received a non-pointer object", fileName)
61+
}
62+
pluginInterface := reflectvalue.Elem().Interface()
63+
plug, ok := pluginInterface.(InitializerPlugin)
5864
if !ok {
59-
return fmt.Errorf("failed to assert type of plugin '%s': expected InitializerPlugin got %T", fileName, p)
65+
return fmt.Errorf("failed to assert type of plugin '%s': expected InitializerPlugin got %T", fileName, pluginInterface)
6066
}
6167
if err := plug.Init(service); err != nil {
6268
return fmt.Errorf("failed to run initialization plugin: %s", err)

0 commit comments

Comments
 (0)