diff --git a/glide.yaml b/glide.yaml index d9d0fd3..8d495b7 100644 --- a/glide.yaml +++ b/glide.yaml @@ -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 diff --git a/protocol/json/client_notification.go b/protocol/json/client_notification.go index 32dad5e..d36434a 100644 --- a/protocol/json/client_notification.go +++ b/protocol/json/client_notification.go @@ -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,15 +45,21 @@ 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 { - cnc.err = &Error{ - Code: E_INVALID_REQ, - Message: err.Error(), - Data: cnc.noti.Params, + + 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(), + Data: cnc.noti.Params, + } + return cnc.err } - return cnc.err } } } diff --git a/protocol/json/server_request.go b/protocol/json/server_request.go index ba90f3e..ecf8a5e 100644 --- a/protocol/json/server_request.go +++ b/protocol/json/server_request.go @@ -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(),