Skip to content

Commit

Permalink
[version compatibility] fix up when response.ResultCode is 200 but re…
Browse files Browse the repository at this point in the history
…sponse.Success is false (#699)

* fix up when response.ResultCode is 200 but is not success

* fix up when response.ResultCode is 200 but is not success

* fix up when response.ResultCode is 200 but is not success

* fix up when response.ResultCode is 200 but is not success

* tiny fix
  • Loading branch information
robynron authored Dec 22, 2023
1 parent 81bf7d5 commit 7178ed3
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
5 changes: 1 addition & 4 deletions common/remote/rpc/grpc_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package rpc

import (
"context"
"encoding/json"
"time"

"github.com/golang/protobuf/ptypes/any"
Expand Down Expand Up @@ -65,9 +64,7 @@ func (g *GrpcConnection) request(request rpc_request.IRequest, timeoutMills int6
return nil, errors.Errorf("request:%s,unsupported response type:%s", request.GetRequestType(),
responsePayload.Metadata.GetType())
}
response := responseFunc()
err = json.Unmarshal(responsePayload.GetBody().Value, response)
return response, err
return rpc_response.InnerResponseJsonUnmarshal(responsePayload.GetBody().Value, responseFunc)
}

func (g *GrpcConnection) close() {
Expand Down
10 changes: 10 additions & 0 deletions common/remote/rpc/rpc_response/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package rpc_response

type ResponseCode int

const (
ResponseSuccessCode ResponseCode = 200
ResponseFailCode ResponseCode = 500

ResponseSuccessField = "success"
)
5 changes: 5 additions & 0 deletions common/remote/rpc/rpc_response/rpc_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type IResponse interface {
GetBody() string
GetErrorCode() int
IsSuccess() bool
SetSuccess(bool)
GetResultCode() int
GetMessage() string
}
Expand All @@ -60,6 +61,10 @@ func (r *Response) IsSuccess() bool {
return r.Success
}

func (r *Response) SetSuccess(successResult bool) {
r.Success = successResult
}

func (r *Response) GetErrorCode() int {
return r.ErrorCode
}
Expand Down
40 changes: 40 additions & 0 deletions common/remote/rpc/rpc_response/rpc_response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package rpc_response

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestRpcResponseIsSuccess(t *testing.T) {
responseBody0 := `{"resultCode":200,"errorCode":0}`
responseBody1 := `{"resultCode":200,"errorCode":0,"success":true}`
responseBody2 := `{"resultCode":200,"errorCode":0,"success":"true"}`
responseBody3 := `{"resultCode":200,"errorCode":0,"success":false}`
responseBody4 := `{"resultCode":500,"errorCode":0,"success":true}`
responseBody5 := `{"resultCode":500,"errorCode":0,"success":false}`

responseBodyList := make([]string, 0)
responseBodyList = append(responseBodyList, responseBody0, responseBody1, responseBody2, responseBody3, responseBody4, responseBody5)
for k, v := range ClientResponseMapping {
t.Run("test "+k, func(t *testing.T) {
for index, responseBody := range responseBodyList {
response, err := InnerResponseJsonUnmarshal([]byte(responseBody), v)
switch index {
case 0, 1, 4:
assert.True(t, response.IsSuccess())
break
case 3, 5:
assert.False(t, response.IsSuccess())
break
case 2:
assert.Nil(t, response)
assert.NotNil(t, err)
t.Logf("handle %d failed with responseBody: %s", index, responseBody)
break
default:
panic("unknown index")
}
}
})
}
}
24 changes: 24 additions & 0 deletions common/remote/rpc/rpc_response/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package rpc_response

import "encoding/json"

func InnerResponseJsonUnmarshal(responseBody []byte, responseFunc func() IResponse) (IResponse, error) {
response := responseFunc()
err := json.Unmarshal(responseBody, response)
if err != nil {
return nil, err
}

if !response.IsSuccess() {
tempFiledMap := make(map[string]interface{})
err = json.Unmarshal(responseBody, &tempFiledMap)
if err != nil {
return response, nil
}
if _, ok := tempFiledMap[ResponseSuccessField]; !ok {
response.SetSuccess(response.GetResultCode() == int(ResponseSuccessCode))
}
}
return response, err

}

0 comments on commit 7178ed3

Please sign in to comment.