Skip to content

Instantly share code, notes, and snippets.

@juanolon
Created September 5, 2015 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanolon/de0c752a92d0cc2ef4f2 to your computer and use it in GitHub Desktop.
Save juanolon/de0c752a92d0cc2ef4f2 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"net"
"net/rpc"
"reflect"
"github.com/davecgh/go-spew/spew"
"github.com/ugorji/go/codec"
)
func main() {
socket := "/var/folders/x6/h18jf2xj10bgb1_jlf_fy0rr0000gn/T/nvimkre3H5/0"
conn, err := net.Dial("unix", socket)
if err != nil {
log.Fatal("fail to connect to server: ", err)
return
}
var h codec.MsgpackHandle
// h.RawToString = true
h.WriteExt = true
h.SetExt(reflect.TypeOf(NvimError{}), 0, &NvimErrorExt{})
rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, &h)
client := rpc.NewClientWithCodec(rpcCodec)
var result []byte
err = client.Call("vim_get_current_window", nil, &result)
spew.Dump(client)
// spew.Dump(client.codec.rpcCodec.)
if err != nil {
log.Fatal("rpc send cmd: ", err)
}
// spew.Dump(result)
}
type NvimError struct {
msg []byte
}
func (e NvimError) Error() string {
return fmt.Sprintf("NvimError: %v", e.msg)
}
type NvimErrorExt struct{}
func (e NvimErrorExt) WriteExt(interface{}) []byte {
panic("unsupported")
}
func (e NvimErrorExt) ReadExt(interface{}, []byte) {
panic("unsupported")
}
func (e NvimErrorExt) ConvertExt(v interface{}) interface{} {
switch v2 := v.(type) {
default:
spew.Dump("converting type", v2)
panic(fmt.Sprintf("unsupported format for time conversin: expecting time.Time; got %T", v))
}
}
func (e NvimErrorExt) UpdateExt(dest interface{}, v interface{}) {
spew.Dump("update v2", dest)
tt := dest.(*NvimError)
switch v2 := v.(type) {
default:
spew.Dump("update v2", v2)
spew.Dump("update tt", tt)
// return NvimError{[]byte("babababa")}
panic(fmt.Sprintf("unsupported format for time conversion: expectiong int64/uint; got %T", v))
}
}
package main
import (
"fmt"
"log"
"reflect"
"github.com/davecgh/go-spew/spew"
"github.com/ugorji/go/codec"
)
func main() {
// 00000000 94 01 00 92 00 c4 30 57 72 6f 6e 67 20 6e 75 6d |......0Wrong num|
// 00000010 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 |ber of arguments|
// 00000020 3a 20 65 78 70 65 63 74 69 6e 67 20 30 20 62 75 |: expecting 0 bu|
// 00000030 74 20 67 6f 74 20 31 c0 00 00 00 00 00 00 00 00 |t got 1.........|
callResult := []byte{
0x94, 0x01, 0x00, 0x92, 0x00, 0xc4, 0x30, 0x57, 0x72, 0x6f, 0x6e, 0x67, 0x20, 0x6e, 0x75, 0x6d,
0x62, 0x65, 0x72, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x72, 0x67, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x73,
0x3a, 0x20, 0x65, 0x78, 0x70, 0x65, 0x63, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x30, 0x20, 0x62, 0x75,
0x74, 0x20, 0x67, 0x6f, 0x74, 0x20, 0x31, 0xc0,
}
var mh codec.MsgpackHandle
mh.RawToString = true
h.WriteExt = true
h.SetExt(reflect.TypeOf(NvimError{}), 0x92, &NvimErrorExt{})
dec := codec.NewDecoderBytes(callResult, &mh)
var result []byte
err := dec.Decode(&result)
if err != nil {
log.Fatal(err)
}
spew.Dump(result)
}
type NvimError struct {
msg []byte
}
func (e NvimError) Error() string {
return fmt.Sprintf("NvimError: %v", e.msg)
}
type NvimErrorExt struct{}
func (e NvimErrorExt) WriteExt(interface{}) []byte {
panic("unsupported")
}
func (e NvimErrorExt) ReadExt(interface{}, []byte) {
panic("unsupported")
}
func (e NvimErrorExt) ConvertExt(v interface{}) interface{} {
switch v2 := v.(type) {
default:
spew.Dump("converting type", v2)
panic(fmt.Sprintf("unsupported format for time conversin: expecting time.Time; got %T", v))
}
}
func (e NvimErrorExt) UpdateExt(dest interface{}, v interface{}) {
spew.Dump("update v2", dest)
tt := dest.(*NvimError)
switch v2 := v.(type) {
default:
spew.Dump("update v2", v2)
spew.Dump("update tt", tt)
// return NvimError{[]byte("babababa")}
panic(fmt.Sprintf("unsupported format for time conversion: expectiong int64/uint; got %T", v))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment