lotus/lib/jsonrpc/util.go

52 lines
897 B
Go
Raw Permalink Normal View History

2019-07-12 17:12:38 +00:00
package jsonrpc
import (
"encoding/json"
2019-07-17 06:05:11 +00:00
"fmt"
2019-07-12 17:12:38 +00:00
"reflect"
)
type param struct {
data []byte // from unmarshal
v reflect.Value // to marshal
}
func (p *param) UnmarshalJSON(raw []byte) error {
p.data = make([]byte, len(raw))
copy(p.data, raw)
return nil
}
func (p *param) MarshalJSON() ([]byte, error) {
return json.Marshal(p.v.Interface())
}
// processFuncOut finds value and error Outs in function
func processFuncOut(funcType reflect.Type) (valOut int, errOut int, n int) {
errOut = -1 // -1 if not found
valOut = -1
n = funcType.NumOut()
switch n {
case 0:
case 1:
if funcType.Out(0) == errorType {
errOut = 0
} else {
valOut = 0
}
case 2:
valOut = 0
errOut = 1
if funcType.Out(1) != errorType {
panic("expected error as second return value")
}
default:
2019-07-17 06:05:11 +00:00
errstr := fmt.Sprintf("too many return values: %s", funcType)
panic(errstr)
2019-07-12 17:12:38 +00:00
}
return
}