Skip to content

Commit c84f7f8

Browse files
committed
feat: v1版本(随意函数传入)
1 parent 8c0f013 commit c84f7f8

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
module github.com/Go-To-Byte/grpc-goroutine
22

33
go 1.19
4+
5+
require github.com/stretchr/testify v1.8.2
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
9+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
12+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
17+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

grpcrun/grpcrun_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// @Author: Ciusyan 2023/2/26
2+
package grpcrun_test
3+
4+
import (
5+
"context"
6+
"fmt"
7+
"github.com/stretchr/testify/assert"
8+
"testing"
9+
"time"
10+
11+
"github.com/Go-To-Byte/grpc-goroutine/grpcrun"
12+
)
13+
14+
// 作为GRPC请求的参数
15+
type loginReq struct {
16+
Username string `json:"username"`
17+
Password string `json:"password"`
18+
}
19+
20+
// 作为GRPC响应的参数
21+
type loginResp struct {
22+
UserId int `json:"user_id"`
23+
Token string `json:"token"`
24+
}
25+
26+
// GRPC请求的方法参数
27+
func Login(ctx context.Context, req *loginReq) (*loginResp, error) {
28+
if req.Username != "ciusyan" && req.Password != "222" {
29+
return nil, fmt.Errorf("登录失败")
30+
}
31+
fmt.Println("登录成功")
32+
return &loginResp{UserId: 21, Token: "testgrpccallsuccess"}, nil
33+
}
34+
35+
func TestGrpcTask(t *testing.T) {
36+
37+
should := assert.New(t)
38+
req := &loginReq{Username: "ciusyan", Password: "222"}
39+
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
40+
defer cancel()
41+
resp, err := grpcrun.GrpcTask(Login, &ctx, req)
42+
43+
if should.NoError(err) {
44+
t.Log(resp.(*loginResp))
45+
}
46+
}

grpcrun/task.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Package grpcrun @Author: Ciusyan 2023/2/26
2+
package grpcrun
3+
4+
import (
5+
"context"
6+
"fmt"
7+
"reflect"
8+
)
9+
10+
// GrpcTask :去调用GRPC的方法
11+
func GrpcTask(f any, ctx *context.Context, req any) (any, error) {
12+
13+
v := reflect.ValueOf(f)
14+
if v.Kind() != reflect.Func {
15+
return nil, fmt.Errorf("参数错误")
16+
}
17+
18+
// 调用参数
19+
argv := make([]reflect.Value, 2)
20+
argv[0] = reflect.ValueOf(*ctx)
21+
argv[1] = reflect.ValueOf(req)
22+
23+
// 具体调用
24+
res := v.Call(argv)
25+
26+
// 返回错误
27+
errV := res[1].Interface()
28+
if errV != nil {
29+
return res[0].Interface(), errV.(error)
30+
}
31+
32+
return res[0].Interface(), nil
33+
}

0 commit comments

Comments
 (0)