diff --git a/goutils/proto_build/main.go b/goutils/proto_build/main.go index 013d6f7..87f296e 100644 --- a/goutils/proto_build/main.go +++ b/goutils/proto_build/main.go @@ -329,11 +329,10 @@ func ExecCreateAutoLogic(workerSpaceDir string, ServiceName string, genDir, pack methodMap := map[string]interface{}{ "StructName": info.StructName, + "MethodType": met.MethodType, "MethodName": met.MethodName, "ParamsName": paramsName, "Params": paramsAndType, - "ParamCtx": met.Params[0], - "ParamReq": met.Params[1], "MethodReturn": met.Returns[0], "MethodResponse": met.Returns[0][1:], } @@ -374,9 +373,9 @@ func ExecCreateAutoLogic(workerSpaceDir string, ServiceName string, genDir, pack // log.Println(genTypesBuffer.String()) formatted, err := format.Source(genTypesBuffer.Bytes()) if err != nil { - - panic(fmt.Sprintf("格式化代码失败:%v\n", err)) - // formatted = genTypesBuffer.Bytes() + log.Println(genTypesBuffer.String()) + // panic(fmt.Sprintf("格式化代码失败:%v\n", err)) + formatted = genTypesBuffer.Bytes() } _, err = f.Write(formatted) @@ -763,7 +762,7 @@ func genGatewayTestFunction(grpcPath string) (createdCollection []*GrpcMethodTes if fdec, ok := decl.(*ast.GenDecl); ok && fdec.Tok == token.TYPE { // ast.Print(fset, fdec) for _, spec := range fdec.Specs { - if typeSpec, ok := spec.(*ast.TypeSpec); ok && strings.HasSuffix(typeSpec.Name.Name, "Server") && !strings.HasPrefix(typeSpec.Name.Name, "Unsafe") { + if typeSpec, ok := spec.(*ast.TypeSpec); ok && strings.HasSuffix(typeSpec.Name.Name, "Server") && !strings.HasPrefix(typeSpec.Name.Name, "Unsafe") && !strings.Contains(typeSpec.Name.Name, "_") { serviceName := typeSpec.Name.Name serviceName = serviceName[:len(serviceName)-6] @@ -773,8 +772,8 @@ func genGatewayTestFunction(grpcPath string) (createdCollection []*GrpcMethodTes // log.Println(ftype.) if methodDecl.Obj != nil { if methodDecl.Obj.Kind == ast.Fun { - log.Println(typeSpec.Name.Name) - log.Println(methodDecl.Name) + // log.Println(typeSpec.Name.Name) + // log.Println(methodDecl.Name) created := GrpcMethodTest{} @@ -1080,9 +1079,25 @@ func parseGoFile(filePath string) (ClientParams []*ClientParam) { // fieldName := field.Names[0].Name // log.Println("Field:", fieldName) } - } else if _, ok := typeSpec.Type.(*ast.InterfaceType); ok { + } else if iface, ok := typeSpec.Type.(*ast.InterfaceType); ok { interfaceName := typeSpec.Name.Name + if len(iface.Methods.List) == 3 { + if len(iface.Methods.List[0].Names) == 1 && len(iface.Methods.List[1].Names) == 1 { + name0 := iface.Methods.List[0].Names[0].Name + name1 := iface.Methods.List[1].Names[0].Name + if name0 == "Send" && name1 == "CloseAndRecv" { + // log.Println(name0, name1) + if getTypeString(iface.Methods.List[2].Type) == "grpc.ClientStream" { + // MethodType = "stream" + // log.Println(interfaceName) + + continue + } + } + } + } + if strings.HasSuffix(interfaceName, "Client") { Param.ClientName = interfaceName[0 : len(interfaceName)-6] } @@ -1109,6 +1124,7 @@ func parseGoFile(filePath string) (ClientParams []*ClientParam) { type GrpcServerMethod struct { MethodName string + MethodType string Params []string Returns []string } @@ -1148,10 +1164,30 @@ func ParseGrpcServerInfo(grpcPath string) (infos []*GrpcServerInfo) { continue } + if strings.Contains(ifaceSpec.Name.Name, "_") { + continue + } + if strings.HasPrefix(ifaceSpec.Name.Name, "Unsafe") { continue } + if len(ifaceType.Methods.List) == 3 { + if len(ifaceType.Methods.List[0].Names) == 1 && len(ifaceType.Methods.List[1].Names) == 1 { + name0 := ifaceType.Methods.List[0].Names[0].Name + name1 := ifaceType.Methods.List[1].Names[0].Name + if name0 == "SendAndClose" && name1 == "Recv" { + // log.Println(name0, name1) + if getTypeString(ifaceType.Methods.List[2].Type) == "grpc.ServerStream" { + // MethodType = "stream" + continue + } + + } + + } + } + ServiceName := ifaceSpec.Name.Name[0 : len(ifaceSpec.Name.Name)-6] info := &GrpcServerInfo{ ServiceName: ServiceName, @@ -1172,25 +1208,32 @@ func ParseGrpcServerInfo(grpcPath string) (infos []*GrpcServerInfo) { continue } - m := &GrpcServerMethod{} + m := &GrpcServerMethod{ + MethodType: "rpc", + } info.Method = append(info.Method, m) // 方法名称 // log.Println("方法名:", method.Names[0].Name) // MethodName := method.Names[0].Name m.MethodName = method.Names[0].Name // 方法参数 - if len(method.Type.(*ast.FuncType).Params.List) > 0 { - // log.Println("参数:") - // params := method.Type.(*ast.FuncType).Params - // log.Println(params.NumFields(), params.List) - // log.Println(string(src[method.Pos()-1 : method.End()-1])) - - for _, field := range method.Type.(*ast.FuncType).Params.List { + mparams := method.Type.(*ast.FuncType).Params.List + switch len(mparams) { + case 2: + for _, field := range mparams { // log.Printf("%s %s\n", field.Names, getTypeString(field.Type, packageName, 0)) m.Params = append(m.Params, getTypeString(field.Type, packageName)) } - } else { + case 1: + m.MethodType = "stream" + for _, field := range mparams { + // log.Printf("%s %s\n", field.Names, getTypeString(field.Type, packageName, 0)) + m.Params = append(m.Params, getTypeString(field.Type, packageName)) + } + case 0: log.Println("无参数") + default: + panic("诡异的结构") } // 方法返回值 diff --git a/goutils/proto_build/tpls/auto_grpc_gateway_nacos.tpl b/goutils/proto_build/tpls/auto_grpc_gateway_nacos.tpl deleted file mode 100644 index 36f5fc9..0000000 --- a/goutils/proto_build/tpls/auto_grpc_gateway_nacos.tpl +++ /dev/null @@ -1,10 +0,0 @@ -package service - -import ( - "context" - "net/http" - - "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" - "google.golang.org/grpc" -) - diff --git a/goutils/proto_build/tpls/auto_grpc_gateway_register.tpl b/goutils/proto_build/tpls/auto_grpc_gateway_register.tpl deleted file mode 100644 index f823a8d..0000000 --- a/goutils/proto_build/tpls/auto_grpc_gateway_register.tpl +++ /dev/null @@ -1,20 +0,0 @@ -package logic - -import ( - "context" - "{{.ProjectName}}/gen/go/service" - - "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" - "google.golang.org/grpc" -) - -func AutoRegisterHandler(ctx context.Context, mux *runtime.ServeMux, opts ...grpc.DialOption) error { - var err error - {{range .FuncNames}} - err = service.{{.}}(ctx, mux, opts...) - if err != nil { - return err - } - {{end}} - return nil -} diff --git a/goutils/proto_build/tpls/logic_fusen_handler.tpl b/goutils/proto_build/tpls/logic_fusen_handler.tpl index 9b55c37..acc11ea 100644 --- a/goutils/proto_build/tpls/logic_fusen_handler.tpl +++ b/goutils/proto_build/tpls/logic_fusen_handler.tpl @@ -1,10 +1,23 @@ package {{.PackageName}} import ( +{{if eq .MethodType "rpc"}} "context" +{{end}} "{{.ProjectName}}/gen/go/service" ) +{{if eq .MethodType "rpc"}} + func (l *{{.StructName}}) {{.MethodName}}Logic({{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param}}{{end}}) (resp {{.MethodReturn}},err error) { return resp, err -} \ No newline at end of file +} + +{{else if eq .MethodType "stream"}} + +func (l *{{.StructName}}) {{.MethodName}}Logic(stream {{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param}}{{end}}) (err error) { + return err +} + +{{end}} + diff --git a/goutils/proto_build/tpls/logic_grpc_struct.tpl b/goutils/proto_build/tpls/logic_grpc_struct.tpl index 286b7c4..daee0b4 100644 --- a/goutils/proto_build/tpls/logic_grpc_struct.tpl +++ b/goutils/proto_build/tpls/logic_grpc_struct.tpl @@ -45,9 +45,19 @@ type {{.MethodName}}Handler struct { type {{.MethodName}}HandlerMust struct{} +{{if eq .MethodType "rpc"}} + func (lgrpc *{{.StructName}}Grpc) {{.MethodName}}({{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param}}{{end}}) ({{.MethodReturn}}, error) { return New{{.StructName}}(ctx).{{.MethodName}}Logic({{range $index, $param := .ParamsName}}{{if $index}}, {{end}}{{$param}}{{end}}) } +{{else if eq .MethodType "stream"}} + +func (lgrpc *{{.StructName}}Grpc) {{.MethodName}}(stream {{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param}}{{end}}) error { + return New{{.StructName}}(stream.Context()).{{.MethodName}}Logic(stream) +} + +{{end}} + {{end}} \ No newline at end of file diff --git a/service/notify.proto b/service/notify.proto index 1de1be6..d5c6f2f 100644 --- a/service/notify.proto +++ b/service/notify.proto @@ -13,32 +13,24 @@ import "google/api/httpbody.proto"; //定义服务 service notify { // 邮件注册确认 - rpc EmailSend(basic.Request) returns (EmailSendRes) { - option (google.api.http) = { - post: "/api/notify/email/send" - body: "*" - }; - } + rpc EmailSend(basic.Request) returns (EmailSendRes) {} // 邮件注册确认 - rpc EmailRegisterConfirm(basic.Request) returns (basic.Response) { - option (google.api.http) = { - post: "/api/notify/email/register/confirm" - body: "*" - }; - } - - - + rpc EmailRegisterConfirm(stream EmailStreamReq) returns (stream EmailStreamResp) {} } message EmailSendReq { - + string name = 1; } message EmailStreamReq { string file_name = 1; - google.api.HttpBody file_content = 2; + string file_content = 2; +} + +message EmailStreamResp { + string code = 1; + string ok = 2; }