-
Notifications
You must be signed in to change notification settings - Fork 2k
/
inmem.go
42 lines (36 loc) · 963 Bytes
/
inmem.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 {
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
}