/
main.go
230 lines (216 loc) · 8.37 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"fmt"
pbzap "github.com/kei2100/protoc-gen-marshal-zap"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/pluginpb"
)
const (
zapcorePkg = protogen.GoImportPath("go.uber.org/zap/zapcore")
fmtPkg = protogen.GoImportPath("fmt")
)
func generateListField(g *protogen.GeneratedFile, f *protogen.Field) {
fname := f.Desc.Name()
g.P(fname, "ArrMarshaller := func(enc ", g.QualifiedGoIdent(zapcorePkg.Ident("ArrayEncoder")), ") error {")
g.P("for _, v := range x.", f.GoName, " {")
switch f.Desc.Kind() {
case protoreflect.BoolKind:
g.P("enc.AppendBool(v)")
case protoreflect.BytesKind:
g.P("enc.AppendByteString(v)")
case protoreflect.DoubleKind:
g.P("enc.AppendFloat64(v)")
case protoreflect.EnumKind:
g.P("enc.AppendString(v.String())")
case protoreflect.Fixed32Kind, protoreflect.Uint32Kind:
g.P("enc.AppendUint32(v)")
case protoreflect.Fixed64Kind, protoreflect.Uint64Kind:
g.P("enc.AppendUint64(v)")
case protoreflect.FloatKind:
g.P("enc.AppendFloat32(v)")
case protoreflect.Int32Kind, protoreflect.Sfixed32Kind, protoreflect.Sint32Kind:
g.P("enc.AppendInt32(v)")
case protoreflect.Int64Kind, protoreflect.Sfixed64Kind, protoreflect.Sint64Kind:
g.P("enc.AppendInt64(v)")
case protoreflect.GroupKind:
g.P("enc.AppendReflected(v)")
case protoreflect.MessageKind:
g.P("if obj, ok := interface{}(v).(", g.QualifiedGoIdent(zapcorePkg.Ident("ObjectMarshaler")), "); ok {")
g.P("enc.AppendObject(obj)")
g.P("} else {")
g.P("enc.AppendReflected(v)")
g.P("}")
case protoreflect.StringKind:
g.P("enc.AppendString(v)")
default:
g.P("enc.AppendReflected(v)")
}
g.P("}")
g.P("return nil")
g.P("}")
g.P("enc.AddArray(\"", fname, "\",", g.QualifiedGoIdent(zapcorePkg.Ident("ArrayMarshalerFunc")), "(", fname, "ArrMarshaller))")
}
func generateMapField(g *protogen.GeneratedFile, f *protogen.Field) {
fname := f.Desc.Name()
g.P("enc.AddObject(\"", fname, "\", ", g.QualifiedGoIdent(zapcorePkg.Ident("ObjectMarshalerFunc")), "(func(enc ", g.QualifiedGoIdent(zapcorePkg.Ident("ObjectEncoder")), ") error {")
g.P("for k, v := range x.", f.GoName, " {")
switch f.Desc.MapValue().Kind() {
case protoreflect.BoolKind:
g.P("enc.AddBool(", g.QualifiedGoIdent(fmtPkg.Ident("Sprintf")), "(\"%v\", k), v)")
case protoreflect.BytesKind:
g.P("enc.AddBinary(", g.QualifiedGoIdent(fmtPkg.Ident("Sprintf")), "(\"%v\", k), v)")
case protoreflect.DoubleKind:
g.P("enc.AddFloat64(", g.QualifiedGoIdent(fmtPkg.Ident("Sprintf")), "(\"%v\", k), v)")
case protoreflect.EnumKind:
g.P("enc.AddString(", g.QualifiedGoIdent(fmtPkg.Ident("Sprintf")), "(\"%v\", k), v.String())")
case protoreflect.Fixed32Kind, protoreflect.Uint32Kind:
g.P("enc.AddUint32(", g.QualifiedGoIdent(fmtPkg.Ident("Sprintf")), "(\"%v\", k), v)")
case protoreflect.Fixed64Kind, protoreflect.Uint64Kind:
g.P("enc.AddUint64(", g.QualifiedGoIdent(fmtPkg.Ident("Sprintf")), "(\"%v\", k), v)")
case protoreflect.FloatKind:
g.P("enc.AddFloat32(", g.QualifiedGoIdent(fmtPkg.Ident("Sprintf")), "(\"%v\", k), v)")
case protoreflect.Int32Kind, protoreflect.Sfixed32Kind, protoreflect.Sint32Kind:
g.P("enc.AddInt32(", g.QualifiedGoIdent(fmtPkg.Ident("Sprintf")), "(\"%v\", k), v)")
case protoreflect.Int64Kind, protoreflect.Sfixed64Kind, protoreflect.Sint64Kind:
g.P("enc.AddInt64(", g.QualifiedGoIdent(fmtPkg.Ident("Sprintf")), "(\"%v\", k), v)")
case protoreflect.GroupKind:
g.P("enc.AddReflected(", g.QualifiedGoIdent(fmtPkg.Ident("Sprintf")), "(\"%v\", k), v)")
case protoreflect.MessageKind:
g.P("if obj, ok := interface{}(v).(", g.QualifiedGoIdent(zapcorePkg.Ident("ObjectMarshaler")), "); ok {")
g.P("enc.AddObject(", g.QualifiedGoIdent(fmtPkg.Ident("Sprintf")), "(\"%v\", k), obj)")
g.P("} else {")
g.P("enc.AddReflected(", g.QualifiedGoIdent(fmtPkg.Ident("Sprintf")), "(\"%v\", k), v)")
g.P("}")
case protoreflect.StringKind:
g.P("enc.AddString(", g.QualifiedGoIdent(fmtPkg.Ident("Sprintf")), "(\"%v\", k), v)")
default:
g.P("enc.AddReflected(", g.QualifiedGoIdent(fmtPkg.Ident("Sprintf")), "(\"%v\", k), v)")
}
g.P("}")
g.P("return nil")
g.P("}))")
}
func generatePrimitiveField(g *protogen.GeneratedFile, f *protogen.Field) {
fname := f.Desc.Name()
var gname string
if f.Oneof != nil {
gname = fmt.Sprintf("Get%s()", f.GoName)
} else {
gname = f.GoName
}
switch f.Desc.Kind() {
case protoreflect.BoolKind:
g.P("enc.AddBool(\"", fname, "\", x.", gname, ")")
case protoreflect.BytesKind:
g.P("enc.AddBinary(\"", fname, "\", x.", gname, ")")
case protoreflect.DoubleKind:
g.P("enc.AddFloat64(\"", fname, "\", x.", gname, ")")
case protoreflect.EnumKind:
g.P("enc.AddString(\"", fname, "\", x.", gname, ".String())")
case protoreflect.Fixed32Kind, protoreflect.Uint32Kind:
g.P("enc.AddUint32(\"", fname, "\", x.", gname, ")")
case protoreflect.Fixed64Kind, protoreflect.Uint64Kind:
g.P("enc.AddUint64(\"", fname, "\", x.", gname, ")")
case protoreflect.FloatKind:
g.P("enc.AddFloat32(\"", fname, "\", x.", gname, ")")
case protoreflect.Int32Kind, protoreflect.Sfixed32Kind, protoreflect.Sint32Kind:
g.P("enc.AddInt32(\"", fname, "\", x.", gname, ")")
case protoreflect.Int64Kind, protoreflect.Sfixed64Kind, protoreflect.Sint64Kind:
g.P("enc.AddInt64(\"", fname, "\", x.", gname, ")")
case protoreflect.GroupKind:
g.P("enc.AddReflected(\"", fname, "\", x.", gname, ")")
case protoreflect.MessageKind:
g.P("if obj, ok := interface{}(x.", gname, ").(", g.QualifiedGoIdent(zapcorePkg.Ident("ObjectMarshaler")), "); ok {")
g.P("enc.AddObject(\"", fname, "\", obj)")
g.P("} else {")
g.P("enc.AddReflected(\"", fname, "\", x.", gname, ")")
g.P("}")
case protoreflect.StringKind:
g.P("enc.AddString(\"", fname, "\", x.", gname, ")")
default:
g.P("enc.AddReflected(\"", fname, "\", x.", gname, ")")
}
}
func isMasked(opts *descriptorpb.FieldOptions) bool {
return proto.GetExtension(opts, pbzap.E_Mask).(bool) || opts.GetDebugRedact()
}
func handleExplicitPresence(g *protogen.GeneratedFile, f *protogen.Field, generateFunc func(*protogen.GeneratedFile, *protogen.Field)) {
// Omit the fields that are defined as `Explicit Presence` and the value is not present.
// https://protobuf.dev/programming-guides/field_presence/#presence-in-proto3-apis
switch {
case f.Oneof != nil && f.Desc.HasOptionalKeyword():
// handle optional fields
g.P("if x.", f.GoName, " != nil {")
defer g.P("}")
case f.Oneof != nil && !f.Desc.HasOptionalKeyword():
// handle oneof fields
g.P("if _, ok := x.Get", f.Oneof.GoName, "().(*", f.GoIdent, "); ok {")
defer g.P("}")
case f.Desc.Kind() == protoreflect.MessageKind || f.Desc.Kind() == protoreflect.GroupKind:
// handle message fields
g.P("if x.", f.GoName, " != nil {")
defer g.P("}")
}
generateFunc(g, f)
}
func generateMessage(g *protogen.GeneratedFile, m *protogen.Message) {
ident := g.QualifiedGoIdent(m.GoIdent)
g.P("func (x *", ident, ") MarshalLogObject(enc ", g.QualifiedGoIdent(zapcorePkg.Ident("ObjectEncoder")), ") error {")
g.P("if x == nil {")
g.P("return nil")
g.P("}")
g.P()
for _, f := range m.Fields {
if isMasked(f.Desc.Options().(*descriptorpb.FieldOptions)) {
g.P("enc.AddString(\"", f.Desc.Name(), "\", \"[MASKED]\")")
} else if f.Desc.IsList() {
generateListField(g, f)
} else if f.Desc.IsMap() {
generateMapField(g, f)
} else {
handleExplicitPresence(g, f, generatePrimitiveField)
}
g.P()
}
g.P("return nil")
g.P("}")
g.P()
for _, submsg := range m.Messages {
if submsg.Desc.IsMapEntry() {
continue
}
generateMessage(g, submsg)
}
}
func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile {
if len(file.Messages) == 0 {
return nil
}
filename := fmt.Sprintf("%s.pb.marshal-zap.go", file.GeneratedFilenamePrefix)
g := gen.NewGeneratedFile(filename, file.GoImportPath)
g.P("// Code generated by protoc-gen-marshal-zap. DO NOT EDIT.")
g.P("//")
g.P("// source: ", file.Desc.Path())
g.P()
g.P("package ", file.GoPackageName)
g.P()
for _, m := range file.Messages {
generateMessage(g, m)
}
return g
}
func main() {
protogen.Options{}.Run(func(plugin *protogen.Plugin) error {
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
for _, file := range plugin.FilesByPath {
if !file.Generate {
continue
}
generateFile(plugin, file)
}
return nil
})
}