Skip to content

Commit

Permalink
refactor: reduce code complexity
Browse files Browse the repository at this point in the history
Signed-off-by: Ink33 <[email protected]>
  • Loading branch information
Ink-33 committed Aug 13, 2023
1 parent 2cef5bb commit dd59a8b
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions pkg/runner/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,15 @@ func (r *gRPCTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataContext

func invokeRequest(ctx context.Context, md protoreflect.MethodDescriptor, payload string, conn *grpc.ClientConn) (respones []string, err error) {
resps := make([]*dynamicpb.Message, 0)

if md.IsStreamingClient() || md.IsStreamingServer() {
result := gjson.Parse(payload)
if !result.IsArray() {
gpayload := gjson.Parse(payload)
if !gpayload.IsArray() {
return nil, fmt.Errorf("payload is not a JSON array")
}

reqs := make([]*dynamicpb.Message, len(result.Array()))
for i, v := range result.Array() {
req := dynamicpb.NewMessage(md.Input())
err := protojson.Unmarshal([]byte(v.Raw), req)
reqs := make([]*dynamicpb.Message, len(gpayload.Array()))
for i, v := range gpayload.Array() {
req,err:=getReqMessagePb(md,v.Raw)
if err != nil {
return nil, err
}
Expand All @@ -113,12 +111,9 @@ func invokeRequest(ctx context.Context, md protoreflect.MethodDescriptor, payloa
}

} else {
request := dynamicpb.NewMessage(md.Input())
if payload != "" {
err = protojson.Unmarshal([]byte(payload), request)
if err != nil {
return nil, err
}
request, err := getReqMessagePb(md, payload)
if err != nil {
return nil, err
}

resp, err := invokeRPC(ctx, conn, md, request)
Expand All @@ -128,6 +123,21 @@ func invokeRequest(ctx context.Context, md protoreflect.MethodDescriptor, payloa
resps = append(resps, resp)
}

return buildResponses(resps)
}

func getReqMessagePb(md protoreflect.MethodDescriptor, message string) (messagepb *dynamicpb.Message, err error) {
request := dynamicpb.NewMessage(md.Input())
if message != "" {
err := protojson.Unmarshal([]byte(message), request)
if err != nil {
return nil, err
}
}
return request, nil
}

func buildResponses(resps []*dynamicpb.Message) ([]string, error) {
respsStr := make([]string, 0)
for i := range resps {
respbR, err := protojson.Marshal(resps[i])
Expand Down Expand Up @@ -289,7 +299,7 @@ func payloadFieldsVerify(caseName string, expect testing.Response, jsonPayload [
msg += err.Error()
}
}

if msg != "" {
return fmt.Errorf(msg)
}
Expand Down

0 comments on commit dd59a8b

Please sign in to comment.