This commit is contained in:
crusader 2017-11-03 20:02:34 +09:00
parent e868bf48ee
commit 62fcae57f7
2 changed files with 68 additions and 13 deletions

View File

@ -3,6 +3,7 @@ package json
import ( import (
"encoding/json" "encoding/json"
"io" "io"
"log"
"sync" "sync"
"git.loafle.net/commons_go/rpc/encode" "git.loafle.net/commons_go/rpc/encode"
@ -164,17 +165,32 @@ func (scr *ServerCodecRequest) ReadParams(args []interface{}) error {
if scr.err == nil && scr.request.Params != nil { if scr.err == nil && scr.request.Params != nil {
// Note: if scr.request.Params is nil it's not an error, it's an optional member. // Note: if scr.request.Params is nil it's not an error, it's an optional member.
// JSON params structured object. Unmarshal to the args object. // JSON params structured object. Unmarshal to the args object.
if err := json.Unmarshal(*scr.request.Params, &args); err != nil {
// Clearly JSON params is not a structured object, // if err := json.Unmarshal(*scr.request.Params, &args); err != nil {
// fallback and attempt an unmarshal with JSON params as // // Clearly JSON params is not a structured object,
// array value and RPC params is struct. Unmarshal into // // fallback and attempt an unmarshal with JSON params as
// array containing the request struct. // // array value and RPC params is struct. Unmarshal into
scr.err = &Error{ // // array containing the request struct.
Code: E_INVALID_REQ, // scr.err = &Error{
Message: err.Error(), // Code: E_INVALID_REQ,
Data: scr.request.Params, // Message: err.Error(),
// Data: scr.request.Params,
// }
// }
jss := make([]json.RawMessage, len(args))
if err := json.Unmarshal(*scr.request.Params, &jss); err != nil {
log.Printf("err:%v", err)
} }
for indexI := 0; indexI < len(args); indexI++ {
js := jss[indexI]
arg := args[indexI]
if err := json.Unmarshal(js, &arg); err != nil {
log.Printf("err:%v", err)
} }
}
} }
return scr.err return scr.err
} }

View File

@ -77,7 +77,7 @@ func (rr *rpcRegistry) Invoke(codec protocol.RegistryCodec) (result interface{},
// Decode the args. // Decode the args.
var in []reflect.Value var in []reflect.Value
_, pInstances := methodSpec.getParamValues() pValues, pInstances := methodSpec.getParamValues()
if nil != pInstances && 0 < len(pInstances) { if nil != pInstances && 0 < len(pInstances) {
if errRead := codec.ReadParams(pInstances); errRead != nil { if errRead := codec.ReadParams(pInstances); errRead != nil {
@ -86,8 +86,8 @@ func (rr *rpcRegistry) Invoke(codec protocol.RegistryCodec) (result interface{},
pCount := len(pInstances) pCount := len(pInstances)
in = make([]reflect.Value, pCount+1) in = make([]reflect.Value, pCount+1)
for indexI := 0; indexI < pCount; indexI++ { for indexI := 0; indexI < pCount; indexI++ {
//in[indexI+1] = pValues[indexI] in[indexI+1] = pValues[indexI]
in[indexI+1] = reflect.ValueOf(pInstances[indexI]).Convert(methodSpec.paramTypes[indexI]) //in[indexI+1] = reflect.ValueOf(pInstances[indexI]).Convert(methodSpec.paramTypes[indexI])
} }
} else { } else {
in = make([]reflect.Value, 1) in = make([]reflect.Value, 1)
@ -110,3 +110,42 @@ func (rr *rpcRegistry) Invoke(codec protocol.RegistryCodec) (result interface{},
return return
} }
// func convertValues(values []interface{}, types []reflect.Type) []reflect.Value {
// c := len(values)
// vs := make([]reflect.Value, c)
// for indexI := 0; indexI < c; indexI++ {
// vs[indexI] = convertValue(&values[indexI], types[indexI])
// }
// return vs
// }
// func convertValue(v *interface{}, t reflect.Type) reflect.Value {
// switch t.Kind() {
// case reflect.Bool:
// return reflect.ValueOf(*v).Convert(t)
// case reflect.Float32, reflect.Float64:
// return reflect.ValueOf(*v).Convert(t)
// case reflect.Int, reflect.Int32, reflect.Int64:
// return reflect.ValueOf(*v).Convert(t)
// case reflect.Interface:
// // When we see an interface, we make our own thing
// return reflect.ValueOf(*v).Convert(t)
// case reflect.Map:
// return reflect.ValueOf(*v).M
// return d.decodeMap(name, node, result)
// case reflect.Ptr:
// return d.decodePtr(name, node, result)
// case reflect.Slice:
// return d.decodeSlice(name, node, result)
// case reflect.String:
// return d.decodeString(name, node, result)
// case reflect.Struct:
// return d.decodeStruct(name, node, result)
// default:
// return &parser.PosError{
// Pos: node.Pos(),
// Err: fmt.Errorf("%s: unknown kind to decode into: %s", name, k.Kind()),
// }
// }
// }