支持 stream 序列化 删除gateway的一些垃圾代码
This commit is contained in:
parent
a2873f273b
commit
2ed4499221
|
@ -329,11 +329,10 @@ func ExecCreateAutoLogic(workerSpaceDir string, ServiceName string, genDir, pack
|
||||||
|
|
||||||
methodMap := map[string]interface{}{
|
methodMap := map[string]interface{}{
|
||||||
"StructName": info.StructName,
|
"StructName": info.StructName,
|
||||||
|
"MethodType": met.MethodType,
|
||||||
"MethodName": met.MethodName,
|
"MethodName": met.MethodName,
|
||||||
"ParamsName": paramsName,
|
"ParamsName": paramsName,
|
||||||
"Params": paramsAndType,
|
"Params": paramsAndType,
|
||||||
"ParamCtx": met.Params[0],
|
|
||||||
"ParamReq": met.Params[1],
|
|
||||||
"MethodReturn": met.Returns[0],
|
"MethodReturn": met.Returns[0],
|
||||||
"MethodResponse": met.Returns[0][1:],
|
"MethodResponse": met.Returns[0][1:],
|
||||||
}
|
}
|
||||||
|
@ -374,9 +373,9 @@ func ExecCreateAutoLogic(workerSpaceDir string, ServiceName string, genDir, pack
|
||||||
// log.Println(genTypesBuffer.String())
|
// log.Println(genTypesBuffer.String())
|
||||||
formatted, err := format.Source(genTypesBuffer.Bytes())
|
formatted, err := format.Source(genTypesBuffer.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println(genTypesBuffer.String())
|
||||||
panic(fmt.Sprintf("格式化代码失败:%v\n", err))
|
// panic(fmt.Sprintf("格式化代码失败:%v\n", err))
|
||||||
// formatted = genTypesBuffer.Bytes()
|
formatted = genTypesBuffer.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = f.Write(formatted)
|
_, 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 {
|
if fdec, ok := decl.(*ast.GenDecl); ok && fdec.Tok == token.TYPE {
|
||||||
// ast.Print(fset, fdec)
|
// ast.Print(fset, fdec)
|
||||||
for _, spec := range fdec.Specs {
|
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 := typeSpec.Name.Name
|
||||||
serviceName = serviceName[:len(serviceName)-6]
|
serviceName = serviceName[:len(serviceName)-6]
|
||||||
|
@ -773,8 +772,8 @@ func genGatewayTestFunction(grpcPath string) (createdCollection []*GrpcMethodTes
|
||||||
// log.Println(ftype.)
|
// log.Println(ftype.)
|
||||||
if methodDecl.Obj != nil {
|
if methodDecl.Obj != nil {
|
||||||
if methodDecl.Obj.Kind == ast.Fun {
|
if methodDecl.Obj.Kind == ast.Fun {
|
||||||
log.Println(typeSpec.Name.Name)
|
// log.Println(typeSpec.Name.Name)
|
||||||
log.Println(methodDecl.Name)
|
// log.Println(methodDecl.Name)
|
||||||
|
|
||||||
created := GrpcMethodTest{}
|
created := GrpcMethodTest{}
|
||||||
|
|
||||||
|
@ -1080,9 +1079,25 @@ func parseGoFile(filePath string) (ClientParams []*ClientParam) {
|
||||||
// fieldName := field.Names[0].Name
|
// fieldName := field.Names[0].Name
|
||||||
// log.Println("Field:", fieldName)
|
// 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
|
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") {
|
if strings.HasSuffix(interfaceName, "Client") {
|
||||||
Param.ClientName = interfaceName[0 : len(interfaceName)-6]
|
Param.ClientName = interfaceName[0 : len(interfaceName)-6]
|
||||||
}
|
}
|
||||||
|
@ -1109,6 +1124,7 @@ func parseGoFile(filePath string) (ClientParams []*ClientParam) {
|
||||||
|
|
||||||
type GrpcServerMethod struct {
|
type GrpcServerMethod struct {
|
||||||
MethodName string
|
MethodName string
|
||||||
|
MethodType string
|
||||||
Params []string
|
Params []string
|
||||||
Returns []string
|
Returns []string
|
||||||
}
|
}
|
||||||
|
@ -1148,10 +1164,30 @@ func ParseGrpcServerInfo(grpcPath string) (infos []*GrpcServerInfo) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if strings.Contains(ifaceSpec.Name.Name, "_") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(ifaceSpec.Name.Name, "Unsafe") {
|
if strings.HasPrefix(ifaceSpec.Name.Name, "Unsafe") {
|
||||||
continue
|
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]
|
ServiceName := ifaceSpec.Name.Name[0 : len(ifaceSpec.Name.Name)-6]
|
||||||
info := &GrpcServerInfo{
|
info := &GrpcServerInfo{
|
||||||
ServiceName: ServiceName,
|
ServiceName: ServiceName,
|
||||||
|
@ -1172,25 +1208,32 @@ func ParseGrpcServerInfo(grpcPath string) (infos []*GrpcServerInfo) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
m := &GrpcServerMethod{}
|
m := &GrpcServerMethod{
|
||||||
|
MethodType: "rpc",
|
||||||
|
}
|
||||||
info.Method = append(info.Method, m)
|
info.Method = append(info.Method, m)
|
||||||
// 方法名称
|
// 方法名称
|
||||||
// log.Println("方法名:", method.Names[0].Name)
|
// log.Println("方法名:", method.Names[0].Name)
|
||||||
// MethodName := method.Names[0].Name
|
// MethodName := method.Names[0].Name
|
||||||
m.MethodName = method.Names[0].Name
|
m.MethodName = method.Names[0].Name
|
||||||
// 方法参数
|
// 方法参数
|
||||||
if len(method.Type.(*ast.FuncType).Params.List) > 0 {
|
mparams := method.Type.(*ast.FuncType).Params.List
|
||||||
// log.Println("参数:")
|
switch len(mparams) {
|
||||||
// params := method.Type.(*ast.FuncType).Params
|
case 2:
|
||||||
// log.Println(params.NumFields(), params.List)
|
for _, field := range mparams {
|
||||||
// log.Println(string(src[method.Pos()-1 : method.End()-1]))
|
|
||||||
|
|
||||||
for _, field := range method.Type.(*ast.FuncType).Params.List {
|
|
||||||
// log.Printf("%s %s\n", field.Names, getTypeString(field.Type, packageName, 0))
|
// log.Printf("%s %s\n", field.Names, getTypeString(field.Type, packageName, 0))
|
||||||
m.Params = append(m.Params, getTypeString(field.Type, packageName))
|
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("无参数")
|
log.Println("无参数")
|
||||||
|
default:
|
||||||
|
panic("诡异的结构")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 方法返回值
|
// 方法返回值
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
package service
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
|
||||||
"google.golang.org/grpc"
|
|
||||||
)
|
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -1,10 +1,23 @@
|
||||||
package {{.PackageName}}
|
package {{.PackageName}}
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
{{if eq .MethodType "rpc"}}
|
||||||
"context"
|
"context"
|
||||||
|
{{end}}
|
||||||
"{{.ProjectName}}/gen/go/service"
|
"{{.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) {
|
func (l *{{.StructName}}) {{.MethodName}}Logic({{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param}}{{end}}) (resp {{.MethodReturn}},err error) {
|
||||||
return resp, err
|
return resp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{{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}}
|
||||||
|
|
||||||
|
|
|
@ -45,9 +45,19 @@ type {{.MethodName}}Handler struct {
|
||||||
|
|
||||||
type {{.MethodName}}HandlerMust 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) {
|
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}})
|
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}}
|
{{end}}
|
||||||
|
|
|
@ -13,32 +13,24 @@ import "google/api/httpbody.proto";
|
||||||
//定义服务
|
//定义服务
|
||||||
service notify {
|
service notify {
|
||||||
// 邮件注册确认
|
// 邮件注册确认
|
||||||
rpc EmailSend(basic.Request) returns (EmailSendRes) {
|
rpc EmailSend(basic.Request) returns (EmailSendRes) {}
|
||||||
option (google.api.http) = {
|
|
||||||
post: "/api/notify/email/send"
|
|
||||||
body: "*"
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// 邮件注册确认
|
// 邮件注册确认
|
||||||
rpc EmailRegisterConfirm(basic.Request) returns (basic.Response) {
|
rpc EmailRegisterConfirm(stream EmailStreamReq) returns (stream EmailStreamResp) {}
|
||||||
option (google.api.http) = {
|
|
||||||
post: "/api/notify/email/register/confirm"
|
|
||||||
body: "*"
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
message EmailSendReq {
|
message EmailSendReq {
|
||||||
|
string name = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
message EmailStreamReq {
|
message EmailStreamReq {
|
||||||
string file_name = 1;
|
string file_name = 1;
|
||||||
google.api.HttpBody file_content = 2;
|
string file_content = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message EmailStreamResp {
|
||||||
|
string code = 1;
|
||||||
|
string ok = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user