forked from alexandrevilain/protoc-gen-moq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
141 lines (119 loc) · 3.29 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"flag"
"path"
"strings"
"github.com/alexandrevilain/protoc-gen-moq/internal/forked/github.com/matryer/moq/template"
"google.golang.org/protobuf/compiler/protogen"
)
var (
contextPackage = protogen.GoImportPath("context")
grpcPackage = protogen.GoImportPath("google.golang.org/grpc")
filePrefix *string
)
func main() {
var flags flag.FlagSet
filePrefix = flags.String("file_prefix", "", "Add prefix to the generated file (eg. /mock)")
protogen.Options{
ParamFunc: flags.Set,
}.Run(func(gen *protogen.Plugin) error {
for _, f := range gen.Files {
if !f.Generate {
continue
}
err := generateFile(gen, f, *filePrefix)
if err != nil {
return err
}
}
return nil
})
}
func rewriteFileNameWithPrefix(filepath, prefix string) string {
return path.Join(path.Dir(filepath), prefix+path.Base(filepath))
}
func generateFile(gen *protogen.Plugin, file *protogen.File, filePrefix string) error {
if len(file.Services) == 0 {
return nil // skip file generation when no service found
}
filepath := file.GeneratedFilenamePrefix + "_moq.pb.go"
if filePrefix != "" {
filepath = rewriteFileNameWithPrefix(filepath, filePrefix)
}
importPath := file.GoImportPath
if strings.HasSuffix(filePrefix, "/") {
basePath := strings.Trim(importPath.String(), "\"")
importPath = protogen.GoImportPath(path.Join(basePath, filePrefix))
}
g := gen.NewGeneratedFile(filepath, importPath)
t, err := template.New()
if err != nil {
return err
}
mocks := make([]template.MockData, len(file.Services))
for i, service := range file.Services {
clientName := service.GoName + "Client"
methods := make([]template.MethodData, len(service.Methods))
for i, method := range service.Methods {
// Generate params list
params := []template.ParamData{
{
Name: "ctx",
Type: g.QualifiedGoIdent(contextPackage.Ident("Context")),
Variadic: false,
},
}
if !method.Desc.IsStreamingClient() {
params = append(params, template.ParamData{
Name: "in",
Type: g.QualifiedGoIdent(method.Input.GoIdent),
Pointer: true,
Variadic: false,
})
}
params = append(params, template.ParamData{
Name: "opts",
Type: g.QualifiedGoIdent(grpcPackage.Ident("CallOption")),
Variadic: true,
})
// Generate return list
var result template.ParamData
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
result = template.ParamData{
Name: "out",
Type: g.QualifiedGoIdent(method.Output.GoIdent),
Pointer: true,
}
} else {
result = template.ParamData{
Name: "out",
Type: method.Parent.GoName + "_" + method.GoName + "Client",
Pointer: false,
}
}
methods[i] = template.MethodData{
Name: method.GoName,
Params: params,
Returns: []template.ParamData{
result,
{
Name: "err",
Type: "error",
},
},
}
}
mocks[i] = template.MockData{
InterfaceName: g.QualifiedGoIdent(file.GoImportPath.Ident(clientName)),
MockName: clientName + "Mock",
Methods: methods,
}
}
data := template.Data{
PkgName: string(file.GoPackageName),
Mocks: mocks,
StubImpl: false,
SyncPkg: g.QualifiedGoIdent(protogen.GoImportPath("sync").Ident("")),
}
return t.Execute(g, data)
}