blob: 4f38570f7ffb4af77bb4ab171a149d5d197131b1 [file] [log] [blame]
Damien Neil220c2022018-08-15 11:24:18 -07001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Damien Neil1adaec92018-09-24 13:43:03 -07005// Package internal_gengo is internal to the protobuf module.
6package internal_gengo
Damien Neil220c2022018-08-15 11:24:18 -07007
8import (
Damien Neil7779e052018-09-07 14:14:06 -07009 "bytes"
10 "compress/gzip"
11 "crypto/sha256"
12 "encoding/hex"
13 "fmt"
Damien Neilebc699d2018-09-13 08:50:13 -070014 "math"
Damien Neilce36f8d2018-09-13 15:19:08 -070015 "sort"
Damien Neil7779e052018-09-07 14:14:06 -070016 "strconv"
Damien Neilcab8dfe2018-09-06 14:51:28 -070017 "strings"
Damien Neil7779e052018-09-07 14:14:06 -070018
19 "github.com/golang/protobuf/proto"
20 descpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
Joe Tsai01ab2962018-09-21 17:44:00 -070021 "github.com/golang/protobuf/v2/protogen"
22 "github.com/golang/protobuf/v2/reflect/protoreflect"
Damien Neil220c2022018-08-15 11:24:18 -070023)
24
Damien Neild4127922018-09-12 11:13:49 -070025// generatedCodeVersion indicates a version of the generated code.
26// It is incremented whenever an incompatibility between the generated code and
27// proto package is introduced; the generated code references
28// a constant, proto.ProtoPackageIsVersionN (where N is generatedCodeVersion).
29const generatedCodeVersion = 2
30
Damien Neil46abb572018-09-07 12:45:37 -070031const protoPackage = "github.com/golang/protobuf/proto"
32
Damien Neild39efc82018-09-24 12:38:10 -070033type fileInfo struct {
Damien Neilcab8dfe2018-09-06 14:51:28 -070034 *protogen.File
Damien Neil46abb572018-09-07 12:45:37 -070035 locationMap map[string][]*descpb.SourceCodeInfo_Location
36 descriptorVar string // var containing the gzipped FileDescriptorProto
Damien Neilce36f8d2018-09-13 15:19:08 -070037 allEnums []*protogen.Enum
38 allMessages []*protogen.Message
Damien Neil993c04d2018-09-14 15:41:11 -070039 allExtensions []*protogen.Extension
Damien Neilcab8dfe2018-09-06 14:51:28 -070040}
41
Damien Neil9c420a62018-09-27 15:26:33 -070042// GenerateFile generates the contents of a .pb.go file.
43func GenerateFile(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile) {
Damien Neild39efc82018-09-24 12:38:10 -070044 f := &fileInfo{
Damien Neilcab8dfe2018-09-06 14:51:28 -070045 File: file,
46 locationMap: make(map[string][]*descpb.SourceCodeInfo_Location),
47 }
48 for _, loc := range file.Proto.GetSourceCodeInfo().GetLocation() {
49 key := pathKey(loc.Path)
50 f.locationMap[key] = append(f.locationMap[key], loc)
51 }
52
Damien Neil993c04d2018-09-14 15:41:11 -070053 // The different order for enums and extensions is to match the output
54 // of the previous implementation.
55 //
56 // TODO: Eventually make this consistent.
Damien Neilce36f8d2018-09-13 15:19:08 -070057 f.allEnums = append(f.allEnums, f.File.Enums...)
Damien Neil73ac8852018-09-17 15:11:24 -070058 walkMessages(f.Messages, func(message *protogen.Message) {
59 f.allMessages = append(f.allMessages, message)
60 f.allEnums = append(f.allEnums, message.Enums...)
61 f.allExtensions = append(f.allExtensions, message.Extensions...)
62 })
Damien Neil993c04d2018-09-14 15:41:11 -070063 f.allExtensions = append(f.allExtensions, f.File.Extensions...)
Damien Neilce36f8d2018-09-13 15:19:08 -070064
Damien Neil46abb572018-09-07 12:45:37 -070065 // Determine the name of the var holding the file descriptor:
66 //
67 // fileDescriptor_<hash of filename>
68 filenameHash := sha256.Sum256([]byte(f.Desc.Path()))
69 f.descriptorVar = fmt.Sprintf("fileDescriptor_%s", hex.EncodeToString(filenameHash[:8]))
70
Damien Neil220c2022018-08-15 11:24:18 -070071 g.P("// Code generated by protoc-gen-go. DO NOT EDIT.")
Damien Neil55fe1c02018-09-17 15:11:24 -070072 if f.Proto.GetOptions().GetDeprecated() {
73 g.P("// ", f.Desc.Path(), " is a deprecated file.")
74 } else {
75 g.P("// source: ", f.Desc.Path())
76 }
Damien Neil220c2022018-08-15 11:24:18 -070077 g.P()
Damien Neilcab8dfe2018-09-06 14:51:28 -070078 const filePackageField = 2 // FileDescriptorProto.package
79 genComment(g, f, []int32{filePackageField})
80 g.P()
Damien Neil082ce922018-09-06 10:23:53 -070081 g.P("package ", f.GoPackageName)
Damien Neilc7d07d92018-08-22 13:46:02 -070082 g.P()
Damien Neil1ec33152018-09-13 13:12:36 -070083
84 // These references are not necessary, since we automatically add
85 // all necessary imports before formatting the generated file.
86 //
87 // This section exists to generate output more consistent with
88 // the previous version of protoc-gen-go, to make it easier to
89 // detect unintended variations.
90 //
91 // TODO: Eventually remove this.
92 g.P("// Reference imports to suppress errors if they are not otherwise used.")
93 g.P("var _ = ", protogen.GoIdent{GoImportPath: protoPackage, GoName: "Marshal"})
94 g.P("var _ = ", protogen.GoIdent{GoImportPath: "fmt", GoName: "Errorf"})
95 g.P("var _ = ", protogen.GoIdent{GoImportPath: "math", GoName: "Inf"})
96 g.P()
97
Damien Neild4127922018-09-12 11:13:49 -070098 g.P("// This is a compile-time assertion to ensure that this generated file")
99 g.P("// is compatible with the proto package it is being compiled against.")
100 g.P("// A compilation error at this line likely means your copy of the")
101 g.P("// proto package needs to be updated.")
102 g.P("const _ = ", protogen.GoIdent{
103 GoImportPath: protoPackage,
104 GoName: fmt.Sprintf("ProtoPackageIsVersion%d", generatedCodeVersion),
105 }, "// please upgrade the proto package")
106 g.P()
Damien Neilc7d07d92018-08-22 13:46:02 -0700107
Damien Neil73ac8852018-09-17 15:11:24 -0700108 for i, imps := 0, f.Desc.Imports(); i < imps.Len(); i++ {
109 genImport(gen, g, f, imps.Get(i))
110 }
Damien Neilce36f8d2018-09-13 15:19:08 -0700111 for _, enum := range f.allEnums {
Damien Neil46abb572018-09-07 12:45:37 -0700112 genEnum(gen, g, f, enum)
113 }
Damien Neilce36f8d2018-09-13 15:19:08 -0700114 for _, message := range f.allMessages {
Damien Neilcab8dfe2018-09-06 14:51:28 -0700115 genMessage(gen, g, f, message)
Damien Neilc7d07d92018-08-22 13:46:02 -0700116 }
Damien Neil993c04d2018-09-14 15:41:11 -0700117 for _, extension := range f.Extensions {
118 genExtension(gen, g, f, extension)
119 }
Damien Neil220c2022018-08-15 11:24:18 -0700120
Damien Neilce36f8d2018-09-13 15:19:08 -0700121 genInitFunction(gen, g, f)
Damien Neil7779e052018-09-07 14:14:06 -0700122 genFileDescriptor(gen, g, f)
123}
124
Damien Neil73ac8852018-09-17 15:11:24 -0700125// walkMessages calls f on each message and all of its descendants.
126func walkMessages(messages []*protogen.Message, f func(*protogen.Message)) {
127 for _, m := range messages {
128 f(m)
129 walkMessages(m.Messages, f)
130 }
131}
132
Damien Neild39efc82018-09-24 12:38:10 -0700133func genImport(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, imp protoreflect.FileImport) {
Damien Neil73ac8852018-09-17 15:11:24 -0700134 impFile, ok := gen.FileByName(imp.Path())
135 if !ok {
136 return
137 }
138 if impFile.GoImportPath == f.GoImportPath {
Damien Neil2e0c3da2018-09-19 12:51:36 -0700139 // Don't generate imports or aliases for types in the same Go package.
140 return
141 }
142 // Generate imports for all dependencies, even if they are not
143 // referenced, because other code and tools depend on having the
144 // full transitive closure of protocol buffer types in the binary.
145 g.Import(impFile.GoImportPath)
146 if !imp.IsPublic {
Damien Neil73ac8852018-09-17 15:11:24 -0700147 return
148 }
149 var enums []*protogen.Enum
150 enums = append(enums, impFile.Enums...)
151 walkMessages(impFile.Messages, func(message *protogen.Message) {
152 enums = append(enums, message.Enums...)
153 g.P("// ", message.GoIdent.GoName, " from public import ", imp.Path())
154 g.P("type ", message.GoIdent.GoName, " = ", message.GoIdent)
155 for _, oneof := range message.Oneofs {
156 for _, field := range oneof.Fields {
157 typ := fieldOneofType(field)
158 g.P("type ", typ.GoName, " = ", typ)
159 }
160 }
161 g.P()
162 })
163 for _, enum := range enums {
164 g.P("// ", enum.GoIdent.GoName, " from public import ", imp.Path())
165 g.P("type ", enum.GoIdent.GoName, " = ", enum.GoIdent)
166 g.P("var ", enum.GoIdent.GoName, "_name = ", enum.GoIdent, "_name")
167 g.P("var ", enum.GoIdent.GoName, "_value = ", enum.GoIdent, "_value")
168 g.P()
169 for _, value := range enum.Values {
170 g.P("const ", value.GoIdent.GoName, " = ", enum.GoIdent.GoName, "(", value.GoIdent, ")")
171 }
Damien Neilce36f8d2018-09-13 15:19:08 -0700172 }
173}
174
Damien Neild39efc82018-09-24 12:38:10 -0700175func genFileDescriptor(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) {
Damien Neil7779e052018-09-07 14:14:06 -0700176 // Trim the source_code_info from the descriptor.
177 // Marshal and gzip it.
178 descProto := proto.Clone(f.Proto).(*descpb.FileDescriptorProto)
179 descProto.SourceCodeInfo = nil
180 b, err := proto.Marshal(descProto)
181 if err != nil {
182 gen.Error(err)
183 return
184 }
185 var buf bytes.Buffer
186 w, _ := gzip.NewWriterLevel(&buf, gzip.BestCompression)
187 w.Write(b)
188 w.Close()
189 b = buf.Bytes()
190
Damien Neil46abb572018-09-07 12:45:37 -0700191 g.P("func init() { proto.RegisterFile(", strconv.Quote(f.Desc.Path()), ", ", f.descriptorVar, ") }")
Damien Neil7779e052018-09-07 14:14:06 -0700192 g.P()
Damien Neil46abb572018-09-07 12:45:37 -0700193 g.P("var ", f.descriptorVar, " = []byte{")
Damien Neil7779e052018-09-07 14:14:06 -0700194 g.P("// ", len(b), " bytes of a gzipped FileDescriptorProto")
195 for len(b) > 0 {
196 n := 16
197 if n > len(b) {
198 n = len(b)
199 }
200
201 s := ""
202 for _, c := range b[:n] {
203 s += fmt.Sprintf("0x%02x,", c)
204 }
205 g.P(s)
206
207 b = b[n:]
208 }
209 g.P("}")
210 g.P()
Damien Neil220c2022018-08-15 11:24:18 -0700211}
Damien Neilc7d07d92018-08-22 13:46:02 -0700212
Damien Neild39efc82018-09-24 12:38:10 -0700213func genEnum(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, enum *protogen.Enum) {
Damien Neil46abb572018-09-07 12:45:37 -0700214 genComment(g, f, enum.Path)
Damien Neil55fe1c02018-09-17 15:11:24 -0700215 g.P("type ", enum.GoIdent, " int32",
216 deprecationComment(enumOptions(gen, enum).GetDeprecated()))
Damien Neil46abb572018-09-07 12:45:37 -0700217 g.P("const (")
218 for _, value := range enum.Values {
219 genComment(g, f, value.Path)
Damien Neil55fe1c02018-09-17 15:11:24 -0700220 g.P(value.GoIdent, " ", enum.GoIdent, " = ", value.Desc.Number(),
221 deprecationComment(enumValueOptions(gen, value).GetDeprecated()))
Damien Neil46abb572018-09-07 12:45:37 -0700222 }
223 g.P(")")
224 g.P()
225 nameMap := enum.GoIdent.GoName + "_name"
226 g.P("var ", nameMap, " = map[int32]string{")
227 generated := make(map[protoreflect.EnumNumber]bool)
228 for _, value := range enum.Values {
229 duplicate := ""
230 if _, present := generated[value.Desc.Number()]; present {
231 duplicate = "// Duplicate value: "
232 }
233 g.P(duplicate, value.Desc.Number(), ": ", strconv.Quote(string(value.Desc.Name())), ",")
234 generated[value.Desc.Number()] = true
235 }
236 g.P("}")
237 g.P()
238 valueMap := enum.GoIdent.GoName + "_value"
239 g.P("var ", valueMap, " = map[string]int32{")
240 for _, value := range enum.Values {
241 g.P(strconv.Quote(string(value.Desc.Name())), ": ", value.Desc.Number(), ",")
242 }
243 g.P("}")
244 g.P()
245 if enum.Desc.Syntax() != protoreflect.Proto3 {
246 g.P("func (x ", enum.GoIdent, ") Enum() *", enum.GoIdent, " {")
247 g.P("p := new(", enum.GoIdent, ")")
248 g.P("*p = x")
249 g.P("return p")
250 g.P("}")
251 g.P()
252 }
253 g.P("func (x ", enum.GoIdent, ") String() string {")
254 g.P("return ", protogen.GoIdent{GoImportPath: protoPackage, GoName: "EnumName"}, "(", enum.GoIdent, "_name, int32(x))")
255 g.P("}")
256 g.P()
257
258 if enum.Desc.Syntax() != protoreflect.Proto3 {
259 g.P("func (x *", enum.GoIdent, ") UnmarshalJSON(data []byte) error {")
260 g.P("value, err := ", protogen.GoIdent{GoImportPath: protoPackage, GoName: "UnmarshalJSONEnum"}, "(", enum.GoIdent, `_value, data, "`, enum.GoIdent, `")`)
261 g.P("if err != nil {")
262 g.P("return err")
263 g.P("}")
264 g.P("*x = ", enum.GoIdent, "(value)")
265 g.P("return nil")
266 g.P("}")
267 g.P()
268 }
269
270 var indexes []string
271 for i := 1; i < len(enum.Path); i += 2 {
272 indexes = append(indexes, strconv.Itoa(int(enum.Path[i])))
273 }
274 g.P("func (", enum.GoIdent, ") EnumDescriptor() ([]byte, []int) {")
275 g.P("return ", f.descriptorVar, ", []int{", strings.Join(indexes, ","), "}")
276 g.P("}")
277 g.P()
278
Damien Neilea7baf42018-09-28 14:23:44 -0700279 genWellKnownType(g, "", enum.GoIdent, enum.Desc)
Damien Neil46abb572018-09-07 12:45:37 -0700280}
281
Damien Neil658051b2018-09-10 12:26:21 -0700282// enumRegistryName returns the name used to register an enum with the proto
283// package registry.
284//
285// Confusingly, this is <proto_package>.<go_ident>. This probably should have
286// been the full name of the proto enum type instead, but changing it at this
287// point would require thought.
288func enumRegistryName(enum *protogen.Enum) string {
289 // Find the FileDescriptor for this enum.
290 var desc protoreflect.Descriptor = enum.Desc
291 for {
292 p, ok := desc.Parent()
293 if !ok {
294 break
295 }
296 desc = p
297 }
298 fdesc := desc.(protoreflect.FileDescriptor)
Damien Neildaa4fad2018-10-08 14:08:27 -0700299 if fdesc.Package() == "" {
300 return enum.GoIdent.GoName
301 }
Damien Neil658051b2018-09-10 12:26:21 -0700302 return string(fdesc.Package()) + "." + enum.GoIdent.GoName
303}
304
Damien Neild39efc82018-09-24 12:38:10 -0700305func genMessage(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) {
Damien Neil0bd5a382018-09-13 15:07:10 -0700306 if message.Desc.IsMapEntry() {
307 return
308 }
309
Damien Neil55fe1c02018-09-17 15:11:24 -0700310 hasComment := genComment(g, f, message.Path)
311 if messageOptions(gen, message).GetDeprecated() {
312 if hasComment {
313 g.P("//")
314 }
315 g.P(deprecationComment(true))
316 }
Damien Neilcab8dfe2018-09-06 14:51:28 -0700317 g.P("type ", message.GoIdent, " struct {")
Damien Neil658051b2018-09-10 12:26:21 -0700318 for _, field := range message.Fields {
Damien Neil1fa78d82018-09-13 13:12:36 -0700319 if field.OneofType != nil {
320 // It would be a bit simpler to iterate over the oneofs below,
321 // but generating the field here keeps the contents of the Go
322 // struct in the same order as the contents of the source
323 // .proto file.
324 if field == field.OneofType.Fields[0] {
325 genOneofField(gen, g, f, message, field.OneofType)
326 }
Damien Neil658051b2018-09-10 12:26:21 -0700327 continue
328 }
329 genComment(g, f, field.Path)
Damien Neil77f82fe2018-09-13 10:59:17 -0700330 goType, pointer := fieldGoType(g, field)
331 if pointer {
332 goType = "*" + goType
333 }
Damien Neil0bd5a382018-09-13 15:07:10 -0700334 tags := []string{
335 fmt.Sprintf("protobuf:%q", fieldProtobufTag(field)),
336 fmt.Sprintf("json:%q", fieldJSONTag(field)),
337 }
338 if field.Desc.IsMap() {
339 key := field.MessageType.Fields[0]
340 val := field.MessageType.Fields[1]
341 tags = append(tags,
342 fmt.Sprintf("protobuf_key:%q", fieldProtobufTag(key)),
343 fmt.Sprintf("protobuf_val:%q", fieldProtobufTag(val)),
344 )
345 }
Damien Neil55fe1c02018-09-17 15:11:24 -0700346 g.P(field.GoName, " ", goType, " `", strings.Join(tags, " "), "`",
347 deprecationComment(fieldOptions(gen, field).GetDeprecated()))
Damien Neil658051b2018-09-10 12:26:21 -0700348 }
349 g.P("XXX_NoUnkeyedLiteral struct{} `json:\"-\"`")
Damien Neil993c04d2018-09-14 15:41:11 -0700350
351 if message.Desc.ExtensionRanges().Len() > 0 {
352 var tags []string
353 if messageOptions(gen, message).GetMessageSetWireFormat() {
354 tags = append(tags, `protobuf_messageset:"1"`)
355 }
356 tags = append(tags, `json:"-"`)
357 g.P(protogen.GoIdent{
358 GoImportPath: protoPackage,
359 GoName: "XXX_InternalExtensions",
360 }, " `", strings.Join(tags, " "), "`")
361 }
Damien Neil658051b2018-09-10 12:26:21 -0700362 // TODO XXX_InternalExtensions
363 g.P("XXX_unrecognized []byte `json:\"-\"`")
364 g.P("XXX_sizecache int32 `json:\"-\"`")
Damien Neilc7d07d92018-08-22 13:46:02 -0700365 g.P("}")
366 g.P()
367
Damien Neila1c6abc2018-09-12 13:36:34 -0700368 // Reset
369 g.P("func (m *", message.GoIdent, ") Reset() { *m = ", message.GoIdent, "{} }")
370 // String
371 g.P("func (m *", message.GoIdent, ") String() string { return ", protogen.GoIdent{
372 GoImportPath: protoPackage,
373 GoName: "CompactTextString",
374 }, "(m) }")
375 // ProtoMessage
376 g.P("func (*", message.GoIdent, ") ProtoMessage() {}")
377 // Descriptor
378 var indexes []string
379 for i := 1; i < len(message.Path); i += 2 {
380 indexes = append(indexes, strconv.Itoa(int(message.Path[i])))
381 }
382 g.P("func (*", message.GoIdent, ") Descriptor() ([]byte, []int) {")
383 g.P("return ", f.descriptorVar, ", []int{", strings.Join(indexes, ","), "}")
384 g.P("}")
Damien Neil993c04d2018-09-14 15:41:11 -0700385 g.P()
386
387 // ExtensionRangeArray
388 if extranges := message.Desc.ExtensionRanges(); extranges.Len() > 0 {
389 if messageOptions(gen, message).GetMessageSetWireFormat() {
390 g.P("func (m *", message.GoIdent, ") MarshalJSON() ([]byte, error) {")
391 g.P("return ", protogen.GoIdent{
392 GoImportPath: protoPackage,
393 GoName: "MarshalMessageSetJSON",
394 }, "(&m.XXX_InternalExtensions)")
395 g.P("}")
396 g.P("func (m *", message.GoIdent, ") UnmarshalJSON(buf []byte) error {")
397 g.P("return ", protogen.GoIdent{
398 GoImportPath: protoPackage,
399 GoName: "UnmarshalMessageSetJSON",
400 }, "(buf, &m.XXX_InternalExtensions)")
401 g.P("}")
402 g.P()
403 }
404
405 protoExtRange := protogen.GoIdent{
406 GoImportPath: protoPackage,
407 GoName: "ExtensionRange",
408 }
409 extRangeVar := "extRange_" + message.GoIdent.GoName
410 g.P("var ", extRangeVar, " = []", protoExtRange, " {")
411 for i := 0; i < extranges.Len(); i++ {
412 r := extranges.Get(i)
413 g.P("{Start:", r[0], ", End:", r[1]-1 /* inclusive */, "},")
414 }
415 g.P("}")
416 g.P()
417 g.P("func (*", message.GoIdent, ") ExtensionRangeArray() []", protoExtRange, " {")
418 g.P("return ", extRangeVar)
419 g.P("}")
420 g.P()
421 }
Damien Neila1c6abc2018-09-12 13:36:34 -0700422
Damien Neilea7baf42018-09-28 14:23:44 -0700423 genWellKnownType(g, "*", message.GoIdent, message.Desc)
424
Damien Neila1c6abc2018-09-12 13:36:34 -0700425 // Table-driven proto support.
426 //
427 // TODO: It does not scale to keep adding another method for every
428 // operation on protos that we want to switch over to using the
429 // table-driven approach. Instead, we should only add a single method
430 // that allows getting access to the *InternalMessageInfo struct and then
431 // calling Unmarshal, Marshal, Merge, Size, and Discard directly on that.
432 messageInfoVar := "xxx_messageInfo_" + message.GoIdent.GoName
433 // XXX_Unmarshal
434 g.P("func (m *", message.GoIdent, ") XXX_Unmarshal(b []byte) error {")
435 g.P("return ", messageInfoVar, ".Unmarshal(m, b)")
436 g.P("}")
437 // XXX_Marshal
438 g.P("func (m *", message.GoIdent, ") XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {")
439 g.P("return ", messageInfoVar, ".Marshal(b, m, deterministic)")
440 g.P("}")
441 // XXX_Merge
442 g.P("func (m *", message.GoIdent, ") XXX_Merge(src proto.Message) {")
443 g.P(messageInfoVar, ".Merge(m, src)")
444 g.P("}")
445 // XXX_Size
446 g.P("func (m *", message.GoIdent, ") XXX_Size() int {")
447 g.P("return ", messageInfoVar, ".Size(m)")
448 g.P("}")
449 // XXX_DiscardUnknown
450 g.P("func (m *", message.GoIdent, ") XXX_DiscardUnknown() {")
451 g.P(messageInfoVar, ".DiscardUnknown(m)")
452 g.P("}")
453 g.P()
454 g.P("var ", messageInfoVar, " ", protogen.GoIdent{
455 GoImportPath: protoPackage,
456 GoName: "InternalMessageInfo",
457 })
458 g.P()
459
Damien Neilebc699d2018-09-13 08:50:13 -0700460 // Constants and vars holding the default values of fields.
461 for _, field := range message.Fields {
Damien Neilccf3fa62018-09-28 14:41:45 -0700462 if !fieldHasDefault(field) {
Damien Neilebc699d2018-09-13 08:50:13 -0700463 continue
464 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700465 defVarName := "Default_" + message.GoIdent.GoName + "_" + field.GoName
Damien Neilebc699d2018-09-13 08:50:13 -0700466 def := field.Desc.Default()
467 switch field.Desc.Kind() {
468 case protoreflect.StringKind:
469 g.P("const ", defVarName, " string = ", strconv.Quote(def.String()))
470 case protoreflect.BytesKind:
471 g.P("var ", defVarName, " []byte = []byte(", strconv.Quote(string(def.Bytes())), ")")
472 case protoreflect.EnumKind:
473 enum := field.EnumType
474 evalue := enum.Values[enum.Desc.Values().ByNumber(def.Enum()).Index()]
475 g.P("const ", defVarName, " ", field.EnumType.GoIdent, " = ", evalue.GoIdent)
476 case protoreflect.FloatKind, protoreflect.DoubleKind:
477 // Floating point numbers need extra handling for -Inf/Inf/NaN.
478 f := field.Desc.Default().Float()
479 goType := "float64"
480 if field.Desc.Kind() == protoreflect.FloatKind {
481 goType = "float32"
482 }
483 // funcCall returns a call to a function in the math package,
484 // possibly converting the result to float32.
485 funcCall := func(fn, param string) string {
486 s := g.QualifiedGoIdent(protogen.GoIdent{
487 GoImportPath: "math",
488 GoName: fn,
489 }) + param
490 if goType != "float64" {
491 s = goType + "(" + s + ")"
492 }
493 return s
494 }
495 switch {
496 case math.IsInf(f, -1):
497 g.P("var ", defVarName, " ", goType, " = ", funcCall("Inf", "(-1)"))
498 case math.IsInf(f, 1):
499 g.P("var ", defVarName, " ", goType, " = ", funcCall("Inf", "(1)"))
500 case math.IsNaN(f):
501 g.P("var ", defVarName, " ", goType, " = ", funcCall("NaN", "()"))
502 default:
Damien Neil982684b2018-09-28 14:12:41 -0700503 g.P("const ", defVarName, " ", goType, " = ", field.Desc.Default().Interface())
Damien Neilebc699d2018-09-13 08:50:13 -0700504 }
505 default:
Damien Neil77f82fe2018-09-13 10:59:17 -0700506 goType, _ := fieldGoType(g, field)
Damien Neilebc699d2018-09-13 08:50:13 -0700507 g.P("const ", defVarName, " ", goType, " = ", def.Interface())
508 }
509 }
510 g.P()
511
Damien Neil77f82fe2018-09-13 10:59:17 -0700512 // Getters.
513 for _, field := range message.Fields {
Damien Neil1fa78d82018-09-13 13:12:36 -0700514 if field.OneofType != nil {
515 if field == field.OneofType.Fields[0] {
516 genOneofTypes(gen, g, f, message, field.OneofType)
517 }
518 }
Damien Neil77f82fe2018-09-13 10:59:17 -0700519 goType, pointer := fieldGoType(g, field)
520 defaultValue := fieldDefaultValue(g, message, field)
Damien Neil55fe1c02018-09-17 15:11:24 -0700521 if fieldOptions(gen, field).GetDeprecated() {
522 g.P(deprecationComment(true))
523 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700524 g.P("func (m *", message.GoIdent, ") Get", field.GoName, "() ", goType, " {")
525 if field.OneofType != nil {
Damien Neil81d6d832018-09-19 12:12:17 -0700526 g.P("if x, ok := m.Get", field.OneofType.GoName, "().(*", fieldOneofType(field), "); ok {")
Damien Neil1fa78d82018-09-13 13:12:36 -0700527 g.P("return x.", field.GoName)
528 g.P("}")
Damien Neil77f82fe2018-09-13 10:59:17 -0700529 } else {
Damien Neil1fa78d82018-09-13 13:12:36 -0700530 if field.Desc.Syntax() == protoreflect.Proto3 || defaultValue == "nil" {
531 g.P("if m != nil {")
532 } else {
533 g.P("if m != nil && m.", field.GoName, " != nil {")
534 }
535 star := ""
536 if pointer {
537 star = "*"
538 }
539 g.P("return ", star, " m.", field.GoName)
540 g.P("}")
Damien Neil77f82fe2018-09-13 10:59:17 -0700541 }
Damien Neil77f82fe2018-09-13 10:59:17 -0700542 g.P("return ", defaultValue)
543 g.P("}")
544 g.P()
545 }
Damien Neila1c6abc2018-09-12 13:36:34 -0700546
Damien Neil1fa78d82018-09-13 13:12:36 -0700547 if len(message.Oneofs) > 0 {
548 genOneofFuncs(gen, g, f, message)
549 }
Damien Neil993c04d2018-09-14 15:41:11 -0700550 for _, extension := range message.Extensions {
551 genExtension(gen, g, f, extension)
552 }
Damien Neilc7d07d92018-08-22 13:46:02 -0700553}
Damien Neilcab8dfe2018-09-06 14:51:28 -0700554
Damien Neil77f82fe2018-09-13 10:59:17 -0700555// fieldGoType returns the Go type used for a field.
556//
557// If it returns pointer=true, the struct field is a pointer to the type.
558func fieldGoType(g *protogen.GeneratedFile, field *protogen.Field) (goType string, pointer bool) {
Damien Neil77f82fe2018-09-13 10:59:17 -0700559 pointer = true
Damien Neil658051b2018-09-10 12:26:21 -0700560 switch field.Desc.Kind() {
561 case protoreflect.BoolKind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700562 goType = "bool"
Damien Neil658051b2018-09-10 12:26:21 -0700563 case protoreflect.EnumKind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700564 goType = g.QualifiedGoIdent(field.EnumType.GoIdent)
Damien Neil658051b2018-09-10 12:26:21 -0700565 case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700566 goType = "int32"
Damien Neil658051b2018-09-10 12:26:21 -0700567 case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700568 goType = "uint32"
Damien Neil658051b2018-09-10 12:26:21 -0700569 case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700570 goType = "int64"
Damien Neil658051b2018-09-10 12:26:21 -0700571 case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700572 goType = "uint64"
Damien Neil658051b2018-09-10 12:26:21 -0700573 case protoreflect.FloatKind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700574 goType = "float32"
Damien Neil658051b2018-09-10 12:26:21 -0700575 case protoreflect.DoubleKind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700576 goType = "float64"
Damien Neil658051b2018-09-10 12:26:21 -0700577 case protoreflect.StringKind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700578 goType = "string"
Damien Neil658051b2018-09-10 12:26:21 -0700579 case protoreflect.BytesKind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700580 goType = "[]byte"
581 pointer = false
Damien Neil658051b2018-09-10 12:26:21 -0700582 case protoreflect.MessageKind, protoreflect.GroupKind:
Damien Neil0bd5a382018-09-13 15:07:10 -0700583 if field.Desc.IsMap() {
584 keyType, _ := fieldGoType(g, field.MessageType.Fields[0])
585 valType, _ := fieldGoType(g, field.MessageType.Fields[1])
586 return fmt.Sprintf("map[%v]%v", keyType, valType), false
587 }
Damien Neil77f82fe2018-09-13 10:59:17 -0700588 goType = "*" + g.QualifiedGoIdent(field.MessageType.GoIdent)
589 pointer = false
Damien Neil658051b2018-09-10 12:26:21 -0700590 }
591 if field.Desc.Cardinality() == protoreflect.Repeated {
Damien Neil77f82fe2018-09-13 10:59:17 -0700592 goType = "[]" + goType
593 pointer = false
Damien Neil658051b2018-09-10 12:26:21 -0700594 }
595 if field.Desc.Syntax() == protoreflect.Proto3 {
Damien Neil77f82fe2018-09-13 10:59:17 -0700596 pointer = false
Damien Neil658051b2018-09-10 12:26:21 -0700597 }
Damien Neil77f82fe2018-09-13 10:59:17 -0700598 return goType, pointer
Damien Neil658051b2018-09-10 12:26:21 -0700599}
600
601func fieldProtobufTag(field *protogen.Field) string {
602 var tag []string
603 // wire type
604 tag = append(tag, wireTypes[field.Desc.Kind()])
605 // field number
606 tag = append(tag, strconv.Itoa(int(field.Desc.Number())))
607 // cardinality
608 switch field.Desc.Cardinality() {
609 case protoreflect.Optional:
610 tag = append(tag, "opt")
611 case protoreflect.Required:
612 tag = append(tag, "req")
613 case protoreflect.Repeated:
614 tag = append(tag, "rep")
615 }
Damien Neild4803f52018-09-19 11:43:35 -0700616 if field.Desc.IsPacked() {
617 tag = append(tag, "packed")
618 }
Damien Neil658051b2018-09-10 12:26:21 -0700619 // TODO: packed
620 // name
621 name := string(field.Desc.Name())
622 if field.Desc.Kind() == protoreflect.GroupKind {
623 // The name of the FieldDescriptor for a group field is
624 // lowercased. To find the original capitalization, we
625 // look in the field's MessageType.
626 name = string(field.MessageType.Desc.Name())
627 }
628 tag = append(tag, "name="+name)
629 // JSON name
630 if jsonName := field.Desc.JSONName(); jsonName != "" && jsonName != name {
631 tag = append(tag, "json="+jsonName)
632 }
633 // proto3
634 if field.Desc.Syntax() == protoreflect.Proto3 {
635 tag = append(tag, "proto3")
636 }
637 // enum
638 if field.Desc.Kind() == protoreflect.EnumKind {
639 tag = append(tag, "enum="+enumRegistryName(field.EnumType))
640 }
641 // oneof
642 if field.Desc.OneofType() != nil {
643 tag = append(tag, "oneof")
644 }
Damien Neilebc699d2018-09-13 08:50:13 -0700645 // default value
646 // This must appear last in the tag, since commas in strings aren't escaped.
647 if field.Desc.HasDefault() {
648 var def string
649 switch field.Desc.Kind() {
650 case protoreflect.BoolKind:
651 if field.Desc.Default().Bool() {
652 def = "1"
653 } else {
654 def = "0"
655 }
656 case protoreflect.BytesKind:
657 def = string(field.Desc.Default().Bytes())
658 case protoreflect.FloatKind, protoreflect.DoubleKind:
659 f := field.Desc.Default().Float()
660 switch {
661 case math.IsInf(f, -1):
662 def = "-inf"
663 case math.IsInf(f, 1):
664 def = "inf"
665 case math.IsNaN(f):
666 def = "nan"
667 default:
Damien Neil982684b2018-09-28 14:12:41 -0700668 def = fmt.Sprint(field.Desc.Default().Interface())
Damien Neilebc699d2018-09-13 08:50:13 -0700669 }
670 default:
671 def = fmt.Sprint(field.Desc.Default().Interface())
672 }
673 tag = append(tag, "def="+def)
674 }
Damien Neil658051b2018-09-10 12:26:21 -0700675 return strings.Join(tag, ",")
676}
677
Damien Neil77f82fe2018-09-13 10:59:17 -0700678func fieldDefaultValue(g *protogen.GeneratedFile, message *protogen.Message, field *protogen.Field) string {
679 if field.Desc.Cardinality() == protoreflect.Repeated {
680 return "nil"
681 }
Damien Neilccf3fa62018-09-28 14:41:45 -0700682 if fieldHasDefault(field) {
Damien Neil1fa78d82018-09-13 13:12:36 -0700683 defVarName := "Default_" + message.GoIdent.GoName + "_" + field.GoName
Damien Neil77f82fe2018-09-13 10:59:17 -0700684 if field.Desc.Kind() == protoreflect.BytesKind {
685 return "append([]byte(nil), " + defVarName + "...)"
686 }
687 return defVarName
688 }
689 switch field.Desc.Kind() {
690 case protoreflect.BoolKind:
691 return "false"
692 case protoreflect.StringKind:
693 return `""`
694 case protoreflect.MessageKind, protoreflect.GroupKind, protoreflect.BytesKind:
695 return "nil"
696 case protoreflect.EnumKind:
697 return g.QualifiedGoIdent(field.EnumType.Values[0].GoIdent)
698 default:
699 return "0"
700 }
701}
702
Damien Neilccf3fa62018-09-28 14:41:45 -0700703// fieldHasDefault returns true if we consider a field to have a default value.
704//
705// For consistency with the previous generator, it returns false for fields with
706// [default=""], preventing the generation of a default const or var for these
707// fields.
708//
709// TODO: Drop this special case.
710func fieldHasDefault(field *protogen.Field) bool {
711 if !field.Desc.HasDefault() {
712 return false
713 }
714 switch field.Desc.Kind() {
715 case protoreflect.StringKind:
716 return field.Desc.Default().String() != ""
717 case protoreflect.BytesKind:
718 return len(field.Desc.Default().Bytes()) > 0
719 }
720 return true
721}
722
Damien Neil658051b2018-09-10 12:26:21 -0700723var wireTypes = map[protoreflect.Kind]string{
724 protoreflect.BoolKind: "varint",
725 protoreflect.EnumKind: "varint",
726 protoreflect.Int32Kind: "varint",
727 protoreflect.Sint32Kind: "zigzag32",
728 protoreflect.Uint32Kind: "varint",
729 protoreflect.Int64Kind: "varint",
730 protoreflect.Sint64Kind: "zigzag64",
731 protoreflect.Uint64Kind: "varint",
732 protoreflect.Sfixed32Kind: "fixed32",
733 protoreflect.Fixed32Kind: "fixed32",
734 protoreflect.FloatKind: "fixed32",
735 protoreflect.Sfixed64Kind: "fixed64",
736 protoreflect.Fixed64Kind: "fixed64",
737 protoreflect.DoubleKind: "fixed64",
738 protoreflect.StringKind: "bytes",
739 protoreflect.BytesKind: "bytes",
740 protoreflect.MessageKind: "bytes",
741 protoreflect.GroupKind: "group",
742}
743
744func fieldJSONTag(field *protogen.Field) string {
745 return string(field.Desc.Name()) + ",omitempty"
746}
747
Damien Neild39efc82018-09-24 12:38:10 -0700748func genExtension(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, extension *protogen.Extension) {
Damien Neil154da982018-09-19 13:21:58 -0700749 // Special case for proto2 message sets: If this extension is extending
750 // proto2.bridge.MessageSet, and its final name component is "message_set_extension",
751 // then drop that last component.
752 //
753 // TODO: This should be implemented in the text formatter rather than the generator.
754 // In addition, the situation for when to apply this special case is implemented
755 // differently in other languages:
756 // https://github.com/google/protobuf/blob/aff10976/src/google/protobuf/text_format.cc#L1560
757 name := extension.Desc.FullName()
758 if isExtensionMessageSetElement(gen, extension) {
759 name = name.Parent()
760 }
761
Damien Neil993c04d2018-09-14 15:41:11 -0700762 g.P("var ", extensionVar(f, extension), " = &", protogen.GoIdent{
763 GoImportPath: protoPackage,
764 GoName: "ExtensionDesc",
765 }, "{")
766 g.P("ExtendedType: (*", extension.ExtendedType.GoIdent, ")(nil),")
767 goType, pointer := fieldGoType(g, extension)
768 if pointer {
769 goType = "*" + goType
770 }
771 g.P("ExtensionType: (", goType, ")(nil),")
772 g.P("Field: ", extension.Desc.Number(), ",")
Damien Neil154da982018-09-19 13:21:58 -0700773 g.P("Name: ", strconv.Quote(string(name)), ",")
Damien Neil993c04d2018-09-14 15:41:11 -0700774 g.P("Tag: ", strconv.Quote(fieldProtobufTag(extension)), ",")
775 g.P("Filename: ", strconv.Quote(f.Desc.Path()), ",")
776 g.P("}")
777 g.P()
778}
779
Damien Neil154da982018-09-19 13:21:58 -0700780func isExtensionMessageSetElement(gen *protogen.Plugin, extension *protogen.Extension) bool {
781 return extension.ParentMessage != nil &&
782 messageOptions(gen, extension.ExtendedType).GetMessageSetWireFormat() &&
783 extension.Desc.Name() == "message_set_extension"
784}
785
Damien Neil993c04d2018-09-14 15:41:11 -0700786// extensionVar returns the var holding the ExtensionDesc for an extension.
Damien Neild39efc82018-09-24 12:38:10 -0700787func extensionVar(f *fileInfo, extension *protogen.Extension) protogen.GoIdent {
Damien Neil993c04d2018-09-14 15:41:11 -0700788 name := "E_"
789 if extension.ParentMessage != nil {
790 name += extension.ParentMessage.GoIdent.GoName + "_"
791 }
792 name += extension.GoName
793 return protogen.GoIdent{
794 GoImportPath: f.GoImportPath,
795 GoName: name,
796 }
797}
798
Damien Neilce36f8d2018-09-13 15:19:08 -0700799// genInitFunction generates an init function that registers the types in the
800// generated file with the proto package.
Damien Neild39efc82018-09-24 12:38:10 -0700801func genInitFunction(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) {
Damien Neil993c04d2018-09-14 15:41:11 -0700802 if len(f.allMessages) == 0 && len(f.allEnums) == 0 && len(f.allExtensions) == 0 {
Damien Neilce36f8d2018-09-13 15:19:08 -0700803 return
804 }
805
806 g.P("func init() {")
Damien Neil154da982018-09-19 13:21:58 -0700807 for _, enum := range f.allEnums {
808 name := enum.GoIdent.GoName
809 g.P(protogen.GoIdent{
810 GoImportPath: protoPackage,
811 GoName: "RegisterEnum",
812 }, fmt.Sprintf("(%q, %s_name, %s_value)", enumRegistryName(enum), name, name))
813 }
Damien Neilce36f8d2018-09-13 15:19:08 -0700814 for _, message := range f.allMessages {
815 if message.Desc.IsMapEntry() {
816 continue
817 }
818
Damien Neil154da982018-09-19 13:21:58 -0700819 for _, extension := range message.Extensions {
820 genRegisterExtension(gen, g, f, extension)
821 }
822
Damien Neilce36f8d2018-09-13 15:19:08 -0700823 name := message.GoIdent.GoName
824 g.P(protogen.GoIdent{
825 GoImportPath: protoPackage,
826 GoName: "RegisterType",
827 }, fmt.Sprintf("((*%s)(nil), %q)", name, message.Desc.FullName()))
828
829 // Types of map fields, sorted by the name of the field message type.
830 var mapFields []*protogen.Field
831 for _, field := range message.Fields {
832 if field.Desc.IsMap() {
833 mapFields = append(mapFields, field)
834 }
835 }
836 sort.Slice(mapFields, func(i, j int) bool {
837 ni := mapFields[i].MessageType.Desc.FullName()
838 nj := mapFields[j].MessageType.Desc.FullName()
839 return ni < nj
840 })
841 for _, field := range mapFields {
842 typeName := string(field.MessageType.Desc.FullName())
843 goType, _ := fieldGoType(g, field)
844 g.P(protogen.GoIdent{
845 GoImportPath: protoPackage,
846 GoName: "RegisterMapType",
847 }, fmt.Sprintf("((%v)(nil), %q)", goType, typeName))
848 }
849 }
Damien Neil154da982018-09-19 13:21:58 -0700850 for _, extension := range f.Extensions {
851 genRegisterExtension(gen, g, f, extension)
Damien Neil993c04d2018-09-14 15:41:11 -0700852 }
Damien Neilce36f8d2018-09-13 15:19:08 -0700853 g.P("}")
854 g.P()
855}
856
Damien Neild39efc82018-09-24 12:38:10 -0700857func genRegisterExtension(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, extension *protogen.Extension) {
Damien Neil154da982018-09-19 13:21:58 -0700858 g.P(protogen.GoIdent{
859 GoImportPath: protoPackage,
860 GoName: "RegisterExtension",
861 }, "(", extensionVar(f, extension), ")")
862 if isExtensionMessageSetElement(gen, extension) {
863 goType, pointer := fieldGoType(g, extension)
864 if pointer {
865 goType = "*" + goType
866 }
867 g.P(protogen.GoIdent{
868 GoImportPath: protoPackage,
869 GoName: "RegisterMessageSetType",
870 }, "((", goType, ")(nil), ", extension.Desc.Number(), ",", strconv.Quote(string(extension.Desc.FullName().Parent())), ")")
871 }
872}
873
Damien Neild39efc82018-09-24 12:38:10 -0700874func genComment(g *protogen.GeneratedFile, f *fileInfo, path []int32) (hasComment bool) {
Damien Neilcab8dfe2018-09-06 14:51:28 -0700875 for _, loc := range f.locationMap[pathKey(path)] {
876 if loc.LeadingComments == nil {
877 continue
878 }
879 for _, line := range strings.Split(strings.TrimSuffix(loc.GetLeadingComments(), "\n"), "\n") {
Damien Neil1fa78d82018-09-13 13:12:36 -0700880 hasComment = true
Damien Neilcab8dfe2018-09-06 14:51:28 -0700881 g.P("//", line)
882 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700883 break
Damien Neilcab8dfe2018-09-06 14:51:28 -0700884 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700885 return hasComment
Damien Neilcab8dfe2018-09-06 14:51:28 -0700886}
887
Damien Neil55fe1c02018-09-17 15:11:24 -0700888// deprecationComment returns a standard deprecation comment if deprecated is true.
889func deprecationComment(deprecated bool) string {
890 if !deprecated {
891 return ""
892 }
893 return "// Deprecated: Do not use."
894}
895
Damien Neilcab8dfe2018-09-06 14:51:28 -0700896// pathKey converts a location path to a string suitable for use as a map key.
897func pathKey(path []int32) string {
898 var buf []byte
899 for i, x := range path {
900 if i != 0 {
901 buf = append(buf, ',')
902 }
903 buf = strconv.AppendInt(buf, int64(x), 10)
904 }
905 return string(buf)
906}
Damien Neil46abb572018-09-07 12:45:37 -0700907
Damien Neilea7baf42018-09-28 14:23:44 -0700908func genWellKnownType(g *protogen.GeneratedFile, ptr string, ident protogen.GoIdent, desc protoreflect.Descriptor) {
Damien Neil46abb572018-09-07 12:45:37 -0700909 if wellKnownTypes[desc.FullName()] {
Damien Neilea7baf42018-09-28 14:23:44 -0700910 g.P("func (", ptr, ident, `) XXX_WellKnownType() string { return "`, desc.Name(), `" }`)
Damien Neil46abb572018-09-07 12:45:37 -0700911 g.P()
912 }
913}
914
915// Names of messages and enums for which we will generate XXX_WellKnownType methods.
916var wellKnownTypes = map[protoreflect.FullName]bool{
Damien Neilea7baf42018-09-28 14:23:44 -0700917 "google.protobuf.Any": true,
918 "google.protobuf.Duration": true,
919 "google.protobuf.Empty": true,
920 "google.protobuf.Struct": true,
921 "google.protobuf.Timestamp": true,
922
923 "google.protobuf.BoolValue": true,
924 "google.protobuf.BytesValue": true,
925 "google.protobuf.DoubleValue": true,
926 "google.protobuf.FloatValue": true,
927 "google.protobuf.Int32Value": true,
928 "google.protobuf.Int64Value": true,
929 "google.protobuf.ListValue": true,
930 "google.protobuf.NullValue": true,
931 "google.protobuf.StringValue": true,
932 "google.protobuf.UInt32Value": true,
933 "google.protobuf.UInt64Value": true,
934 "google.protobuf.Value": true,
Damien Neil46abb572018-09-07 12:45:37 -0700935}