Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 1 | // 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 Neil | 1adaec9 | 2018-09-24 13:43:03 -0700 | [diff] [blame] | 5 | // Package internal_gengo is internal to the protobuf module. |
| 6 | package internal_gengo |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 7 | |
| 8 | import ( |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 9 | "bytes" |
| 10 | "compress/gzip" |
| 11 | "crypto/sha256" |
| 12 | "encoding/hex" |
| 13 | "fmt" |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 14 | "math" |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 15 | "sort" |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 16 | "strconv" |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 17 | "strings" |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 18 | |
| 19 | "github.com/golang/protobuf/proto" |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame] | 20 | "github.com/golang/protobuf/v2/internal/encoding/tag" |
Joe Tsai | 01ab296 | 2018-09-21 17:44:00 -0700 | [diff] [blame] | 21 | "github.com/golang/protobuf/v2/protogen" |
| 22 | "github.com/golang/protobuf/v2/reflect/protoreflect" |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 23 | |
| 24 | descriptorpb "github.com/golang/protobuf/v2/types/descriptor" |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
Damien Neil | d412792 | 2018-09-12 11:13:49 -0700 | [diff] [blame] | 27 | // generatedCodeVersion indicates a version of the generated code. |
| 28 | // It is incremented whenever an incompatibility between the generated code and |
| 29 | // proto package is introduced; the generated code references |
| 30 | // a constant, proto.ProtoPackageIsVersionN (where N is generatedCodeVersion). |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 31 | const generatedCodeVersion = 3 |
Damien Neil | d412792 | 2018-09-12 11:13:49 -0700 | [diff] [blame] | 32 | |
Joe Tsai | c1c17aa | 2018-11-16 11:14:14 -0800 | [diff] [blame] | 33 | const ( |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 34 | fmtPackage = protogen.GoImportPath("fmt") |
| 35 | mathPackage = protogen.GoImportPath("math") |
| 36 | protoPackage = protogen.GoImportPath("github.com/golang/protobuf/proto") |
| 37 | protoapiPackage = protogen.GoImportPath("github.com/golang/protobuf/protoapi") |
Joe Tsai | c1c17aa | 2018-11-16 11:14:14 -0800 | [diff] [blame] | 38 | ) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 39 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 40 | type fileInfo struct { |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 41 | *protogen.File |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 42 | descriptorVar string // var containing the gzipped FileDescriptorProto |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 43 | allEnums []*protogen.Enum |
| 44 | allMessages []*protogen.Message |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 45 | allExtensions []*protogen.Extension |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 46 | |
| 47 | fileReflect fileReflect |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 50 | // protoPackage returns the package to import, which is either the protoPackage |
| 51 | // or the protoapiPackage constant. |
| 52 | // |
| 53 | // This special casing exists because we are unable to move InternalMessageInfo |
| 54 | // to protoapi since the implementation behind that logic is heavy and |
| 55 | // too intricately connected to other parts of the proto package. |
| 56 | // The descriptor proto is special in that it avoids using InternalMessageInfo |
| 57 | // so that it is able to depend solely on protoapi and break its dependency |
| 58 | // on the proto package. It is still semantically correct for descriptor to |
| 59 | // avoid using InternalMessageInfo, but it does incur some performance penalty. |
| 60 | // This is acceptable for descriptor, which is a single proto file and is not |
| 61 | // known to be in the hot path for any code. |
| 62 | // |
| 63 | // TODO: Remove this special-casing when the table-driven implementation has |
| 64 | // been ported over to v2. |
| 65 | func (f *fileInfo) protoPackage() protogen.GoImportPath { |
| 66 | if isDescriptor(f.File) { |
| 67 | return protoapiPackage |
| 68 | } |
| 69 | return protoPackage |
| 70 | } |
| 71 | |
Damien Neil | 9c420a6 | 2018-09-27 15:26:33 -0700 | [diff] [blame] | 72 | // GenerateFile generates the contents of a .pb.go file. |
| 73 | func GenerateFile(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile) { |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 74 | f := &fileInfo{ |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 75 | File: file, |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 78 | // The different order for enums and extensions is to match the output |
| 79 | // of the previous implementation. |
| 80 | // |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 81 | // TODO: Eventually make this consistent (and remove fileReflect). |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 82 | f.allEnums = append(f.allEnums, f.File.Enums...) |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 83 | walkMessages(f.Messages, func(message *protogen.Message) { |
| 84 | f.allMessages = append(f.allMessages, message) |
| 85 | f.allEnums = append(f.allEnums, message.Enums...) |
| 86 | f.allExtensions = append(f.allExtensions, message.Extensions...) |
| 87 | }) |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 88 | f.allExtensions = append(f.allExtensions, f.File.Extensions...) |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 89 | |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 90 | // Initialize data structures needed for reflection. |
| 91 | f.fileReflect.init(f) |
| 92 | |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 93 | // Determine the name of the var holding the file descriptor: |
| 94 | // |
| 95 | // fileDescriptor_<hash of filename> |
| 96 | filenameHash := sha256.Sum256([]byte(f.Desc.Path())) |
| 97 | f.descriptorVar = fmt.Sprintf("fileDescriptor_%s", hex.EncodeToString(filenameHash[:8])) |
| 98 | |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 99 | g.P("// Code generated by protoc-gen-go. DO NOT EDIT.") |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 100 | if f.Proto.GetOptions().GetDeprecated() { |
| 101 | g.P("// ", f.Desc.Path(), " is a deprecated file.") |
| 102 | } else { |
| 103 | g.P("// source: ", f.Desc.Path()) |
| 104 | } |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 105 | g.P() |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 106 | const filePackageField = 2 // FileDescriptorProto.package |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 107 | g.PrintLeadingComments(protogen.Location{ |
| 108 | SourceFile: f.Proto.GetName(), |
| 109 | Path: []int32{filePackageField}, |
| 110 | }) |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 111 | g.P() |
Damien Neil | 082ce92 | 2018-09-06 10:23:53 -0700 | [diff] [blame] | 112 | g.P("package ", f.GoPackageName) |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 113 | g.P() |
Damien Neil | 1ec3315 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 114 | |
| 115 | // These references are not necessary, since we automatically add |
| 116 | // all necessary imports before formatting the generated file. |
| 117 | // |
| 118 | // This section exists to generate output more consistent with |
| 119 | // the previous version of protoc-gen-go, to make it easier to |
| 120 | // detect unintended variations. |
| 121 | // |
| 122 | // TODO: Eventually remove this. |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 123 | if !isDescriptor(file) { |
| 124 | g.P("// Reference imports to suppress errors if they are not otherwise used.") |
| 125 | g.P("var _ = ", protoPackage.Ident("Marshal")) |
| 126 | g.P("var _ = ", fmtPackage.Ident("Errorf")) |
| 127 | g.P("var _ = ", mathPackage.Ident("Inf")) |
| 128 | g.P() |
Damien Neil | 1ec3315 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 129 | |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 130 | g.P("// This is a compile-time assertion to ensure that this generated file") |
| 131 | g.P("// is compatible with the proto package it is being compiled against.") |
| 132 | g.P("// A compilation error at this line likely means your copy of the") |
| 133 | g.P("// proto package needs to be updated.") |
| 134 | g.P("const _ = ", protoPackage.Ident(fmt.Sprintf("ProtoPackageIsVersion%d", generatedCodeVersion)), |
| 135 | "// please upgrade the proto package") |
| 136 | g.P() |
| 137 | } |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 138 | |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 139 | for i, imps := 0, f.Desc.Imports(); i < imps.Len(); i++ { |
| 140 | genImport(gen, g, f, imps.Get(i)) |
| 141 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 142 | for _, enum := range f.allEnums { |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 143 | genEnum(gen, g, f, enum) |
| 144 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 145 | for _, message := range f.allMessages { |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 146 | genMessage(gen, g, f, message) |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 147 | } |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 148 | for _, extension := range f.Extensions { |
| 149 | genExtension(gen, g, f, extension) |
| 150 | } |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 151 | |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 152 | genInitFunction(gen, g, f) |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 153 | genFileDescriptor(gen, g, f) |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 154 | genReflectInitFunction(gen, g, f) |
| 155 | genReflectFileDescriptor(gen, g, f) |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 158 | // walkMessages calls f on each message and all of its descendants. |
| 159 | func walkMessages(messages []*protogen.Message, f func(*protogen.Message)) { |
| 160 | for _, m := range messages { |
| 161 | f(m) |
| 162 | walkMessages(m.Messages, f) |
| 163 | } |
| 164 | } |
| 165 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 166 | func genImport(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, imp protoreflect.FileImport) { |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 167 | impFile, ok := gen.FileByName(imp.Path()) |
| 168 | if !ok { |
| 169 | return |
| 170 | } |
| 171 | if impFile.GoImportPath == f.GoImportPath { |
Damien Neil | 2e0c3da | 2018-09-19 12:51:36 -0700 | [diff] [blame] | 172 | // Don't generate imports or aliases for types in the same Go package. |
| 173 | return |
| 174 | } |
Damien Neil | 40a0805 | 2018-10-29 09:07:41 -0700 | [diff] [blame] | 175 | // Generate imports for all non-weak dependencies, even if they are not |
Damien Neil | 2e0c3da | 2018-09-19 12:51:36 -0700 | [diff] [blame] | 176 | // referenced, because other code and tools depend on having the |
| 177 | // full transitive closure of protocol buffer types in the binary. |
Damien Neil | 40a0805 | 2018-10-29 09:07:41 -0700 | [diff] [blame] | 178 | if !imp.IsWeak { |
| 179 | g.Import(impFile.GoImportPath) |
| 180 | } |
Damien Neil | 2e0c3da | 2018-09-19 12:51:36 -0700 | [diff] [blame] | 181 | if !imp.IsPublic { |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 182 | return |
| 183 | } |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 184 | // TODO: An alternate approach to generating public imports might be |
| 185 | // to generate the imported file contents, parse it, and extract all |
| 186 | // exported identifiers from the AST to build a list of forwarding |
| 187 | // declarations. |
| 188 | // |
| 189 | // TODO: Consider whether this should generate recursive aliases. e.g., |
| 190 | // if a.proto publicly imports b.proto publicly imports c.proto, should |
| 191 | // a.pb.go contain aliases for symbols defined in c.proto? |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 192 | var enums []*protogen.Enum |
| 193 | enums = append(enums, impFile.Enums...) |
| 194 | walkMessages(impFile.Messages, func(message *protogen.Message) { |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 195 | if message.Desc.IsMapEntry() { |
| 196 | return |
| 197 | } |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 198 | enums = append(enums, message.Enums...) |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 199 | for _, field := range message.Fields { |
| 200 | if !fieldHasDefault(field) { |
| 201 | continue |
| 202 | } |
| 203 | defVar := protogen.GoIdent{ |
| 204 | GoImportPath: message.GoIdent.GoImportPath, |
| 205 | GoName: "Default_" + message.GoIdent.GoName + "_" + field.GoName, |
| 206 | } |
| 207 | decl := "const" |
Damien Neil | 7e5c647 | 2018-11-29 08:57:07 -0800 | [diff] [blame] | 208 | switch field.Desc.Kind() { |
| 209 | case protoreflect.BytesKind: |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 210 | decl = "var" |
Damien Neil | 7e5c647 | 2018-11-29 08:57:07 -0800 | [diff] [blame] | 211 | case protoreflect.FloatKind, protoreflect.DoubleKind: |
| 212 | f := field.Desc.Default().Float() |
| 213 | if math.IsInf(f, -1) || math.IsInf(f, 1) || math.IsNaN(f) { |
| 214 | decl = "var" |
| 215 | } |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 216 | } |
| 217 | g.P(decl, " ", defVar.GoName, " = ", defVar) |
| 218 | } |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 219 | g.P("// ", message.GoIdent.GoName, " from public import ", imp.Path()) |
| 220 | g.P("type ", message.GoIdent.GoName, " = ", message.GoIdent) |
| 221 | for _, oneof := range message.Oneofs { |
| 222 | for _, field := range oneof.Fields { |
| 223 | typ := fieldOneofType(field) |
| 224 | g.P("type ", typ.GoName, " = ", typ) |
| 225 | } |
| 226 | } |
| 227 | g.P() |
| 228 | }) |
| 229 | for _, enum := range enums { |
| 230 | g.P("// ", enum.GoIdent.GoName, " from public import ", imp.Path()) |
| 231 | g.P("type ", enum.GoIdent.GoName, " = ", enum.GoIdent) |
| 232 | g.P("var ", enum.GoIdent.GoName, "_name = ", enum.GoIdent, "_name") |
| 233 | g.P("var ", enum.GoIdent.GoName, "_value = ", enum.GoIdent, "_value") |
| 234 | g.P() |
| 235 | for _, value := range enum.Values { |
| 236 | g.P("const ", value.GoIdent.GoName, " = ", enum.GoIdent.GoName, "(", value.GoIdent, ")") |
| 237 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 238 | } |
Damien Neil | 6b54131 | 2018-10-29 09:14:14 -0700 | [diff] [blame] | 239 | for _, ext := range impFile.Extensions { |
| 240 | ident := extensionVar(impFile, ext) |
| 241 | g.P("var ", ident.GoName, " = ", ident) |
| 242 | g.P() |
| 243 | } |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 244 | g.P() |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 247 | func genFileDescriptor(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) { |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 248 | // Trim the source_code_info from the descriptor. |
| 249 | // Marshal and gzip it. |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 250 | descProto := proto.Clone(f.Proto).(*descriptorpb.FileDescriptorProto) |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 251 | descProto.SourceCodeInfo = nil |
| 252 | b, err := proto.Marshal(descProto) |
| 253 | if err != nil { |
| 254 | gen.Error(err) |
| 255 | return |
| 256 | } |
| 257 | var buf bytes.Buffer |
| 258 | w, _ := gzip.NewWriterLevel(&buf, gzip.BestCompression) |
| 259 | w.Write(b) |
| 260 | w.Close() |
| 261 | b = buf.Bytes() |
| 262 | |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 263 | g.P("func init() {", f.protoPackage().Ident("RegisterFile"), "(", strconv.Quote(f.Desc.Path()), ", ", f.descriptorVar, ") }") |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 264 | g.P() |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 265 | g.P("var ", f.descriptorVar, " = []byte{") |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 266 | g.P("// ", len(b), " bytes of a gzipped FileDescriptorProto") |
| 267 | for len(b) > 0 { |
| 268 | n := 16 |
| 269 | if n > len(b) { |
| 270 | n = len(b) |
| 271 | } |
| 272 | |
| 273 | s := "" |
| 274 | for _, c := range b[:n] { |
| 275 | s += fmt.Sprintf("0x%02x,", c) |
| 276 | } |
| 277 | g.P(s) |
| 278 | |
| 279 | b = b[n:] |
| 280 | } |
| 281 | g.P("}") |
| 282 | g.P() |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 283 | } |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 284 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 285 | func genEnum(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, enum *protogen.Enum) { |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 286 | g.PrintLeadingComments(enum.Location) |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 287 | g.Annotate(enum.GoIdent.GoName, enum.Location) |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 288 | g.P("type ", enum.GoIdent, " int32", |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 289 | deprecationComment(enum.Desc.Options().(*descriptorpb.EnumOptions).GetDeprecated())) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 290 | g.P("const (") |
| 291 | for _, value := range enum.Values { |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 292 | g.PrintLeadingComments(value.Location) |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 293 | g.Annotate(value.GoIdent.GoName, value.Location) |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 294 | g.P(value.GoIdent, " ", enum.GoIdent, " = ", value.Desc.Number(), |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 295 | deprecationComment(value.Desc.Options().(*descriptorpb.EnumValueOptions).GetDeprecated())) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 296 | } |
| 297 | g.P(")") |
| 298 | g.P() |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 299 | |
| 300 | // Generate support for protobuf reflection. |
| 301 | genReflectEnum(gen, g, f, enum) |
| 302 | |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 303 | nameMap := enum.GoIdent.GoName + "_name" |
| 304 | g.P("var ", nameMap, " = map[int32]string{") |
| 305 | generated := make(map[protoreflect.EnumNumber]bool) |
| 306 | for _, value := range enum.Values { |
| 307 | duplicate := "" |
| 308 | if _, present := generated[value.Desc.Number()]; present { |
| 309 | duplicate = "// Duplicate value: " |
| 310 | } |
| 311 | g.P(duplicate, value.Desc.Number(), ": ", strconv.Quote(string(value.Desc.Name())), ",") |
| 312 | generated[value.Desc.Number()] = true |
| 313 | } |
| 314 | g.P("}") |
| 315 | g.P() |
| 316 | valueMap := enum.GoIdent.GoName + "_value" |
| 317 | g.P("var ", valueMap, " = map[string]int32{") |
| 318 | for _, value := range enum.Values { |
| 319 | g.P(strconv.Quote(string(value.Desc.Name())), ": ", value.Desc.Number(), ",") |
| 320 | } |
| 321 | g.P("}") |
| 322 | g.P() |
| 323 | if enum.Desc.Syntax() != protoreflect.Proto3 { |
| 324 | g.P("func (x ", enum.GoIdent, ") Enum() *", enum.GoIdent, " {") |
| 325 | g.P("p := new(", enum.GoIdent, ")") |
| 326 | g.P("*p = x") |
| 327 | g.P("return p") |
| 328 | g.P("}") |
| 329 | g.P() |
| 330 | } |
| 331 | g.P("func (x ", enum.GoIdent, ") String() string {") |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 332 | g.P("return ", f.protoPackage().Ident("EnumName"), "(", enum.GoIdent, "_name, int32(x))") |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 333 | g.P("}") |
| 334 | g.P() |
| 335 | |
| 336 | if enum.Desc.Syntax() != protoreflect.Proto3 { |
| 337 | g.P("func (x *", enum.GoIdent, ") UnmarshalJSON(data []byte) error {") |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 338 | g.P("value, err := ", f.protoPackage().Ident("UnmarshalJSONEnum"), "(", enum.GoIdent, `_value, data, "`, enum.GoIdent, `")`) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 339 | g.P("if err != nil {") |
| 340 | g.P("return err") |
| 341 | g.P("}") |
| 342 | g.P("*x = ", enum.GoIdent, "(value)") |
| 343 | g.P("return nil") |
| 344 | g.P("}") |
| 345 | g.P() |
| 346 | } |
| 347 | |
| 348 | var indexes []string |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 349 | for i := 1; i < len(enum.Location.Path); i += 2 { |
| 350 | indexes = append(indexes, strconv.Itoa(int(enum.Location.Path[i]))) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 351 | } |
| 352 | g.P("func (", enum.GoIdent, ") EnumDescriptor() ([]byte, []int) {") |
| 353 | g.P("return ", f.descriptorVar, ", []int{", strings.Join(indexes, ","), "}") |
| 354 | g.P("}") |
| 355 | g.P() |
| 356 | |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 357 | genWellKnownType(g, "", enum.GoIdent, enum.Desc) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 360 | // enumRegistryName returns the name used to register an enum with the proto |
| 361 | // package registry. |
| 362 | // |
| 363 | // Confusingly, this is <proto_package>.<go_ident>. This probably should have |
| 364 | // been the full name of the proto enum type instead, but changing it at this |
| 365 | // point would require thought. |
| 366 | func enumRegistryName(enum *protogen.Enum) string { |
| 367 | // Find the FileDescriptor for this enum. |
| 368 | var desc protoreflect.Descriptor = enum.Desc |
| 369 | for { |
| 370 | p, ok := desc.Parent() |
| 371 | if !ok { |
| 372 | break |
| 373 | } |
| 374 | desc = p |
| 375 | } |
| 376 | fdesc := desc.(protoreflect.FileDescriptor) |
Damien Neil | daa4fad | 2018-10-08 14:08:27 -0700 | [diff] [blame] | 377 | if fdesc.Package() == "" { |
| 378 | return enum.GoIdent.GoName |
| 379 | } |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 380 | return string(fdesc.Package()) + "." + enum.GoIdent.GoName |
| 381 | } |
| 382 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 383 | func genMessage(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) { |
Damien Neil | 0bd5a38 | 2018-09-13 15:07:10 -0700 | [diff] [blame] | 384 | if message.Desc.IsMapEntry() { |
| 385 | return |
| 386 | } |
| 387 | |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 388 | hasComment := g.PrintLeadingComments(message.Location) |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 389 | if message.Desc.Options().(*descriptorpb.MessageOptions).GetDeprecated() { |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 390 | if hasComment { |
| 391 | g.P("//") |
| 392 | } |
| 393 | g.P(deprecationComment(true)) |
| 394 | } |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 395 | g.Annotate(message.GoIdent.GoName, message.Location) |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 396 | g.P("type ", message.GoIdent, " struct {") |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 397 | for _, field := range message.Fields { |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 398 | if field.OneofType != nil { |
| 399 | // It would be a bit simpler to iterate over the oneofs below, |
| 400 | // but generating the field here keeps the contents of the Go |
| 401 | // struct in the same order as the contents of the source |
| 402 | // .proto file. |
| 403 | if field == field.OneofType.Fields[0] { |
| 404 | genOneofField(gen, g, f, message, field.OneofType) |
| 405 | } |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 406 | continue |
| 407 | } |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 408 | g.PrintLeadingComments(field.Location) |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 409 | goType, pointer := fieldGoType(g, field) |
| 410 | if pointer { |
| 411 | goType = "*" + goType |
| 412 | } |
Damien Neil | 0bd5a38 | 2018-09-13 15:07:10 -0700 | [diff] [blame] | 413 | tags := []string{ |
| 414 | fmt.Sprintf("protobuf:%q", fieldProtobufTag(field)), |
| 415 | fmt.Sprintf("json:%q", fieldJSONTag(field)), |
| 416 | } |
| 417 | if field.Desc.IsMap() { |
| 418 | key := field.MessageType.Fields[0] |
| 419 | val := field.MessageType.Fields[1] |
| 420 | tags = append(tags, |
| 421 | fmt.Sprintf("protobuf_key:%q", fieldProtobufTag(key)), |
| 422 | fmt.Sprintf("protobuf_val:%q", fieldProtobufTag(val)), |
| 423 | ) |
| 424 | } |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 425 | g.Annotate(message.GoIdent.GoName+"."+field.GoName, field.Location) |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 426 | g.P(field.GoName, " ", goType, " `", strings.Join(tags, " "), "`", |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 427 | deprecationComment(field.Desc.Options().(*descriptorpb.FieldOptions).GetDeprecated())) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 428 | } |
| 429 | g.P("XXX_NoUnkeyedLiteral struct{} `json:\"-\"`") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 430 | |
| 431 | if message.Desc.ExtensionRanges().Len() > 0 { |
| 432 | var tags []string |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 433 | if message.Desc.Options().(*descriptorpb.MessageOptions).GetMessageSetWireFormat() { |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 434 | tags = append(tags, `protobuf_messageset:"1"`) |
| 435 | } |
| 436 | tags = append(tags, `json:"-"`) |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 437 | g.P(f.protoPackage().Ident("XXX_InternalExtensions"), " `", strings.Join(tags, " "), "`") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 438 | } |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 439 | // TODO XXX_InternalExtensions |
| 440 | g.P("XXX_unrecognized []byte `json:\"-\"`") |
| 441 | g.P("XXX_sizecache int32 `json:\"-\"`") |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 442 | g.P("}") |
| 443 | g.P() |
| 444 | |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 445 | // Generate support for protobuf reflection. |
| 446 | genReflectMessage(gen, g, f, message) |
| 447 | |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 448 | // Reset |
| 449 | g.P("func (m *", message.GoIdent, ") Reset() { *m = ", message.GoIdent, "{} }") |
| 450 | // String |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 451 | g.P("func (m *", message.GoIdent, ") String() string { return ", f.protoPackage().Ident("CompactTextString"), "(m) }") |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 452 | // ProtoMessage |
| 453 | g.P("func (*", message.GoIdent, ") ProtoMessage() {}") |
| 454 | // Descriptor |
| 455 | var indexes []string |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 456 | for i := 1; i < len(message.Location.Path); i += 2 { |
| 457 | indexes = append(indexes, strconv.Itoa(int(message.Location.Path[i]))) |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 458 | } |
| 459 | g.P("func (*", message.GoIdent, ") Descriptor() ([]byte, []int) {") |
| 460 | g.P("return ", f.descriptorVar, ", []int{", strings.Join(indexes, ","), "}") |
| 461 | g.P("}") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 462 | g.P() |
| 463 | |
| 464 | // ExtensionRangeArray |
| 465 | if extranges := message.Desc.ExtensionRanges(); extranges.Len() > 0 { |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 466 | protoExtRange := f.protoPackage().Ident("ExtensionRange") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 467 | extRangeVar := "extRange_" + message.GoIdent.GoName |
| 468 | g.P("var ", extRangeVar, " = []", protoExtRange, " {") |
| 469 | for i := 0; i < extranges.Len(); i++ { |
| 470 | r := extranges.Get(i) |
| 471 | g.P("{Start:", r[0], ", End:", r[1]-1 /* inclusive */, "},") |
| 472 | } |
| 473 | g.P("}") |
| 474 | g.P() |
| 475 | g.P("func (*", message.GoIdent, ") ExtensionRangeArray() []", protoExtRange, " {") |
| 476 | g.P("return ", extRangeVar) |
| 477 | g.P("}") |
| 478 | g.P() |
| 479 | } |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 480 | |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 481 | genWellKnownType(g, "*", message.GoIdent, message.Desc) |
| 482 | |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 483 | // Table-driven proto support. |
| 484 | // |
| 485 | // TODO: It does not scale to keep adding another method for every |
| 486 | // operation on protos that we want to switch over to using the |
| 487 | // table-driven approach. Instead, we should only add a single method |
| 488 | // that allows getting access to the *InternalMessageInfo struct and then |
| 489 | // calling Unmarshal, Marshal, Merge, Size, and Discard directly on that. |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 490 | if !isDescriptor(f.File) { |
| 491 | // NOTE: We avoid adding table-driven support for descriptor proto |
| 492 | // since this depends on the v1 proto package, which would eventually |
| 493 | // need to depend on the descriptor itself. |
| 494 | messageInfoVar := "xxx_messageInfo_" + message.GoIdent.GoName |
| 495 | // XXX_Unmarshal |
| 496 | g.P("func (m *", message.GoIdent, ") XXX_Unmarshal(b []byte) error {") |
| 497 | g.P("return ", messageInfoVar, ".Unmarshal(m, b)") |
| 498 | g.P("}") |
| 499 | // XXX_Marshal |
| 500 | g.P("func (m *", message.GoIdent, ") XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {") |
| 501 | g.P("return ", messageInfoVar, ".Marshal(b, m, deterministic)") |
| 502 | g.P("}") |
| 503 | // XXX_Merge |
| 504 | g.P("func (m *", message.GoIdent, ") XXX_Merge(src proto.Message) {") |
| 505 | g.P(messageInfoVar, ".Merge(m, src)") |
| 506 | g.P("}") |
| 507 | // XXX_Size |
| 508 | g.P("func (m *", message.GoIdent, ") XXX_Size() int {") |
| 509 | g.P("return ", messageInfoVar, ".Size(m)") |
| 510 | g.P("}") |
| 511 | // XXX_DiscardUnknown |
| 512 | g.P("func (m *", message.GoIdent, ") XXX_DiscardUnknown() {") |
| 513 | g.P(messageInfoVar, ".DiscardUnknown(m)") |
| 514 | g.P("}") |
| 515 | g.P() |
| 516 | g.P("var ", messageInfoVar, " ", protoPackage.Ident("InternalMessageInfo")) |
| 517 | g.P() |
| 518 | } |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 519 | |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 520 | // Constants and vars holding the default values of fields. |
| 521 | for _, field := range message.Fields { |
Damien Neil | ccf3fa6 | 2018-09-28 14:41:45 -0700 | [diff] [blame] | 522 | if !fieldHasDefault(field) { |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 523 | continue |
| 524 | } |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 525 | defVarName := "Default_" + message.GoIdent.GoName + "_" + field.GoName |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 526 | def := field.Desc.Default() |
| 527 | switch field.Desc.Kind() { |
| 528 | case protoreflect.StringKind: |
| 529 | g.P("const ", defVarName, " string = ", strconv.Quote(def.String())) |
| 530 | case protoreflect.BytesKind: |
| 531 | g.P("var ", defVarName, " []byte = []byte(", strconv.Quote(string(def.Bytes())), ")") |
| 532 | case protoreflect.EnumKind: |
Damien Neil | a485fbd | 2018-10-26 13:28:37 -0700 | [diff] [blame] | 533 | evalueDesc := field.Desc.DefaultEnumValue() |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 534 | enum := field.EnumType |
Damien Neil | a485fbd | 2018-10-26 13:28:37 -0700 | [diff] [blame] | 535 | evalue := enum.Values[evalueDesc.Index()] |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 536 | g.P("const ", defVarName, " ", field.EnumType.GoIdent, " = ", evalue.GoIdent) |
| 537 | case protoreflect.FloatKind, protoreflect.DoubleKind: |
| 538 | // Floating point numbers need extra handling for -Inf/Inf/NaN. |
| 539 | f := field.Desc.Default().Float() |
| 540 | goType := "float64" |
| 541 | if field.Desc.Kind() == protoreflect.FloatKind { |
| 542 | goType = "float32" |
| 543 | } |
| 544 | // funcCall returns a call to a function in the math package, |
| 545 | // possibly converting the result to float32. |
| 546 | funcCall := func(fn, param string) string { |
Joe Tsai | c1c17aa | 2018-11-16 11:14:14 -0800 | [diff] [blame] | 547 | s := g.QualifiedGoIdent(mathPackage.Ident(fn)) + param |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 548 | if goType != "float64" { |
| 549 | s = goType + "(" + s + ")" |
| 550 | } |
| 551 | return s |
| 552 | } |
| 553 | switch { |
| 554 | case math.IsInf(f, -1): |
| 555 | g.P("var ", defVarName, " ", goType, " = ", funcCall("Inf", "(-1)")) |
| 556 | case math.IsInf(f, 1): |
| 557 | g.P("var ", defVarName, " ", goType, " = ", funcCall("Inf", "(1)")) |
| 558 | case math.IsNaN(f): |
| 559 | g.P("var ", defVarName, " ", goType, " = ", funcCall("NaN", "()")) |
| 560 | default: |
Damien Neil | 982684b | 2018-09-28 14:12:41 -0700 | [diff] [blame] | 561 | g.P("const ", defVarName, " ", goType, " = ", field.Desc.Default().Interface()) |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 562 | } |
| 563 | default: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 564 | goType, _ := fieldGoType(g, field) |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 565 | g.P("const ", defVarName, " ", goType, " = ", def.Interface()) |
| 566 | } |
| 567 | } |
| 568 | g.P() |
| 569 | |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 570 | // Getters. |
| 571 | for _, field := range message.Fields { |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 572 | if field.OneofType != nil { |
| 573 | if field == field.OneofType.Fields[0] { |
| 574 | genOneofTypes(gen, g, f, message, field.OneofType) |
| 575 | } |
| 576 | } |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 577 | goType, pointer := fieldGoType(g, field) |
| 578 | defaultValue := fieldDefaultValue(g, message, field) |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 579 | if field.Desc.Options().(*descriptorpb.FieldOptions).GetDeprecated() { |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 580 | g.P(deprecationComment(true)) |
| 581 | } |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 582 | g.Annotate(message.GoIdent.GoName+".Get"+field.GoName, field.Location) |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 583 | g.P("func (m *", message.GoIdent, ") Get", field.GoName, "() ", goType, " {") |
| 584 | if field.OneofType != nil { |
Damien Neil | 81d6d83 | 2018-09-19 12:12:17 -0700 | [diff] [blame] | 585 | g.P("if x, ok := m.Get", field.OneofType.GoName, "().(*", fieldOneofType(field), "); ok {") |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 586 | g.P("return x.", field.GoName) |
| 587 | g.P("}") |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 588 | } else { |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 589 | if field.Desc.Syntax() == protoreflect.Proto3 || defaultValue == "nil" { |
| 590 | g.P("if m != nil {") |
| 591 | } else { |
| 592 | g.P("if m != nil && m.", field.GoName, " != nil {") |
| 593 | } |
| 594 | star := "" |
| 595 | if pointer { |
| 596 | star = "*" |
| 597 | } |
| 598 | g.P("return ", star, " m.", field.GoName) |
| 599 | g.P("}") |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 600 | } |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 601 | g.P("return ", defaultValue) |
| 602 | g.P("}") |
| 603 | g.P() |
| 604 | } |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 605 | |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 606 | if len(message.Oneofs) > 0 { |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 607 | genOneofWrappers(gen, g, f, message) |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 608 | } |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 609 | for _, extension := range message.Extensions { |
| 610 | genExtension(gen, g, f, extension) |
| 611 | } |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 612 | } |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 613 | |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 614 | // fieldGoType returns the Go type used for a field. |
| 615 | // |
| 616 | // If it returns pointer=true, the struct field is a pointer to the type. |
| 617 | func fieldGoType(g *protogen.GeneratedFile, field *protogen.Field) (goType string, pointer bool) { |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 618 | pointer = true |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 619 | switch field.Desc.Kind() { |
| 620 | case protoreflect.BoolKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 621 | goType = "bool" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 622 | case protoreflect.EnumKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 623 | goType = g.QualifiedGoIdent(field.EnumType.GoIdent) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 624 | case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 625 | goType = "int32" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 626 | case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 627 | goType = "uint32" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 628 | case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 629 | goType = "int64" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 630 | case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 631 | goType = "uint64" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 632 | case protoreflect.FloatKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 633 | goType = "float32" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 634 | case protoreflect.DoubleKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 635 | goType = "float64" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 636 | case protoreflect.StringKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 637 | goType = "string" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 638 | case protoreflect.BytesKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 639 | goType = "[]byte" |
| 640 | pointer = false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 641 | case protoreflect.MessageKind, protoreflect.GroupKind: |
Damien Neil | 0bd5a38 | 2018-09-13 15:07:10 -0700 | [diff] [blame] | 642 | if field.Desc.IsMap() { |
| 643 | keyType, _ := fieldGoType(g, field.MessageType.Fields[0]) |
| 644 | valType, _ := fieldGoType(g, field.MessageType.Fields[1]) |
| 645 | return fmt.Sprintf("map[%v]%v", keyType, valType), false |
| 646 | } |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 647 | goType = "*" + g.QualifiedGoIdent(field.MessageType.GoIdent) |
| 648 | pointer = false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 649 | } |
| 650 | if field.Desc.Cardinality() == protoreflect.Repeated { |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 651 | goType = "[]" + goType |
| 652 | pointer = false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 653 | } |
Damien Neil | 44000a1 | 2018-10-24 12:31:16 -0700 | [diff] [blame] | 654 | // Extension fields always have pointer type, even when defined in a proto3 file. |
| 655 | if field.Desc.Syntax() == protoreflect.Proto3 && field.Desc.ExtendedType() == nil { |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 656 | pointer = false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 657 | } |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 658 | return goType, pointer |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | func fieldProtobufTag(field *protogen.Field) string { |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame] | 662 | var enumName string |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 663 | if field.Desc.Kind() == protoreflect.EnumKind { |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame] | 664 | enumName = enumRegistryName(field.EnumType) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 665 | } |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame] | 666 | return tag.Marshal(field.Desc, enumName) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 669 | func fieldDefaultValue(g *protogen.GeneratedFile, message *protogen.Message, field *protogen.Field) string { |
| 670 | if field.Desc.Cardinality() == protoreflect.Repeated { |
| 671 | return "nil" |
| 672 | } |
Damien Neil | ccf3fa6 | 2018-09-28 14:41:45 -0700 | [diff] [blame] | 673 | if fieldHasDefault(field) { |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 674 | defVarName := "Default_" + message.GoIdent.GoName + "_" + field.GoName |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 675 | if field.Desc.Kind() == protoreflect.BytesKind { |
| 676 | return "append([]byte(nil), " + defVarName + "...)" |
| 677 | } |
| 678 | return defVarName |
| 679 | } |
| 680 | switch field.Desc.Kind() { |
| 681 | case protoreflect.BoolKind: |
| 682 | return "false" |
| 683 | case protoreflect.StringKind: |
| 684 | return `""` |
| 685 | case protoreflect.MessageKind, protoreflect.GroupKind, protoreflect.BytesKind: |
| 686 | return "nil" |
| 687 | case protoreflect.EnumKind: |
| 688 | return g.QualifiedGoIdent(field.EnumType.Values[0].GoIdent) |
| 689 | default: |
| 690 | return "0" |
| 691 | } |
| 692 | } |
| 693 | |
Damien Neil | ccf3fa6 | 2018-09-28 14:41:45 -0700 | [diff] [blame] | 694 | // fieldHasDefault returns true if we consider a field to have a default value. |
| 695 | // |
| 696 | // For consistency with the previous generator, it returns false for fields with |
| 697 | // [default=""], preventing the generation of a default const or var for these |
| 698 | // fields. |
| 699 | // |
| 700 | // TODO: Drop this special case. |
| 701 | func fieldHasDefault(field *protogen.Field) bool { |
| 702 | if !field.Desc.HasDefault() { |
| 703 | return false |
| 704 | } |
| 705 | switch field.Desc.Kind() { |
| 706 | case protoreflect.StringKind: |
| 707 | return field.Desc.Default().String() != "" |
| 708 | case protoreflect.BytesKind: |
| 709 | return len(field.Desc.Default().Bytes()) > 0 |
| 710 | } |
| 711 | return true |
| 712 | } |
| 713 | |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 714 | func fieldJSONTag(field *protogen.Field) string { |
| 715 | return string(field.Desc.Name()) + ",omitempty" |
| 716 | } |
| 717 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 718 | func genExtension(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, extension *protogen.Extension) { |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 719 | // Special case for proto2 message sets: If this extension is extending |
| 720 | // proto2.bridge.MessageSet, and its final name component is "message_set_extension", |
| 721 | // then drop that last component. |
| 722 | // |
| 723 | // TODO: This should be implemented in the text formatter rather than the generator. |
| 724 | // In addition, the situation for when to apply this special case is implemented |
| 725 | // differently in other languages: |
| 726 | // https://github.com/google/protobuf/blob/aff10976/src/google/protobuf/text_format.cc#L1560 |
| 727 | name := extension.Desc.FullName() |
Damien Neil | 6238696 | 2018-10-30 10:35:48 -0700 | [diff] [blame] | 728 | if n, ok := isExtensionMessageSetElement(extension); ok { |
| 729 | name = n |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 732 | g.P("var ", extensionVar(f.File, extension), " = &", f.protoPackage().Ident("ExtensionDesc"), "{") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 733 | g.P("ExtendedType: (*", extension.ExtendedType.GoIdent, ")(nil),") |
| 734 | goType, pointer := fieldGoType(g, extension) |
| 735 | if pointer { |
| 736 | goType = "*" + goType |
| 737 | } |
| 738 | g.P("ExtensionType: (", goType, ")(nil),") |
| 739 | g.P("Field: ", extension.Desc.Number(), ",") |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 740 | g.P("Name: ", strconv.Quote(string(name)), ",") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 741 | g.P("Tag: ", strconv.Quote(fieldProtobufTag(extension)), ",") |
| 742 | g.P("Filename: ", strconv.Quote(f.Desc.Path()), ",") |
| 743 | g.P("}") |
| 744 | g.P() |
| 745 | } |
| 746 | |
Damien Neil | 6238696 | 2018-10-30 10:35:48 -0700 | [diff] [blame] | 747 | // isExtensionMessageSetELement returns the adjusted name of an extension |
| 748 | // which extends proto2.bridge.MessageSet. |
| 749 | func isExtensionMessageSetElement(extension *protogen.Extension) (name protoreflect.FullName, ok bool) { |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 750 | opts := extension.ExtendedType.Desc.Options().(*descriptorpb.MessageOptions) |
Damien Neil | 6238696 | 2018-10-30 10:35:48 -0700 | [diff] [blame] | 751 | if !opts.GetMessageSetWireFormat() || extension.Desc.Name() != "message_set_extension" { |
| 752 | return "", false |
| 753 | } |
| 754 | if extension.ParentMessage == nil { |
| 755 | // This case shouldn't be given special handling at all--we're |
| 756 | // only supposed to drop the ".message_set_extension" for |
| 757 | // extensions defined within a message (i.e., the extension |
| 758 | // takes the message's name). |
| 759 | // |
| 760 | // This matches the behavior of the v1 generator, however. |
| 761 | // |
| 762 | // TODO: See if we can drop this case. |
| 763 | name = extension.Desc.FullName() |
| 764 | name = name[:len(name)-len("message_set_extension")] |
| 765 | return name, true |
| 766 | } |
| 767 | return extension.Desc.FullName().Parent(), true |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 770 | // extensionVar returns the var holding the ExtensionDesc for an extension. |
Damien Neil | 6b54131 | 2018-10-29 09:14:14 -0700 | [diff] [blame] | 771 | func extensionVar(f *protogen.File, extension *protogen.Extension) protogen.GoIdent { |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 772 | name := "E_" |
| 773 | if extension.ParentMessage != nil { |
| 774 | name += extension.ParentMessage.GoIdent.GoName + "_" |
| 775 | } |
| 776 | name += extension.GoName |
Joe Tsai | c1c17aa | 2018-11-16 11:14:14 -0800 | [diff] [blame] | 777 | return f.GoImportPath.Ident(name) |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 780 | // genInitFunction generates an init function that registers the types in the |
| 781 | // generated file with the proto package. |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 782 | func genInitFunction(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) { |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 783 | if len(f.allEnums)+len(f.allMessages)+len(f.allExtensions) == 0 { |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 784 | return |
| 785 | } |
| 786 | |
| 787 | g.P("func init() {") |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 788 | for _, enum := range f.allEnums { |
| 789 | name := enum.GoIdent.GoName |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 790 | g.P(f.protoPackage().Ident("RegisterEnum"), fmt.Sprintf("(%q, %s_name, %s_value)", enumRegistryName(enum), name, name)) |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 791 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 792 | for _, message := range f.allMessages { |
| 793 | if message.Desc.IsMapEntry() { |
| 794 | continue |
| 795 | } |
| 796 | |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 797 | for _, extension := range message.Extensions { |
| 798 | genRegisterExtension(gen, g, f, extension) |
| 799 | } |
| 800 | |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 801 | name := message.GoIdent.GoName |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 802 | g.P(f.protoPackage().Ident("RegisterType"), fmt.Sprintf("((*%s)(nil), %q)", name, message.Desc.FullName())) |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 803 | |
| 804 | // Types of map fields, sorted by the name of the field message type. |
| 805 | var mapFields []*protogen.Field |
| 806 | for _, field := range message.Fields { |
| 807 | if field.Desc.IsMap() { |
| 808 | mapFields = append(mapFields, field) |
| 809 | } |
| 810 | } |
| 811 | sort.Slice(mapFields, func(i, j int) bool { |
| 812 | ni := mapFields[i].MessageType.Desc.FullName() |
| 813 | nj := mapFields[j].MessageType.Desc.FullName() |
| 814 | return ni < nj |
| 815 | }) |
| 816 | for _, field := range mapFields { |
| 817 | typeName := string(field.MessageType.Desc.FullName()) |
| 818 | goType, _ := fieldGoType(g, field) |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 819 | g.P(f.protoPackage().Ident("RegisterMapType"), fmt.Sprintf("((%v)(nil), %q)", goType, typeName)) |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 820 | } |
| 821 | } |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 822 | for _, extension := range f.Extensions { |
| 823 | genRegisterExtension(gen, g, f, extension) |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 824 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 825 | g.P("}") |
| 826 | g.P() |
| 827 | } |
| 828 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 829 | func genRegisterExtension(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, extension *protogen.Extension) { |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame^] | 830 | g.P(f.protoPackage().Ident("RegisterExtension"), "(", extensionVar(f.File, extension), ")") |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 833 | // deprecationComment returns a standard deprecation comment if deprecated is true. |
| 834 | func deprecationComment(deprecated bool) string { |
| 835 | if !deprecated { |
| 836 | return "" |
| 837 | } |
| 838 | return "// Deprecated: Do not use." |
| 839 | } |
| 840 | |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 841 | func genWellKnownType(g *protogen.GeneratedFile, ptr string, ident protogen.GoIdent, desc protoreflect.Descriptor) { |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 842 | if wellKnownTypes[desc.FullName()] { |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 843 | g.P("func (", ptr, ident, `) XXX_WellKnownType() string { return "`, desc.Name(), `" }`) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 844 | g.P() |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | // Names of messages and enums for which we will generate XXX_WellKnownType methods. |
| 849 | var wellKnownTypes = map[protoreflect.FullName]bool{ |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 850 | "google.protobuf.Any": true, |
| 851 | "google.protobuf.Duration": true, |
| 852 | "google.protobuf.Empty": true, |
| 853 | "google.protobuf.Struct": true, |
| 854 | "google.protobuf.Timestamp": true, |
| 855 | |
| 856 | "google.protobuf.BoolValue": true, |
| 857 | "google.protobuf.BytesValue": true, |
| 858 | "google.protobuf.DoubleValue": true, |
| 859 | "google.protobuf.FloatValue": true, |
| 860 | "google.protobuf.Int32Value": true, |
| 861 | "google.protobuf.Int64Value": true, |
| 862 | "google.protobuf.ListValue": true, |
| 863 | "google.protobuf.NullValue": true, |
| 864 | "google.protobuf.StringValue": true, |
| 865 | "google.protobuf.UInt32Value": true, |
| 866 | "google.protobuf.UInt64Value": true, |
| 867 | "google.protobuf.Value": true, |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 868 | } |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 869 | |
| 870 | // genOneofField generates the struct field for a oneof. |
| 871 | func genOneofField(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message, oneof *protogen.Oneof) { |
| 872 | if g.PrintLeadingComments(oneof.Location) { |
| 873 | g.P("//") |
| 874 | } |
| 875 | g.P("// Types that are valid to be assigned to ", oneofFieldName(oneof), ":") |
| 876 | for _, field := range oneof.Fields { |
| 877 | g.PrintLeadingComments(field.Location) |
| 878 | g.P("//\t*", fieldOneofType(field)) |
| 879 | } |
| 880 | g.Annotate(message.GoIdent.GoName+"."+oneofFieldName(oneof), oneof.Location) |
| 881 | g.P(oneofFieldName(oneof), " ", oneofInterfaceName(oneof), " `protobuf_oneof:\"", oneof.Desc.Name(), "\"`") |
| 882 | } |
| 883 | |
| 884 | // genOneofTypes generates the interface type used for a oneof field, |
| 885 | // and the wrapper types that satisfy that interface. |
| 886 | // |
| 887 | // It also generates the getter method for the parent oneof field |
| 888 | // (but not the member fields). |
| 889 | func genOneofTypes(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message, oneof *protogen.Oneof) { |
| 890 | ifName := oneofInterfaceName(oneof) |
| 891 | g.P("type ", ifName, " interface {") |
| 892 | g.P(ifName, "()") |
| 893 | g.P("}") |
| 894 | g.P() |
| 895 | for _, field := range oneof.Fields { |
| 896 | name := fieldOneofType(field) |
| 897 | g.Annotate(name.GoName, field.Location) |
| 898 | g.Annotate(name.GoName+"."+field.GoName, field.Location) |
| 899 | g.P("type ", name, " struct {") |
| 900 | goType, _ := fieldGoType(g, field) |
| 901 | tags := []string{ |
| 902 | fmt.Sprintf("protobuf:%q", fieldProtobufTag(field)), |
| 903 | } |
| 904 | g.P(field.GoName, " ", goType, " `", strings.Join(tags, " "), "`") |
| 905 | g.P("}") |
| 906 | g.P() |
| 907 | } |
| 908 | for _, field := range oneof.Fields { |
| 909 | g.P("func (*", fieldOneofType(field), ") ", ifName, "() {}") |
| 910 | g.P() |
| 911 | } |
| 912 | g.Annotate(message.GoIdent.GoName+".Get"+oneof.GoName, oneof.Location) |
| 913 | g.P("func (m *", message.GoIdent.GoName, ") Get", oneof.GoName, "() ", ifName, " {") |
| 914 | g.P("if m != nil {") |
| 915 | g.P("return m.", oneofFieldName(oneof)) |
| 916 | g.P("}") |
| 917 | g.P("return nil") |
| 918 | g.P("}") |
| 919 | g.P() |
| 920 | } |
| 921 | |
| 922 | // oneofFieldName returns the name of the struct field holding the oneof value. |
| 923 | // |
| 924 | // This function is trivial, but pulling out the name like this makes it easier |
| 925 | // to experiment with alternative oneof implementations. |
| 926 | func oneofFieldName(oneof *protogen.Oneof) string { |
| 927 | return oneof.GoName |
| 928 | } |
| 929 | |
| 930 | // oneofInterfaceName returns the name of the interface type implemented by |
| 931 | // the oneof field value types. |
| 932 | func oneofInterfaceName(oneof *protogen.Oneof) string { |
| 933 | return fmt.Sprintf("is%s_%s", oneof.ParentMessage.GoIdent.GoName, oneof.GoName) |
| 934 | } |
| 935 | |
| 936 | // genOneofWrappers generates the XXX_OneofWrappers method for a message. |
| 937 | func genOneofWrappers(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) { |
| 938 | g.P("// XXX_OneofWrappers is for the internal use of the proto package.") |
| 939 | g.P("func (*", message.GoIdent.GoName, ") XXX_OneofWrappers() []interface{} {") |
| 940 | g.P("return []interface{}{") |
| 941 | for _, oneof := range message.Oneofs { |
| 942 | for _, field := range oneof.Fields { |
| 943 | g.P("(*", fieldOneofType(field), ")(nil),") |
| 944 | } |
| 945 | } |
| 946 | g.P("}") |
| 947 | g.P("}") |
| 948 | g.P() |
| 949 | } |
| 950 | |
| 951 | // fieldOneofType returns the wrapper type used to represent a field in a oneof. |
| 952 | func fieldOneofType(field *protogen.Field) protogen.GoIdent { |
| 953 | ident := protogen.GoIdent{ |
| 954 | GoImportPath: field.ParentMessage.GoIdent.GoImportPath, |
| 955 | GoName: field.ParentMessage.GoIdent.GoName + "_" + field.GoName, |
| 956 | } |
| 957 | // Check for collisions with nested messages or enums. |
| 958 | // |
| 959 | // This conflict resolution is incomplete: Among other things, it |
| 960 | // does not consider collisions with other oneof field types. |
| 961 | // |
| 962 | // TODO: Consider dropping this entirely. Detecting conflicts and |
| 963 | // producing an error is almost certainly better than permuting |
| 964 | // field and type names in mostly unpredictable ways. |
| 965 | Loop: |
| 966 | for { |
| 967 | for _, message := range field.ParentMessage.Messages { |
| 968 | if message.GoIdent == ident { |
| 969 | ident.GoName += "_" |
| 970 | continue Loop |
| 971 | } |
| 972 | } |
| 973 | for _, enum := range field.ParentMessage.Enums { |
| 974 | if enum.GoIdent == ident { |
| 975 | ident.GoName += "_" |
| 976 | continue Loop |
| 977 | } |
| 978 | } |
| 979 | return ident |
| 980 | } |
| 981 | } |