1

I am trying to use github.com/dullgiulio/pingo and send my custom struct

type LuaPlugin struct {
    Name string
    List []PluginTable
}

type PluginTable struct {
    Name string
    F lua.LGFunction
}

// LoadPlugins walks over the plugin directory loading all exported plugins
func LoadPlugins() {
    //
    p := pingo.NewPlugin("tcp", "plugins/test")
    // Actually start the plugin
    p.Start()
    // Remember to stop the plugin when done using it
    defer p.Stop()

    gob.Register(&LuaPlugin{})
    gob.Register(&PluginTable{})

    var resp *LuaPlugin

    // Call a function from the object we created previously
    if err := p.Call("MyPlugin.SayHello", "Go developer", &resp); err != nil {
        log.Print(err)
    } else {
        log.Print(resp.List[0])
    }
}

However I am always getting nil for the F field of ym struct. This is what I am sending on the client

// Create an object to be exported
type MyPlugin struct{}

// Exported method, with a RPC signature
func (p *MyPlugin) SayHello(name string, msg *util.LuaPlugin) error {
    //

    //
    *msg = util.LuaPlugin{
        Name: "test",
        List: []util.PluginTable{
            {
                Name: "hey",
                F: func(L *lua.LState) int {
                    log.Println(L.ToString(2))
                    return 0
                },
            },
        },
    }
    return nil
}

Is it not possible to send custom data types over RPC?

Raggaer
  • 3,244
  • 8
  • 36
  • 67

1 Answers1

0

I'm not familiar with the library however, you could try converting the struct to a byte slice before transport. Late reply, might help others....

Simple conversion: returns a struct as bytes

func StructToBytes(s interface{}) (converted []byte, err error) {
    var buff bytes.Buffer
    encoder := gob.NewEncoder(&buff)
    if err = encoder.Encode(s); err != nil {
        return
    }
    converted = buff.Bytes()
    return
}

Decoder: returns a wrapper to decode the bytes into

func Decoder(rawBytes []byte) (decoder *gob.Decoder) {
    reader := bytes.NewReader(rawBytes)
    decoder = gob.NewDecoder(reader)
    return
}

Example:

type MyStruct struct {
    Name string
}

toEncode := MyStruct{"John Doe"}

// convert the struct to bytes
structBytes, err := StructToBytes(toEncode)
if err != nil {
    panic(err)
}

//-----------
// send over RPC and decode on other side
//-----------

// create a new struct to decode into
var DecodeInto MyStruct

// pass the bytes to the decoder
decoder := Decoder(structBytes)

// Decode into the struct
decoder.Decode(&DecodeInto)

fmt.Println(DecodeInto.Name) // John Doe

Due to the use of the gob package you can swap types and typecast to a certain extent.

For more see: https://golang.org/pkg/encoding/gob/

Chucky
  • 126
  • 1