-2

I have a project consisting of 4 parts:

  1. A gateway (gateway/gateway.go) it's a package that knows how to talk with an application server and open this connection channel.

  2. A runner (runner/runner.go) it's the main (go build -o runner/runner runner/runner.go) It loads and execute Modules (using reflect I run functions from the module)!

  3. A framework (framework/framework.go) Implements many functionalities calling the gateway.

  4. Modules (aka Plugins in Go) (modules/sample.go) (go build -buildmode plugin -o modules/sample.so ./modules/sample.go) Using the framework, does customer logic! When init I export the reflect.Value of struct, then runner can run methods of this struct.

I want the runner instantiate the gateway and the framework obtain this instance without create a dependency between runner/framework.

Why? To avoid Go error 'plugin was built with a different version of package' when runner loads the module! If I update the runner (with the framework changed), I will invalidate old modules.

I already do that using 2 ways I don't like:

  1. Using context, but all functions from module and framework need receive a parameter context, then framework extract the gateway.

  2. Just let the framework instantiate gateway, but then the runner cannot use gateway.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ricardo
  • 11
  • 2

1 Answers1

-1

There have been a lot of headaches with Go plugins, especially the plugin compiler version must exactly match the program's compiler version. But the example works.

runner/runner.go

package main

import (
  "context"
  "fmt"
  "os"
  "plugin"
  "reflect"

  "../gateway"
  "../dep"
)

var a *gateway.Gateway

func main() {
  myModule := os.Args[1]

  if _, err := plugin.Open(myModule); err != nil {
    os.Exit(1)
  }

  mod, err := dep.NewModule()
  if err != nil {
    os.Exit(1)
  }

  a = gateway.NewGW()
  ctx := context.WithValue(context.Background(), "gateway", a)

  modreflect, err := mod.Init(ctx, dep.Config{})
  if err != nil {
    os.Exit(1)
  }

  if !modreflect.IsValid() {
    os.Exit(1)
  }

  modnode, err := mod.Start(ctx)
  if err != nil {
    os.Exit(1002)
  }

  for {
    if len(modnode) <= 0 {
      break
    }

    modnoderefl := modreflect.MethodByName(modnode)
    if !modnoderefl.IsValid() {
      break
    }

    result := modnoderefl.Call([]reflect.Value{reflect.ValueOf(ctx)})
    if len(result) != 2 {
      break
    }

    modnode = result[0].String()
  }

  mod.End(ctx)
}

gateway/gateway.go

package gateway

type Gateway struct {}

fun NewGW() *Gateway {
  a := Gateway{}
  return &a
}

dep/dep.go

package dep

import (
  "errors"
  "context"
  "reflect"
)

// Config is a configuration provider.
type Config map[string]interface{}

// Module is the interface implementated by types that
// register themselves as modular plug-ins.
type Module interface {
  Init(ctx context.Context, config Config) (reflect.Value,error)
  Start(ctx context.Context) (string,error)
  End(ctx context.Context) error
}

var themod = []func() Module{}

func RegisterModule(ctor func() Module) {
  themod = append(themod, ctor)
}

func NewModule() (Module, error) {
  if len(themod) == 0 {
    return nil, errors.New("Module not registered")
  }
  return themod[0](), nil
}

framework/framework.go

package framework

import (
    "fmt"
  "context"
  "../gateway"
)

type PlayFileInput struct {
  Path string
}

func Play(ctx context.Context, p PlayFileInput) error {
  if a := ctx.Value("gateway"); a != nil {

    if a.(*gateway.Gateway) != nil {
      _, err := a.(*gateway.Gateway).Exec("Playback", p.Path)
      return err
    }
  }

  return nil
}

modules/sample.go

package main

import "C"

import (
    "context"
    "fmt"
    "os"
  "reflect"

  "../dep"
  "../framework"
)

type MyModuleImpl struct {}

func init() {
  dep.RegisterModule(func() dep.Module {
    return &MyModuleImpl{}
  })
}

func (m *MyModuleImpl) Init(ctx context.Context, config dep.Config) (reflect.Value, error) {
  return reflect.ValueOf(m), nil
}

func (m *MyModuleImpl) Start(ctx context.Context) (string,error) {
  return "Menu_1",nil
}
func (n *MyModuleImpl)Menu_1(ctx context.Context) (string, error) {
  framework.Play(ctx, framework.PlayFileInput{Path: "welcome.wav"})
  return "Menu_2",nil
}
func (n *MyModuleImpl)Menu_2(ctx context.Context) (string, error) {
  return "Menu_3", nil
}
// ....
// ....
func (m *MyModuleImpl) End(ctx context.Context) error {
  return nil
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ricardo
  • 11
  • 2
  • ` "../gateway"` <-- don't use relative imports. It's officially unsupported, and only works "kind of by accident".See here: https://stackoverflow.com/q/38517593/13860 – Jonathan Hall Oct 30 '19 at 18:09
  • Your entire project looks like you're trying to fight against Go. You're using reflection, you're forcing plugins, you're using incorrect imports. I strongly encourage you to start fresh, read a book about Go idioms, and take a new approach. – Jonathan Hall Oct 30 '19 at 18:10