blob: 8bc1b816c90a109f84a139cbaf3ffb8ff2f066ff [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 Tsai05828db2018-11-01 13:52:16 -070021 "github.com/golang/protobuf/v2/internal/encoding/tag"
Joe Tsai01ab2962018-09-21 17:44:00 -070022 "github.com/golang/protobuf/v2/protogen"
23 "github.com/golang/protobuf/v2/reflect/protoreflect"
Damien Neil220c2022018-08-15 11:24:18 -070024)
25
Damien Neild4127922018-09-12 11:13:49 -070026// generatedCodeVersion indicates a version of the generated code.
27// It is incremented whenever an incompatibility between the generated code and
28// proto package is introduced; the generated code references
29// a constant, proto.ProtoPackageIsVersionN (where N is generatedCodeVersion).
30const generatedCodeVersion = 2
31
Damien Neil46abb572018-09-07 12:45:37 -070032const protoPackage = "github.com/golang/protobuf/proto"
33
Damien Neild39efc82018-09-24 12:38:10 -070034type fileInfo struct {
Damien Neilcab8dfe2018-09-06 14:51:28 -070035 *protogen.File
Damien Neil46abb572018-09-07 12:45:37 -070036 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 Neilba1159f2018-10-17 12:53:18 -070045 File: file,
Damien Neilcab8dfe2018-09-06 14:51:28 -070046 }
47
Damien Neil993c04d2018-09-14 15:41:11 -070048 // The different order for enums and extensions is to match the output
49 // of the previous implementation.
50 //
51 // TODO: Eventually make this consistent.
Damien Neilce36f8d2018-09-13 15:19:08 -070052 f.allEnums = append(f.allEnums, f.File.Enums...)
Damien Neil73ac8852018-09-17 15:11:24 -070053 walkMessages(f.Messages, func(message *protogen.Message) {
54 f.allMessages = append(f.allMessages, message)
55 f.allEnums = append(f.allEnums, message.Enums...)
56 f.allExtensions = append(f.allExtensions, message.Extensions...)
57 })
Damien Neil993c04d2018-09-14 15:41:11 -070058 f.allExtensions = append(f.allExtensions, f.File.Extensions...)
Damien Neilce36f8d2018-09-13 15:19:08 -070059
Damien Neil46abb572018-09-07 12:45:37 -070060 // Determine the name of the var holding the file descriptor:
61 //
62 // fileDescriptor_<hash of filename>
63 filenameHash := sha256.Sum256([]byte(f.Desc.Path()))
64 f.descriptorVar = fmt.Sprintf("fileDescriptor_%s", hex.EncodeToString(filenameHash[:8]))
65
Damien Neil220c2022018-08-15 11:24:18 -070066 g.P("// Code generated by protoc-gen-go. DO NOT EDIT.")
Damien Neil55fe1c02018-09-17 15:11:24 -070067 if f.Proto.GetOptions().GetDeprecated() {
68 g.P("// ", f.Desc.Path(), " is a deprecated file.")
69 } else {
70 g.P("// source: ", f.Desc.Path())
71 }
Damien Neil220c2022018-08-15 11:24:18 -070072 g.P()
Damien Neilcab8dfe2018-09-06 14:51:28 -070073 const filePackageField = 2 // FileDescriptorProto.package
Damien Neilba1159f2018-10-17 12:53:18 -070074 g.PrintLeadingComments(protogen.Location{
75 SourceFile: f.Proto.GetName(),
76 Path: []int32{filePackageField},
77 })
Damien Neilcab8dfe2018-09-06 14:51:28 -070078 g.P()
Damien Neil082ce922018-09-06 10:23:53 -070079 g.P("package ", f.GoPackageName)
Damien Neilc7d07d92018-08-22 13:46:02 -070080 g.P()
Damien Neil1ec33152018-09-13 13:12:36 -070081
82 // These references are not necessary, since we automatically add
83 // all necessary imports before formatting the generated file.
84 //
85 // This section exists to generate output more consistent with
86 // the previous version of protoc-gen-go, to make it easier to
87 // detect unintended variations.
88 //
89 // TODO: Eventually remove this.
90 g.P("// Reference imports to suppress errors if they are not otherwise used.")
91 g.P("var _ = ", protogen.GoIdent{GoImportPath: protoPackage, GoName: "Marshal"})
92 g.P("var _ = ", protogen.GoIdent{GoImportPath: "fmt", GoName: "Errorf"})
93 g.P("var _ = ", protogen.GoIdent{GoImportPath: "math", GoName: "Inf"})
94 g.P()
95
Damien Neild4127922018-09-12 11:13:49 -070096 g.P("// This is a compile-time assertion to ensure that this generated file")
97 g.P("// is compatible with the proto package it is being compiled against.")
98 g.P("// A compilation error at this line likely means your copy of the")
99 g.P("// proto package needs to be updated.")
100 g.P("const _ = ", protogen.GoIdent{
101 GoImportPath: protoPackage,
102 GoName: fmt.Sprintf("ProtoPackageIsVersion%d", generatedCodeVersion),
103 }, "// please upgrade the proto package")
104 g.P()
Damien Neilc7d07d92018-08-22 13:46:02 -0700105
Damien Neil73ac8852018-09-17 15:11:24 -0700106 for i, imps := 0, f.Desc.Imports(); i < imps.Len(); i++ {
107 genImport(gen, g, f, imps.Get(i))
108 }
Damien Neilce36f8d2018-09-13 15:19:08 -0700109 for _, enum := range f.allEnums {
Damien Neil46abb572018-09-07 12:45:37 -0700110 genEnum(gen, g, f, enum)
111 }
Damien Neilce36f8d2018-09-13 15:19:08 -0700112 for _, message := range f.allMessages {
Damien Neilcab8dfe2018-09-06 14:51:28 -0700113 genMessage(gen, g, f, message)
Damien Neilc7d07d92018-08-22 13:46:02 -0700114 }
Damien Neil993c04d2018-09-14 15:41:11 -0700115 for _, extension := range f.Extensions {
116 genExtension(gen, g, f, extension)
117 }
Damien Neil220c2022018-08-15 11:24:18 -0700118
Damien Neilce36f8d2018-09-13 15:19:08 -0700119 genInitFunction(gen, g, f)
Damien Neil7779e052018-09-07 14:14:06 -0700120 genFileDescriptor(gen, g, f)
121}
122
Damien Neil73ac8852018-09-17 15:11:24 -0700123// walkMessages calls f on each message and all of its descendants.
124func walkMessages(messages []*protogen.Message, f func(*protogen.Message)) {
125 for _, m := range messages {
126 f(m)
127 walkMessages(m.Messages, f)
128 }
129}
130
Damien Neild39efc82018-09-24 12:38:10 -0700131func genImport(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, imp protoreflect.FileImport) {
Damien Neil73ac8852018-09-17 15:11:24 -0700132 impFile, ok := gen.FileByName(imp.Path())
133 if !ok {
134 return
135 }
136 if impFile.GoImportPath == f.GoImportPath {
Damien Neil2e0c3da2018-09-19 12:51:36 -0700137 // Don't generate imports or aliases for types in the same Go package.
138 return
139 }
Damien Neil40a08052018-10-29 09:07:41 -0700140 // Generate imports for all non-weak dependencies, even if they are not
Damien Neil2e0c3da2018-09-19 12:51:36 -0700141 // referenced, because other code and tools depend on having the
142 // full transitive closure of protocol buffer types in the binary.
Damien Neil40a08052018-10-29 09:07:41 -0700143 if !imp.IsWeak {
144 g.Import(impFile.GoImportPath)
145 }
Damien Neil2e0c3da2018-09-19 12:51:36 -0700146 if !imp.IsPublic {
Damien Neil73ac8852018-09-17 15:11:24 -0700147 return
148 }
Damien Neil2193e8d2018-10-09 12:49:13 -0700149 // TODO: An alternate approach to generating public imports might be
150 // to generate the imported file contents, parse it, and extract all
151 // exported identifiers from the AST to build a list of forwarding
152 // declarations.
153 //
154 // TODO: Consider whether this should generate recursive aliases. e.g.,
155 // if a.proto publicly imports b.proto publicly imports c.proto, should
156 // a.pb.go contain aliases for symbols defined in c.proto?
Damien Neil73ac8852018-09-17 15:11:24 -0700157 var enums []*protogen.Enum
158 enums = append(enums, impFile.Enums...)
159 walkMessages(impFile.Messages, func(message *protogen.Message) {
Damien Neil2193e8d2018-10-09 12:49:13 -0700160 if message.Desc.IsMapEntry() {
161 return
162 }
Damien Neil73ac8852018-09-17 15:11:24 -0700163 enums = append(enums, message.Enums...)
Damien Neil2193e8d2018-10-09 12:49:13 -0700164 for _, field := range message.Fields {
165 if !fieldHasDefault(field) {
166 continue
167 }
168 defVar := protogen.GoIdent{
169 GoImportPath: message.GoIdent.GoImportPath,
170 GoName: "Default_" + message.GoIdent.GoName + "_" + field.GoName,
171 }
172 decl := "const"
173 if field.Desc.Kind() == protoreflect.BytesKind {
174 decl = "var"
175 }
176 g.P(decl, " ", defVar.GoName, " = ", defVar)
177 }
Damien Neil73ac8852018-09-17 15:11:24 -0700178 g.P("// ", message.GoIdent.GoName, " from public import ", imp.Path())
179 g.P("type ", message.GoIdent.GoName, " = ", message.GoIdent)
180 for _, oneof := range message.Oneofs {
181 for _, field := range oneof.Fields {
182 typ := fieldOneofType(field)
183 g.P("type ", typ.GoName, " = ", typ)
184 }
185 }
186 g.P()
187 })
188 for _, enum := range enums {
189 g.P("// ", enum.GoIdent.GoName, " from public import ", imp.Path())
190 g.P("type ", enum.GoIdent.GoName, " = ", enum.GoIdent)
191 g.P("var ", enum.GoIdent.GoName, "_name = ", enum.GoIdent, "_name")
192 g.P("var ", enum.GoIdent.GoName, "_value = ", enum.GoIdent, "_value")
193 g.P()
194 for _, value := range enum.Values {
195 g.P("const ", value.GoIdent.GoName, " = ", enum.GoIdent.GoName, "(", value.GoIdent, ")")
196 }
Damien Neilce36f8d2018-09-13 15:19:08 -0700197 }
Damien Neil6b541312018-10-29 09:14:14 -0700198 for _, ext := range impFile.Extensions {
199 ident := extensionVar(impFile, ext)
200 g.P("var ", ident.GoName, " = ", ident)
201 g.P()
202 }
Damien Neil2193e8d2018-10-09 12:49:13 -0700203 g.P()
Damien Neilce36f8d2018-09-13 15:19:08 -0700204}
205
Damien Neild39efc82018-09-24 12:38:10 -0700206func genFileDescriptor(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) {
Damien Neil7779e052018-09-07 14:14:06 -0700207 // Trim the source_code_info from the descriptor.
208 // Marshal and gzip it.
209 descProto := proto.Clone(f.Proto).(*descpb.FileDescriptorProto)
210 descProto.SourceCodeInfo = nil
211 b, err := proto.Marshal(descProto)
212 if err != nil {
213 gen.Error(err)
214 return
215 }
216 var buf bytes.Buffer
217 w, _ := gzip.NewWriterLevel(&buf, gzip.BestCompression)
218 w.Write(b)
219 w.Close()
220 b = buf.Bytes()
221
Damien Neil46abb572018-09-07 12:45:37 -0700222 g.P("func init() { proto.RegisterFile(", strconv.Quote(f.Desc.Path()), ", ", f.descriptorVar, ") }")
Damien Neil7779e052018-09-07 14:14:06 -0700223 g.P()
Damien Neil46abb572018-09-07 12:45:37 -0700224 g.P("var ", f.descriptorVar, " = []byte{")
Damien Neil7779e052018-09-07 14:14:06 -0700225 g.P("// ", len(b), " bytes of a gzipped FileDescriptorProto")
226 for len(b) > 0 {
227 n := 16
228 if n > len(b) {
229 n = len(b)
230 }
231
232 s := ""
233 for _, c := range b[:n] {
234 s += fmt.Sprintf("0x%02x,", c)
235 }
236 g.P(s)
237
238 b = b[n:]
239 }
240 g.P("}")
241 g.P()
Damien Neil220c2022018-08-15 11:24:18 -0700242}
Damien Neilc7d07d92018-08-22 13:46:02 -0700243
Damien Neild39efc82018-09-24 12:38:10 -0700244func genEnum(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, enum *protogen.Enum) {
Damien Neilba1159f2018-10-17 12:53:18 -0700245 g.PrintLeadingComments(enum.Location)
Damien Neil162c1272018-10-04 12:42:37 -0700246 g.Annotate(enum.GoIdent.GoName, enum.Location)
Damien Neil55fe1c02018-09-17 15:11:24 -0700247 g.P("type ", enum.GoIdent, " int32",
Damien Neil204f1c02018-10-23 15:03:38 -0700248 deprecationComment(enum.Desc.Options().(*descpb.EnumOptions).GetDeprecated()))
Damien Neil46abb572018-09-07 12:45:37 -0700249 g.P("const (")
250 for _, value := range enum.Values {
Damien Neilba1159f2018-10-17 12:53:18 -0700251 g.PrintLeadingComments(value.Location)
Damien Neil162c1272018-10-04 12:42:37 -0700252 g.Annotate(value.GoIdent.GoName, value.Location)
Damien Neil55fe1c02018-09-17 15:11:24 -0700253 g.P(value.GoIdent, " ", enum.GoIdent, " = ", value.Desc.Number(),
Damien Neil204f1c02018-10-23 15:03:38 -0700254 deprecationComment(value.Desc.Options().(*descpb.EnumValueOptions).GetDeprecated()))
Damien Neil46abb572018-09-07 12:45:37 -0700255 }
256 g.P(")")
257 g.P()
258 nameMap := enum.GoIdent.GoName + "_name"
259 g.P("var ", nameMap, " = map[int32]string{")
260 generated := make(map[protoreflect.EnumNumber]bool)
261 for _, value := range enum.Values {
262 duplicate := ""
263 if _, present := generated[value.Desc.Number()]; present {
264 duplicate = "// Duplicate value: "
265 }
266 g.P(duplicate, value.Desc.Number(), ": ", strconv.Quote(string(value.Desc.Name())), ",")
267 generated[value.Desc.Number()] = true
268 }
269 g.P("}")
270 g.P()
271 valueMap := enum.GoIdent.GoName + "_value"
272 g.P("var ", valueMap, " = map[string]int32{")
273 for _, value := range enum.Values {
274 g.P(strconv.Quote(string(value.Desc.Name())), ": ", value.Desc.Number(), ",")
275 }
276 g.P("}")
277 g.P()
278 if enum.Desc.Syntax() != protoreflect.Proto3 {
279 g.P("func (x ", enum.GoIdent, ") Enum() *", enum.GoIdent, " {")
280 g.P("p := new(", enum.GoIdent, ")")
281 g.P("*p = x")
282 g.P("return p")
283 g.P("}")
284 g.P()
285 }
286 g.P("func (x ", enum.GoIdent, ") String() string {")
287 g.P("return ", protogen.GoIdent{GoImportPath: protoPackage, GoName: "EnumName"}, "(", enum.GoIdent, "_name, int32(x))")
288 g.P("}")
289 g.P()
290
291 if enum.Desc.Syntax() != protoreflect.Proto3 {
292 g.P("func (x *", enum.GoIdent, ") UnmarshalJSON(data []byte) error {")
293 g.P("value, err := ", protogen.GoIdent{GoImportPath: protoPackage, GoName: "UnmarshalJSONEnum"}, "(", enum.GoIdent, `_value, data, "`, enum.GoIdent, `")`)
294 g.P("if err != nil {")
295 g.P("return err")
296 g.P("}")
297 g.P("*x = ", enum.GoIdent, "(value)")
298 g.P("return nil")
299 g.P("}")
300 g.P()
301 }
302
303 var indexes []string
Damien Neil162c1272018-10-04 12:42:37 -0700304 for i := 1; i < len(enum.Location.Path); i += 2 {
305 indexes = append(indexes, strconv.Itoa(int(enum.Location.Path[i])))
Damien Neil46abb572018-09-07 12:45:37 -0700306 }
307 g.P("func (", enum.GoIdent, ") EnumDescriptor() ([]byte, []int) {")
308 g.P("return ", f.descriptorVar, ", []int{", strings.Join(indexes, ","), "}")
309 g.P("}")
310 g.P()
311
Damien Neilea7baf42018-09-28 14:23:44 -0700312 genWellKnownType(g, "", enum.GoIdent, enum.Desc)
Damien Neil46abb572018-09-07 12:45:37 -0700313}
314
Damien Neil658051b2018-09-10 12:26:21 -0700315// enumRegistryName returns the name used to register an enum with the proto
316// package registry.
317//
318// Confusingly, this is <proto_package>.<go_ident>. This probably should have
319// been the full name of the proto enum type instead, but changing it at this
320// point would require thought.
321func enumRegistryName(enum *protogen.Enum) string {
322 // Find the FileDescriptor for this enum.
323 var desc protoreflect.Descriptor = enum.Desc
324 for {
325 p, ok := desc.Parent()
326 if !ok {
327 break
328 }
329 desc = p
330 }
331 fdesc := desc.(protoreflect.FileDescriptor)
Damien Neildaa4fad2018-10-08 14:08:27 -0700332 if fdesc.Package() == "" {
333 return enum.GoIdent.GoName
334 }
Damien Neil658051b2018-09-10 12:26:21 -0700335 return string(fdesc.Package()) + "." + enum.GoIdent.GoName
336}
337
Damien Neild39efc82018-09-24 12:38:10 -0700338func genMessage(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) {
Damien Neil0bd5a382018-09-13 15:07:10 -0700339 if message.Desc.IsMapEntry() {
340 return
341 }
342
Damien Neilba1159f2018-10-17 12:53:18 -0700343 hasComment := g.PrintLeadingComments(message.Location)
Damien Neil204f1c02018-10-23 15:03:38 -0700344 if message.Desc.Options().(*descpb.MessageOptions).GetDeprecated() {
Damien Neil55fe1c02018-09-17 15:11:24 -0700345 if hasComment {
346 g.P("//")
347 }
348 g.P(deprecationComment(true))
349 }
Damien Neil162c1272018-10-04 12:42:37 -0700350 g.Annotate(message.GoIdent.GoName, message.Location)
Damien Neilcab8dfe2018-09-06 14:51:28 -0700351 g.P("type ", message.GoIdent, " struct {")
Damien Neil658051b2018-09-10 12:26:21 -0700352 for _, field := range message.Fields {
Damien Neil1fa78d82018-09-13 13:12:36 -0700353 if field.OneofType != nil {
354 // It would be a bit simpler to iterate over the oneofs below,
355 // but generating the field here keeps the contents of the Go
356 // struct in the same order as the contents of the source
357 // .proto file.
358 if field == field.OneofType.Fields[0] {
359 genOneofField(gen, g, f, message, field.OneofType)
360 }
Damien Neil658051b2018-09-10 12:26:21 -0700361 continue
362 }
Damien Neilba1159f2018-10-17 12:53:18 -0700363 g.PrintLeadingComments(field.Location)
Damien Neil77f82fe2018-09-13 10:59:17 -0700364 goType, pointer := fieldGoType(g, field)
365 if pointer {
366 goType = "*" + goType
367 }
Damien Neil0bd5a382018-09-13 15:07:10 -0700368 tags := []string{
369 fmt.Sprintf("protobuf:%q", fieldProtobufTag(field)),
370 fmt.Sprintf("json:%q", fieldJSONTag(field)),
371 }
372 if field.Desc.IsMap() {
373 key := field.MessageType.Fields[0]
374 val := field.MessageType.Fields[1]
375 tags = append(tags,
376 fmt.Sprintf("protobuf_key:%q", fieldProtobufTag(key)),
377 fmt.Sprintf("protobuf_val:%q", fieldProtobufTag(val)),
378 )
379 }
Damien Neil162c1272018-10-04 12:42:37 -0700380 g.Annotate(message.GoIdent.GoName+"."+field.GoName, field.Location)
Damien Neil55fe1c02018-09-17 15:11:24 -0700381 g.P(field.GoName, " ", goType, " `", strings.Join(tags, " "), "`",
Damien Neil204f1c02018-10-23 15:03:38 -0700382 deprecationComment(field.Desc.Options().(*descpb.FieldOptions).GetDeprecated()))
Damien Neil658051b2018-09-10 12:26:21 -0700383 }
384 g.P("XXX_NoUnkeyedLiteral struct{} `json:\"-\"`")
Damien Neil993c04d2018-09-14 15:41:11 -0700385
386 if message.Desc.ExtensionRanges().Len() > 0 {
387 var tags []string
Damien Neil204f1c02018-10-23 15:03:38 -0700388 if message.Desc.Options().(*descpb.MessageOptions).GetMessageSetWireFormat() {
Damien Neil993c04d2018-09-14 15:41:11 -0700389 tags = append(tags, `protobuf_messageset:"1"`)
390 }
391 tags = append(tags, `json:"-"`)
392 g.P(protogen.GoIdent{
393 GoImportPath: protoPackage,
394 GoName: "XXX_InternalExtensions",
395 }, " `", strings.Join(tags, " "), "`")
396 }
Damien Neil658051b2018-09-10 12:26:21 -0700397 // TODO XXX_InternalExtensions
398 g.P("XXX_unrecognized []byte `json:\"-\"`")
399 g.P("XXX_sizecache int32 `json:\"-\"`")
Damien Neilc7d07d92018-08-22 13:46:02 -0700400 g.P("}")
401 g.P()
402
Damien Neila1c6abc2018-09-12 13:36:34 -0700403 // Reset
404 g.P("func (m *", message.GoIdent, ") Reset() { *m = ", message.GoIdent, "{} }")
405 // String
406 g.P("func (m *", message.GoIdent, ") String() string { return ", protogen.GoIdent{
407 GoImportPath: protoPackage,
408 GoName: "CompactTextString",
409 }, "(m) }")
410 // ProtoMessage
411 g.P("func (*", message.GoIdent, ") ProtoMessage() {}")
412 // Descriptor
413 var indexes []string
Damien Neil162c1272018-10-04 12:42:37 -0700414 for i := 1; i < len(message.Location.Path); i += 2 {
415 indexes = append(indexes, strconv.Itoa(int(message.Location.Path[i])))
Damien Neila1c6abc2018-09-12 13:36:34 -0700416 }
417 g.P("func (*", message.GoIdent, ") Descriptor() ([]byte, []int) {")
418 g.P("return ", f.descriptorVar, ", []int{", strings.Join(indexes, ","), "}")
419 g.P("}")
Damien Neil993c04d2018-09-14 15:41:11 -0700420 g.P()
421
422 // ExtensionRangeArray
423 if extranges := message.Desc.ExtensionRanges(); extranges.Len() > 0 {
Damien Neil204f1c02018-10-23 15:03:38 -0700424 if message.Desc.Options().(*descpb.MessageOptions).GetMessageSetWireFormat() {
Damien Neil993c04d2018-09-14 15:41:11 -0700425 g.P("func (m *", message.GoIdent, ") MarshalJSON() ([]byte, error) {")
426 g.P("return ", protogen.GoIdent{
427 GoImportPath: protoPackage,
428 GoName: "MarshalMessageSetJSON",
429 }, "(&m.XXX_InternalExtensions)")
430 g.P("}")
431 g.P("func (m *", message.GoIdent, ") UnmarshalJSON(buf []byte) error {")
432 g.P("return ", protogen.GoIdent{
433 GoImportPath: protoPackage,
434 GoName: "UnmarshalMessageSetJSON",
435 }, "(buf, &m.XXX_InternalExtensions)")
436 g.P("}")
437 g.P()
438 }
439
440 protoExtRange := protogen.GoIdent{
441 GoImportPath: protoPackage,
442 GoName: "ExtensionRange",
443 }
444 extRangeVar := "extRange_" + message.GoIdent.GoName
445 g.P("var ", extRangeVar, " = []", protoExtRange, " {")
446 for i := 0; i < extranges.Len(); i++ {
447 r := extranges.Get(i)
448 g.P("{Start:", r[0], ", End:", r[1]-1 /* inclusive */, "},")
449 }
450 g.P("}")
451 g.P()
452 g.P("func (*", message.GoIdent, ") ExtensionRangeArray() []", protoExtRange, " {")
453 g.P("return ", extRangeVar)
454 g.P("}")
455 g.P()
456 }
Damien Neila1c6abc2018-09-12 13:36:34 -0700457
Damien Neilea7baf42018-09-28 14:23:44 -0700458 genWellKnownType(g, "*", message.GoIdent, message.Desc)
459
Damien Neila1c6abc2018-09-12 13:36:34 -0700460 // Table-driven proto support.
461 //
462 // TODO: It does not scale to keep adding another method for every
463 // operation on protos that we want to switch over to using the
464 // table-driven approach. Instead, we should only add a single method
465 // that allows getting access to the *InternalMessageInfo struct and then
466 // calling Unmarshal, Marshal, Merge, Size, and Discard directly on that.
467 messageInfoVar := "xxx_messageInfo_" + message.GoIdent.GoName
468 // XXX_Unmarshal
469 g.P("func (m *", message.GoIdent, ") XXX_Unmarshal(b []byte) error {")
470 g.P("return ", messageInfoVar, ".Unmarshal(m, b)")
471 g.P("}")
472 // XXX_Marshal
473 g.P("func (m *", message.GoIdent, ") XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {")
474 g.P("return ", messageInfoVar, ".Marshal(b, m, deterministic)")
475 g.P("}")
476 // XXX_Merge
477 g.P("func (m *", message.GoIdent, ") XXX_Merge(src proto.Message) {")
478 g.P(messageInfoVar, ".Merge(m, src)")
479 g.P("}")
480 // XXX_Size
481 g.P("func (m *", message.GoIdent, ") XXX_Size() int {")
482 g.P("return ", messageInfoVar, ".Size(m)")
483 g.P("}")
484 // XXX_DiscardUnknown
485 g.P("func (m *", message.GoIdent, ") XXX_DiscardUnknown() {")
486 g.P(messageInfoVar, ".DiscardUnknown(m)")
487 g.P("}")
488 g.P()
489 g.P("var ", messageInfoVar, " ", protogen.GoIdent{
490 GoImportPath: protoPackage,
491 GoName: "InternalMessageInfo",
492 })
493 g.P()
494
Damien Neilebc699d2018-09-13 08:50:13 -0700495 // Constants and vars holding the default values of fields.
496 for _, field := range message.Fields {
Damien Neilccf3fa62018-09-28 14:41:45 -0700497 if !fieldHasDefault(field) {
Damien Neilebc699d2018-09-13 08:50:13 -0700498 continue
499 }
Damien Neil1fa78d82018-09-13 13:12:36 -0700500 defVarName := "Default_" + message.GoIdent.GoName + "_" + field.GoName
Damien Neilebc699d2018-09-13 08:50:13 -0700501 def := field.Desc.Default()
502 switch field.Desc.Kind() {
503 case protoreflect.StringKind:
504 g.P("const ", defVarName, " string = ", strconv.Quote(def.String()))
505 case protoreflect.BytesKind:
506 g.P("var ", defVarName, " []byte = []byte(", strconv.Quote(string(def.Bytes())), ")")
507 case protoreflect.EnumKind:
Damien Neila485fbd2018-10-26 13:28:37 -0700508 evalueDesc := field.Desc.DefaultEnumValue()
Damien Neilebc699d2018-09-13 08:50:13 -0700509 enum := field.EnumType
Damien Neila485fbd2018-10-26 13:28:37 -0700510 evalue := enum.Values[evalueDesc.Index()]
Damien Neilebc699d2018-09-13 08:50:13 -0700511 g.P("const ", defVarName, " ", field.EnumType.GoIdent, " = ", evalue.GoIdent)
512 case protoreflect.FloatKind, protoreflect.DoubleKind:
513 // Floating point numbers need extra handling for -Inf/Inf/NaN.
514 f := field.Desc.Default().Float()
515 goType := "float64"
516 if field.Desc.Kind() == protoreflect.FloatKind {
517 goType = "float32"
518 }
519 // funcCall returns a call to a function in the math package,
520 // possibly converting the result to float32.
521 funcCall := func(fn, param string) string {
522 s := g.QualifiedGoIdent(protogen.GoIdent{
523 GoImportPath: "math",
524 GoName: fn,
525 }) + param
526 if goType != "float64" {
527 s = goType + "(" + s + ")"
528 }
529 return s
530 }
531 switch {
532 case math.IsInf(f, -1):
533 g.P("var ", defVarName, " ", goType, " = ", funcCall("Inf", "(-1)"))
534 case math.IsInf(f, 1):
535 g.P("var ", defVarName, " ", goType, " = ", funcCall("Inf", "(1)"))
536 case math.IsNaN(f):
537 g.P("var ", defVarName, " ", goType, " = ", funcCall("NaN", "()"))
538 default:
Damien Neil982684b2018-09-28 14:12:41 -0700539 g.P("const ", defVarName, " ", goType, " = ", field.Desc.Default().Interface())
Damien Neilebc699d2018-09-13 08:50:13 -0700540 }
541 default:
Damien Neil77f82fe2018-09-13 10:59:17 -0700542 goType, _ := fieldGoType(g, field)
Damien Neilebc699d2018-09-13 08:50:13 -0700543 g.P("const ", defVarName, " ", goType, " = ", def.Interface())
544 }
545 }
546 g.P()
547
Damien Neil77f82fe2018-09-13 10:59:17 -0700548 // Getters.
549 for _, field := range message.Fields {
Damien Neil1fa78d82018-09-13 13:12:36 -0700550 if field.OneofType != nil {
551 if field == field.OneofType.Fields[0] {
552 genOneofTypes(gen, g, f, message, field.OneofType)
553 }
554 }
Damien Neil77f82fe2018-09-13 10:59:17 -0700555 goType, pointer := fieldGoType(g, field)
556 defaultValue := fieldDefaultValue(g, message, field)
Damien Neil204f1c02018-10-23 15:03:38 -0700557 if field.Desc.Options().(*descpb.FieldOptions).GetDeprecated() {
Damien Neil55fe1c02018-09-17 15:11:24 -0700558 g.P(deprecationComment(true))
559 }
Damien Neil162c1272018-10-04 12:42:37 -0700560 g.Annotate(message.GoIdent.GoName+".Get"+field.GoName, field.Location)
Damien Neil1fa78d82018-09-13 13:12:36 -0700561 g.P("func (m *", message.GoIdent, ") Get", field.GoName, "() ", goType, " {")
562 if field.OneofType != nil {
Damien Neil81d6d832018-09-19 12:12:17 -0700563 g.P("if x, ok := m.Get", field.OneofType.GoName, "().(*", fieldOneofType(field), "); ok {")
Damien Neil1fa78d82018-09-13 13:12:36 -0700564 g.P("return x.", field.GoName)
565 g.P("}")
Damien Neil77f82fe2018-09-13 10:59:17 -0700566 } else {
Damien Neil1fa78d82018-09-13 13:12:36 -0700567 if field.Desc.Syntax() == protoreflect.Proto3 || defaultValue == "nil" {
568 g.P("if m != nil {")
569 } else {
570 g.P("if m != nil && m.", field.GoName, " != nil {")
571 }
572 star := ""
573 if pointer {
574 star = "*"
575 }
576 g.P("return ", star, " m.", field.GoName)
577 g.P("}")
Damien Neil77f82fe2018-09-13 10:59:17 -0700578 }
Damien Neil77f82fe2018-09-13 10:59:17 -0700579 g.P("return ", defaultValue)
580 g.P("}")
581 g.P()
582 }
Damien Neila1c6abc2018-09-12 13:36:34 -0700583
Damien Neil1fa78d82018-09-13 13:12:36 -0700584 if len(message.Oneofs) > 0 {
585 genOneofFuncs(gen, g, f, message)
586 }
Damien Neil993c04d2018-09-14 15:41:11 -0700587 for _, extension := range message.Extensions {
588 genExtension(gen, g, f, extension)
589 }
Damien Neilc7d07d92018-08-22 13:46:02 -0700590}
Damien Neilcab8dfe2018-09-06 14:51:28 -0700591
Damien Neil77f82fe2018-09-13 10:59:17 -0700592// fieldGoType returns the Go type used for a field.
593//
594// If it returns pointer=true, the struct field is a pointer to the type.
595func fieldGoType(g *protogen.GeneratedFile, field *protogen.Field) (goType string, pointer bool) {
Damien Neil77f82fe2018-09-13 10:59:17 -0700596 pointer = true
Damien Neil658051b2018-09-10 12:26:21 -0700597 switch field.Desc.Kind() {
598 case protoreflect.BoolKind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700599 goType = "bool"
Damien Neil658051b2018-09-10 12:26:21 -0700600 case protoreflect.EnumKind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700601 goType = g.QualifiedGoIdent(field.EnumType.GoIdent)
Damien Neil658051b2018-09-10 12:26:21 -0700602 case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700603 goType = "int32"
Damien Neil658051b2018-09-10 12:26:21 -0700604 case protoreflect.Uint32Kind, protoreflect.Fixed32Kind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700605 goType = "uint32"
Damien Neil658051b2018-09-10 12:26:21 -0700606 case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700607 goType = "int64"
Damien Neil658051b2018-09-10 12:26:21 -0700608 case protoreflect.Uint64Kind, protoreflect.Fixed64Kind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700609 goType = "uint64"
Damien Neil658051b2018-09-10 12:26:21 -0700610 case protoreflect.FloatKind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700611 goType = "float32"
Damien Neil658051b2018-09-10 12:26:21 -0700612 case protoreflect.DoubleKind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700613 goType = "float64"
Damien Neil658051b2018-09-10 12:26:21 -0700614 case protoreflect.StringKind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700615 goType = "string"
Damien Neil658051b2018-09-10 12:26:21 -0700616 case protoreflect.BytesKind:
Damien Neil77f82fe2018-09-13 10:59:17 -0700617 goType = "[]byte"
618 pointer = false
Damien Neil658051b2018-09-10 12:26:21 -0700619 case protoreflect.MessageKind, protoreflect.GroupKind:
Damien Neil0bd5a382018-09-13 15:07:10 -0700620 if field.Desc.IsMap() {
621 keyType, _ := fieldGoType(g, field.MessageType.Fields[0])
622 valType, _ := fieldGoType(g, field.MessageType.Fields[1])
623 return fmt.Sprintf("map[%v]%v", keyType, valType), false
624 }
Damien Neil77f82fe2018-09-13 10:59:17 -0700625 goType = "*" + g.QualifiedGoIdent(field.MessageType.GoIdent)
626 pointer = false
Damien Neil658051b2018-09-10 12:26:21 -0700627 }
628 if field.Desc.Cardinality() == protoreflect.Repeated {
Damien Neil77f82fe2018-09-13 10:59:17 -0700629 goType = "[]" + goType
630 pointer = false
Damien Neil658051b2018-09-10 12:26:21 -0700631 }
Damien Neil44000a12018-10-24 12:31:16 -0700632 // Extension fields always have pointer type, even when defined in a proto3 file.
633 if field.Desc.Syntax() == protoreflect.Proto3 && field.Desc.ExtendedType() == nil {
Damien Neil77f82fe2018-09-13 10:59:17 -0700634 pointer = false
Damien Neil658051b2018-09-10 12:26:21 -0700635 }
Damien Neil77f82fe2018-09-13 10:59:17 -0700636 return goType, pointer
Damien Neil658051b2018-09-10 12:26:21 -0700637}
638
639func fieldProtobufTag(field *protogen.Field) string {
Joe Tsai05828db2018-11-01 13:52:16 -0700640 var enumName string
Damien Neil658051b2018-09-10 12:26:21 -0700641 if field.Desc.Kind() == protoreflect.EnumKind {
Joe Tsai05828db2018-11-01 13:52:16 -0700642 enumName = enumRegistryName(field.EnumType)
Damien Neil658051b2018-09-10 12:26:21 -0700643 }
Joe Tsai05828db2018-11-01 13:52:16 -0700644 return tag.Marshal(field.Desc, enumName)
Damien Neil658051b2018-09-10 12:26:21 -0700645}
646
Damien Neil77f82fe2018-09-13 10:59:17 -0700647func fieldDefaultValue(g *protogen.GeneratedFile, message *protogen.Message, field *protogen.Field) string {
648 if field.Desc.Cardinality() == protoreflect.Repeated {
649 return "nil"
650 }
Damien Neilccf3fa62018-09-28 14:41:45 -0700651 if fieldHasDefault(field) {
Damien Neil1fa78d82018-09-13 13:12:36 -0700652 defVarName := "Default_" + message.GoIdent.GoName + "_" + field.GoName
Damien Neil77f82fe2018-09-13 10:59:17 -0700653 if field.Desc.Kind() == protoreflect.BytesKind {
654 return "append([]byte(nil), " + defVarName + "...)"
655 }
656 return defVarName
657 }
658 switch field.Desc.Kind() {
659 case protoreflect.BoolKind:
660 return "false"
661 case protoreflect.StringKind:
662 return `""`
663 case protoreflect.MessageKind, protoreflect.GroupKind, protoreflect.BytesKind:
664 return "nil"
665 case protoreflect.EnumKind:
666 return g.QualifiedGoIdent(field.EnumType.Values[0].GoIdent)
667 default:
668 return "0"
669 }
670}
671
Damien Neilccf3fa62018-09-28 14:41:45 -0700672// fieldHasDefault returns true if we consider a field to have a default value.
673//
674// For consistency with the previous generator, it returns false for fields with
675// [default=""], preventing the generation of a default const or var for these
676// fields.
677//
678// TODO: Drop this special case.
679func fieldHasDefault(field *protogen.Field) bool {
680 if !field.Desc.HasDefault() {
681 return false
682 }
683 switch field.Desc.Kind() {
684 case protoreflect.StringKind:
685 return field.Desc.Default().String() != ""
686 case protoreflect.BytesKind:
687 return len(field.Desc.Default().Bytes()) > 0
688 }
689 return true
690}
691
Damien Neil658051b2018-09-10 12:26:21 -0700692func fieldJSONTag(field *protogen.Field) string {
693 return string(field.Desc.Name()) + ",omitempty"
694}
695
Damien Neild39efc82018-09-24 12:38:10 -0700696func genExtension(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, extension *protogen.Extension) {
Damien Neil154da982018-09-19 13:21:58 -0700697 // Special case for proto2 message sets: If this extension is extending
698 // proto2.bridge.MessageSet, and its final name component is "message_set_extension",
699 // then drop that last component.
700 //
701 // TODO: This should be implemented in the text formatter rather than the generator.
702 // In addition, the situation for when to apply this special case is implemented
703 // differently in other languages:
704 // https://github.com/google/protobuf/blob/aff10976/src/google/protobuf/text_format.cc#L1560
705 name := extension.Desc.FullName()
Damien Neil62386962018-10-30 10:35:48 -0700706 if n, ok := isExtensionMessageSetElement(extension); ok {
707 name = n
Damien Neil154da982018-09-19 13:21:58 -0700708 }
709
Damien Neil6b541312018-10-29 09:14:14 -0700710 g.P("var ", extensionVar(f.File, extension), " = &", protogen.GoIdent{
Damien Neil993c04d2018-09-14 15:41:11 -0700711 GoImportPath: protoPackage,
712 GoName: "ExtensionDesc",
713 }, "{")
714 g.P("ExtendedType: (*", extension.ExtendedType.GoIdent, ")(nil),")
715 goType, pointer := fieldGoType(g, extension)
716 if pointer {
717 goType = "*" + goType
718 }
719 g.P("ExtensionType: (", goType, ")(nil),")
720 g.P("Field: ", extension.Desc.Number(), ",")
Damien Neil154da982018-09-19 13:21:58 -0700721 g.P("Name: ", strconv.Quote(string(name)), ",")
Damien Neil993c04d2018-09-14 15:41:11 -0700722 g.P("Tag: ", strconv.Quote(fieldProtobufTag(extension)), ",")
723 g.P("Filename: ", strconv.Quote(f.Desc.Path()), ",")
724 g.P("}")
725 g.P()
726}
727
Damien Neil62386962018-10-30 10:35:48 -0700728// isExtensionMessageSetELement returns the adjusted name of an extension
729// which extends proto2.bridge.MessageSet.
730func isExtensionMessageSetElement(extension *protogen.Extension) (name protoreflect.FullName, ok bool) {
731 opts := extension.ExtendedType.Desc.Options().(*descpb.MessageOptions)
732 if !opts.GetMessageSetWireFormat() || extension.Desc.Name() != "message_set_extension" {
733 return "", false
734 }
735 if extension.ParentMessage == nil {
736 // This case shouldn't be given special handling at all--we're
737 // only supposed to drop the ".message_set_extension" for
738 // extensions defined within a message (i.e., the extension
739 // takes the message's name).
740 //
741 // This matches the behavior of the v1 generator, however.
742 //
743 // TODO: See if we can drop this case.
744 name = extension.Desc.FullName()
745 name = name[:len(name)-len("message_set_extension")]
746 return name, true
747 }
748 return extension.Desc.FullName().Parent(), true
Damien Neil154da982018-09-19 13:21:58 -0700749}
750
Damien Neil993c04d2018-09-14 15:41:11 -0700751// extensionVar returns the var holding the ExtensionDesc for an extension.
Damien Neil6b541312018-10-29 09:14:14 -0700752func extensionVar(f *protogen.File, extension *protogen.Extension) protogen.GoIdent {
Damien Neil993c04d2018-09-14 15:41:11 -0700753 name := "E_"
754 if extension.ParentMessage != nil {
755 name += extension.ParentMessage.GoIdent.GoName + "_"
756 }
757 name += extension.GoName
758 return protogen.GoIdent{
759 GoImportPath: f.GoImportPath,
760 GoName: name,
761 }
762}
763
Damien Neilce36f8d2018-09-13 15:19:08 -0700764// genInitFunction generates an init function that registers the types in the
765// generated file with the proto package.
Damien Neild39efc82018-09-24 12:38:10 -0700766func genInitFunction(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) {
Damien Neil993c04d2018-09-14 15:41:11 -0700767 if len(f.allMessages) == 0 && len(f.allEnums) == 0 && len(f.allExtensions) == 0 {
Damien Neilce36f8d2018-09-13 15:19:08 -0700768 return
769 }
770
771 g.P("func init() {")
Damien Neil154da982018-09-19 13:21:58 -0700772 for _, enum := range f.allEnums {
773 name := enum.GoIdent.GoName
774 g.P(protogen.GoIdent{
775 GoImportPath: protoPackage,
776 GoName: "RegisterEnum",
777 }, fmt.Sprintf("(%q, %s_name, %s_value)", enumRegistryName(enum), name, name))
778 }
Damien Neilce36f8d2018-09-13 15:19:08 -0700779 for _, message := range f.allMessages {
780 if message.Desc.IsMapEntry() {
781 continue
782 }
783
Damien Neil154da982018-09-19 13:21:58 -0700784 for _, extension := range message.Extensions {
785 genRegisterExtension(gen, g, f, extension)
786 }
787
Damien Neilce36f8d2018-09-13 15:19:08 -0700788 name := message.GoIdent.GoName
789 g.P(protogen.GoIdent{
790 GoImportPath: protoPackage,
791 GoName: "RegisterType",
792 }, fmt.Sprintf("((*%s)(nil), %q)", name, message.Desc.FullName()))
793
794 // Types of map fields, sorted by the name of the field message type.
795 var mapFields []*protogen.Field
796 for _, field := range message.Fields {
797 if field.Desc.IsMap() {
798 mapFields = append(mapFields, field)
799 }
800 }
801 sort.Slice(mapFields, func(i, j int) bool {
802 ni := mapFields[i].MessageType.Desc.FullName()
803 nj := mapFields[j].MessageType.Desc.FullName()
804 return ni < nj
805 })
806 for _, field := range mapFields {
807 typeName := string(field.MessageType.Desc.FullName())
808 goType, _ := fieldGoType(g, field)
809 g.P(protogen.GoIdent{
810 GoImportPath: protoPackage,
811 GoName: "RegisterMapType",
812 }, fmt.Sprintf("((%v)(nil), %q)", goType, typeName))
813 }
814 }
Damien Neil154da982018-09-19 13:21:58 -0700815 for _, extension := range f.Extensions {
816 genRegisterExtension(gen, g, f, extension)
Damien Neil993c04d2018-09-14 15:41:11 -0700817 }
Damien Neilce36f8d2018-09-13 15:19:08 -0700818 g.P("}")
819 g.P()
820}
821
Damien Neild39efc82018-09-24 12:38:10 -0700822func genRegisterExtension(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, extension *protogen.Extension) {
Damien Neil154da982018-09-19 13:21:58 -0700823 g.P(protogen.GoIdent{
824 GoImportPath: protoPackage,
825 GoName: "RegisterExtension",
Damien Neil6b541312018-10-29 09:14:14 -0700826 }, "(", extensionVar(f.File, extension), ")")
Damien Neil62386962018-10-30 10:35:48 -0700827 if name, ok := isExtensionMessageSetElement(extension); ok {
Damien Neil154da982018-09-19 13:21:58 -0700828 goType, pointer := fieldGoType(g, extension)
829 if pointer {
830 goType = "*" + goType
831 }
832 g.P(protogen.GoIdent{
833 GoImportPath: protoPackage,
834 GoName: "RegisterMessageSetType",
Damien Neil62386962018-10-30 10:35:48 -0700835 }, "((", goType, ")(nil), ", extension.Desc.Number(), ",", strconv.Quote(string(name)), ")")
Damien Neil154da982018-09-19 13:21:58 -0700836 }
837}
838
Damien Neil55fe1c02018-09-17 15:11:24 -0700839// deprecationComment returns a standard deprecation comment if deprecated is true.
840func deprecationComment(deprecated bool) string {
841 if !deprecated {
842 return ""
843 }
844 return "// Deprecated: Do not use."
845}
846
Damien Neilea7baf42018-09-28 14:23:44 -0700847func genWellKnownType(g *protogen.GeneratedFile, ptr string, ident protogen.GoIdent, desc protoreflect.Descriptor) {
Damien Neil46abb572018-09-07 12:45:37 -0700848 if wellKnownTypes[desc.FullName()] {
Damien Neilea7baf42018-09-28 14:23:44 -0700849 g.P("func (", ptr, ident, `) XXX_WellKnownType() string { return "`, desc.Name(), `" }`)
Damien Neil46abb572018-09-07 12:45:37 -0700850 g.P()
851 }
852}
853
854// Names of messages and enums for which we will generate XXX_WellKnownType methods.
855var wellKnownTypes = map[protoreflect.FullName]bool{
Damien Neilea7baf42018-09-28 14:23:44 -0700856 "google.protobuf.Any": true,
857 "google.protobuf.Duration": true,
858 "google.protobuf.Empty": true,
859 "google.protobuf.Struct": true,
860 "google.protobuf.Timestamp": true,
861
862 "google.protobuf.BoolValue": true,
863 "google.protobuf.BytesValue": true,
864 "google.protobuf.DoubleValue": true,
865 "google.protobuf.FloatValue": true,
866 "google.protobuf.Int32Value": true,
867 "google.protobuf.Int64Value": true,
868 "google.protobuf.ListValue": true,
869 "google.protobuf.NullValue": true,
870 "google.protobuf.StringValue": true,
871 "google.protobuf.UInt32Value": true,
872 "google.protobuf.UInt64Value": true,
873 "google.protobuf.Value": true,
Damien Neil46abb572018-09-07 12:45:37 -0700874}