From 62fcae57f77d352c32d21375afcef1e8e811a3d0 Mon Sep 17 00:00:00 2001 From: crusader Date: Fri, 3 Nov 2017 20:02:34 +0900 Subject: [PATCH] ing --- protocol/json/server.go | 36 ++++++++++++++++++++++++--------- registry.go | 45 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 13 deletions(-) diff --git a/protocol/json/server.go b/protocol/json/server.go index 878122e..9995583 100644 --- a/protocol/json/server.go +++ b/protocol/json/server.go @@ -3,6 +3,7 @@ package json import ( "encoding/json" "io" + "log" "sync" "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 { // 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. - if err := json.Unmarshal(*scr.request.Params, &args); err != nil { - // Clearly JSON params is not a structured object, - // fallback and attempt an unmarshal with JSON params as - // array value and RPC params is struct. Unmarshal into - // array containing the request struct. - scr.err = &Error{ - Code: E_INVALID_REQ, - Message: err.Error(), - Data: scr.request.Params, - } + + // if err := json.Unmarshal(*scr.request.Params, &args); err != nil { + // // Clearly JSON params is not a structured object, + // // fallback and attempt an unmarshal with JSON params as + // // array value and RPC params is struct. Unmarshal into + // // array containing the request struct. + // scr.err = &Error{ + // Code: E_INVALID_REQ, + // 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 } diff --git a/registry.go b/registry.go index 2b6bc05..950cee4 100644 --- a/registry.go +++ b/registry.go @@ -77,7 +77,7 @@ func (rr *rpcRegistry) Invoke(codec protocol.RegistryCodec) (result interface{}, // Decode the args. var in []reflect.Value - _, pInstances := methodSpec.getParamValues() + pValues, pInstances := methodSpec.getParamValues() if nil != pInstances && 0 < len(pInstances) { if errRead := codec.ReadParams(pInstances); errRead != nil { @@ -86,8 +86,8 @@ func (rr *rpcRegistry) Invoke(codec protocol.RegistryCodec) (result interface{}, pCount := len(pInstances) in = make([]reflect.Value, pCount+1) for indexI := 0; indexI < pCount; indexI++ { - //in[indexI+1] = pValues[indexI] - in[indexI+1] = reflect.ValueOf(pInstances[indexI]).Convert(methodSpec.paramTypes[indexI]) + in[indexI+1] = pValues[indexI] + //in[indexI+1] = reflect.ValueOf(pInstances[indexI]).Convert(methodSpec.paramTypes[indexI]) } } else { in = make([]reflect.Value, 1) @@ -110,3 +110,42 @@ func (rr *rpcRegistry) Invoke(codec protocol.RegistryCodec) (result interface{}, 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()), +// } +// } +// }