/
gen_logic.go
89 lines (77 loc) · 2.92 KB
/
gen_logic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"os"
"strings"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/descriptorpb"
)
const (
logxPackage = protogen.GoImportPath("github.com/zeromicro/go-zero/core/logx")
)
const (
contextPkg = protogen.GoImportPath("context")
deprecationComment = "// Deprecated: Do not use."
zrpcPkg = protogen.GoImportPath("github.com/zeromicro/go-zero/zrpc")
grpcPkg = protogen.GoImportPath("google.golang.org/grpc")
)
func genZeroLogic(outDir string, gen *protogen.Plugin, file *protogen.File) {
if len(file.Services) == 0 {
return
}
rootPackage := protogen.GoImportPath(goPackage(outDir))
for _, service := range file.Services {
genZeroLogicFiles(outDir, rootPackage, file, gen, service)
}
return
}
func genZeroLogicFiles(outDir string, rootPackage protogen.GoImportPath, file *protogen.File, gen *protogen.Plugin, s *protogen.Service) {
uniMap := make(map[string]struct{})
for _, method := range s.Methods {
if _, ok := uniMap[method.GoName]; ok {
continue
}
uniMap[method.GoName] = struct{}{}
generateZeroLogicFile(outDir, rootPackage, method, gen, s)
}
}
func generateZeroLogicFile(outDir string, rootPackage protogen.GoImportPath, method *protogen.Method, gen *protogen.Plugin, s *protogen.Service) {
logicPackage := rootPackage + "/internal/logic"
svcPackage := rootPackage + "/internal/svc"
filename := outDir + "/internal/logic/" + strings.ToLower(method.GoName) + ".logic.go"
_, err := os.Stat(filename)
if !os.IsNotExist(err) {
return
}
g := gen.NewGeneratedFile(filename, logicPackage)
g.P("package logic")
g.P()
g.P("// This is a compile-time assertion to ensure that this generated file")
g.P("// is compatible with the Ccheers/protoc-gen-zeroapi package it is being compiled against.")
if s.Desc.Options().(*descriptorpb.ServiceOptions).GetDeprecated() {
g.P("//")
g.P(deprecationComment)
}
g.P(fmt.Sprintf("type %sLogic struct {", method.GoName))
g.P("logger ", logxPackage.Ident("Logger"))
g.P("ctx ", contextPkg.Ident("Context"))
g.P("svcCtx *", svcPackage.Ident("ServiceContext"))
g.P("}")
g.P("func New", method.GoName, "Logic(ctx ", contextPkg.Ident("Context"), ", svcCtx *", svcPackage.Ident("ServiceContext"), ") *", method.GoName, "Logic {")
g.P("return &", method.GoName, "Logic{")
g.P("logger: ", logxPackage.Ident("WithContext"), "(ctx),")
g.P("ctx: ctx,")
g.P("svcCtx: svcCtx,")
g.P("}")
g.P("}")
var args []string
if !method.Desc.IsStreamingClient() {
args = append(args, "req *"+g.QualifiedGoIdent(method.Input.GoIdent))
}
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
args = append(args, "stream "+g.QualifiedGoIdent(method.Input.GoIdent.GoImportPath.Ident(method.Parent.GoName+"_"+method.GoName+"Server")))
}
g.P("func (l *", method.GoName, "Logic) ", method.GoName, "(", strings.Join(args, ", "), ")", serverReturn(g, method), "{")
g.P("panic(\"implement me\")")
g.P("}")
}