/
fieldmask.go
144 lines (128 loc) · 4.25 KB
/
fieldmask.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
142
143
144
package main
import (
"fmt"
"strings"
fieldmask "github.com/douyu/jupiter/proto/fieldmask/v1"
"github.com/douyu/jupiter/pkg"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)
var version = pkg.JupiterVersion()
const (
googlefieldmaskPkg = protogen.GoImportPath("google.golang.org/protobuf/types/known/fieldmaskpb")
pbfieldmaskPkg = protogen.GoImportPath("github.com/douyu/jupiter/pkg/util/xfieldmask")
deprecationComment = "// Deprecated: Do not use."
)
// generateFile generates a _fm.pb.go file.
func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile {
if len(file.Services) == 0 {
return nil
}
filename := file.GeneratedFilenamePrefix + "_fm.pb.go"
g := gen.NewGeneratedFile(filename, file.GoImportPath)
g.P("// Code generated by github.com/douyu/jupiter/cmd/protoc-gen-go-fieldmask. DO NOT EDIT.")
g.P("// versions:")
g.P(fmt.Sprintf("// - protoc-gen-go-fieldmask %s", version))
g.P("// - protoc ", protocVersion(gen))
g.P()
g.P("package ", file.GoPackageName)
g.P()
g.P("// This is a compile-time assertion to ensure that this generated file")
g.P("// is compatible with the github.com/douyu/jupiter/cmd/protoc-gen-go-fieldmask package it is being compiled against.")
g.P("var _ = ", googlefieldmaskPkg.Ident("New"))
g.P("var _ = ", pbfieldmaskPkg.Ident("New"))
g.P()
for _, service := range file.Services {
genService(gen, file, g, service)
}
return g
}
func genService(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, s *protogen.Service) {
if s.Desc.Options().(*descriptorpb.ServiceOptions).GetDeprecated() {
g.P("//")
g.P(deprecationComment)
}
sd := &service{
Name: s.GoName,
FullName: string(s.Desc.FullName()),
FilePath: file.Desc.Path(),
}
// 判断是否存在对应扩展字段
for _, method := range s.Methods {
if genMessage(gen, method) != nil {
sd.Message = append(sd.Message, genMessage(gen, method))
}
}
g.P(sd.execute())
}
func genMessage(gen *protogen.Plugin, m *protogen.Method) *message {
var msg = &message{
UpdateInFields: make([]*field, 0),
UpdateOutFields: make([]*field, 0),
}
// 找到标识字段并判断请求响应是否需要mask
for _, field := range m.Input.Fields {
mask, ok := proto.GetExtension(field.Desc.Options(), fieldmask.E_Option).(*fieldmask.FieldMask)
if mask != nil && ok {
msg.IdentifyFieldGoName = field.GoName
msg.IdentifyField = string(field.Desc.Name())
if mask.GetIn() {
msg.RequestName = m.Input.GoIdent.GoName
msg.In = true
}
if mask.GetOut() {
msg.ResponseName = m.Output.GoIdent.GoName
msg.Out = true
}
break
}
}
// 写入所有mask的字段
visitMessage(gen, m.Input.Desc, "", msg, MsgTypeIn)
visitMessage(gen, m.Output.Desc, "", msg, MsgTypeOut)
msg.RemovePrefix()
return msg
}
// 递归遍历消息类型中的所有字段
func visitMessage(gen *protogen.Plugin, md protoreflect.MessageDescriptor, prefix string, msg *message, msgType MsgType) {
var subString string
for i := 0; i < md.Fields().Len(); i++ {
fd := md.Fields().Get(i)
if fd.Name() == protoreflect.Name(msg.IdentifyField) {
continue
}
if msgType == MsgTypeIn {
msg.UpdateInFields = append(msg.UpdateInFields, &field{
UnderLineName: fmt.Sprintf("%s_%s", prefix, fd.Name()),
DotName: fmt.Sprintf("%s.%s", prefix, fd.Name()),
})
subString = msg.RequestName
} else {
msg.UpdateOutFields = append(msg.UpdateOutFields, &field{
UnderLineName: fmt.Sprintf("%s_%s", prefix, fd.Name()),
DotName: fmt.Sprintf("%s.%s", prefix, fd.Name()),
})
subString = msg.ResponseName
}
// 判断嵌套类型
if fd.Kind() == protoreflect.MessageKind {
nestedMd := fd.Message()
if nestedMd != nil && strings.Contains(string(nestedMd.FullName()), subString) {
visitMessage(gen, nestedMd, prefix+"."+string(fd.Name()), msg, msgType)
}
}
}
}
func protocVersion(gen *protogen.Plugin) string {
v := gen.Request.GetCompilerVersion()
if v == nil {
return "(unknown)"
}
var suffix string
if s := v.GetSuffix(); s != "" {
suffix = "-" + s
}
return fmt.Sprintf("v%d.%d.%d%s", v.GetMajor(), v.GetMinor(), v.GetPatch(), suffix)
}