This commit is contained in:
crusader 2018-03-20 11:51:14 +09:00
parent 3b15bbd349
commit 5298f21b68
3 changed files with 21 additions and 26 deletions

View File

@ -5,3 +5,4 @@ import:
- package: git.loafle.net/commons_go/server
- package: gopkg.in/natefinch/npipe.v2
- package: git.loafle.net/commons_go/websocket_fasthttp
- package: git.loafle.net/commons_go/util

View File

@ -2,8 +2,10 @@ package json
import (
"encoding/json"
"reflect"
"git.loafle.net/commons_go/rpc/codec"
cur "git.loafle.net/commons_go/util/reflect"
)
// ----------------------------------------------------------------------------
@ -32,8 +34,8 @@ func (cnc *ClientNotificationCodec) ReadParams(args []interface{}) error {
if cnc.err == nil && cnc.noti.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.
raws := make([]json.RawMessage, len(args))
if err := json.Unmarshal(*cnc.noti.Params, &raws); err != nil {
var values []string
if err := json.Unmarshal(*cnc.noti.Params, &values); err != nil {
cnc.err = &Error{
Code: E_INVALID_REQ,
Message: err.Error(),
@ -43,9 +45,14 @@ func (cnc *ClientNotificationCodec) ReadParams(args []interface{}) error {
}
for indexI := 0; indexI < len(args); indexI++ {
raw := raws[indexI]
value := values[indexI]
arg := args[indexI]
if err := json.Unmarshal(raw, &arg); err != nil {
if cur.IsTypeKind(reflect.TypeOf(arg), reflect.String, true) {
arg = arg.(*string)
arg = &value
} else {
if err := json.Unmarshal([]byte(value), &arg); err != nil {
cnc.err = &Error{
Code: E_INVALID_REQ,
Message: err.Error(),
@ -55,6 +62,7 @@ func (cnc *ClientNotificationCodec) ReadParams(args []interface{}) error {
}
}
}
}
return cnc.err
}

View File

@ -95,20 +95,8 @@ func (src *ServerRequestCodec) ReadParams(args []interface{}) error {
if src.err == nil && src.req.Params != nil {
// Note: if src.req.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(*src.req.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.
// src.err = &Error{
// Code: E_INVALID_REQ,
// Message: err.Error(),
// Data: src.req.Params,
// }
// }
raws := make([]json.RawMessage, len(args))
if err := json.Unmarshal(*src.req.Params, &raws); err != nil {
var values []json.RawMessage
if err := json.Unmarshal(*src.req.Params, &values); err != nil {
src.err = &Error{
Code: E_INVALID_REQ,
Message: err.Error(),
@ -118,9 +106,7 @@ func (src *ServerRequestCodec) ReadParams(args []interface{}) error {
}
for indexI := 0; indexI < len(args); indexI++ {
raw := raws[indexI]
arg := args[indexI]
if err := json.Unmarshal(raw, &arg); err != nil {
if err := json.Unmarshal(values[indexI], &args[indexI]); err != nil {
src.err = &Error{
Code: E_INVALID_REQ,
Message: err.Error(),