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 {
|
2022-05-13 17:11:27 +00:00
|
|
|
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
|
|
|
|
}
|