open-nomad/helper/codec/inmem.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

46 lines
997 B
Go
Raw Normal View History

2018-01-11 18:17:23 +00:00
package codec
import (
"errors"
"net/rpc"
"reflect"
)
// InmemCodec is used to do an RPC call without going over a network
type InmemCodec struct {
Method string
Args interface{}
Reply interface{}
Err error
}
func (i *InmemCodec) ReadRequestHeader(req *rpc.Request) error {
req.ServiceMethod = i.Method
return nil
}
func (i *InmemCodec) ReadRequestBody(args interface{}) error {
if args == nil {
return nil
}
2018-01-11 18:17:23 +00:00
sourceValue := reflect.Indirect(reflect.Indirect(reflect.ValueOf(i.Args)))
dst := reflect.Indirect(reflect.Indirect(reflect.ValueOf(args)))
dst.Set(sourceValue)
return nil
}
func (i *InmemCodec) WriteResponse(resp *rpc.Response, reply interface{}) error {
if resp.Error != "" {
i.Err = errors.New(resp.Error)
return nil
}
sourceValue := reflect.Indirect(reflect.Indirect(reflect.ValueOf(reply)))
dst := reflect.Indirect(reflect.Indirect(reflect.ValueOf(i.Reply)))
dst.Set(sourceValue)
return nil
}
func (i *InmemCodec) Close() error {
return nil
}