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 | "crypto/sha256" |
| 10 | "encoding/hex" |
| 11 | "fmt" |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 12 | "go/ast" |
| 13 | "go/parser" |
| 14 | "go/token" |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 15 | "math" |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 16 | "sort" |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 17 | "strconv" |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 18 | "strings" |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 19 | "unicode" |
| 20 | "unicode/utf8" |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 21 | |
| 22 | "github.com/golang/protobuf/proto" |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame] | 23 | "github.com/golang/protobuf/v2/internal/encoding/tag" |
Joe Tsai | 01ab296 | 2018-09-21 17:44:00 -0700 | [diff] [blame] | 24 | "github.com/golang/protobuf/v2/protogen" |
| 25 | "github.com/golang/protobuf/v2/reflect/protoreflect" |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 26 | |
| 27 | descriptorpb "github.com/golang/protobuf/v2/types/descriptor" |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
Damien Neil | d412792 | 2018-09-12 11:13:49 -0700 | [diff] [blame] | 30 | // generatedCodeVersion indicates a version of the generated code. |
| 31 | // It is incremented whenever an incompatibility between the generated code and |
| 32 | // proto package is introduced; the generated code references |
| 33 | // a constant, proto.ProtoPackageIsVersionN (where N is generatedCodeVersion). |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 34 | const generatedCodeVersion = 3 |
Damien Neil | d412792 | 2018-09-12 11:13:49 -0700 | [diff] [blame] | 35 | |
Joe Tsai | c1c17aa | 2018-11-16 11:14:14 -0800 | [diff] [blame] | 36 | const ( |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame] | 37 | mathPackage = protogen.GoImportPath("math") |
| 38 | protoPackage = protogen.GoImportPath("github.com/golang/protobuf/proto") |
| 39 | protoapiPackage = protogen.GoImportPath("github.com/golang/protobuf/protoapi") |
Joe Tsai | c1c17aa | 2018-11-16 11:14:14 -0800 | [diff] [blame] | 40 | ) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 41 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 42 | type fileInfo struct { |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 43 | *protogen.File |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 44 | |
| 45 | // vars containing the raw wire-encoded and compressed FileDescriptorProto. |
| 46 | descriptorRawVar string |
| 47 | descriptorGzipVar string |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 48 | |
Joe Tsai | 9667c48 | 2018-12-05 15:42:52 -0800 | [diff] [blame] | 49 | allEnums []*protogen.Enum |
| 50 | allEnumsByPtr map[*protogen.Enum]int // value is index into allEnums |
| 51 | allMessages []*protogen.Message |
| 52 | allMessagesByPtr map[*protogen.Message]int // value is index into allMessages |
| 53 | allExtensions []*protogen.Extension |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame] | 56 | // protoPackage returns the package to import, which is either the protoPackage |
| 57 | // or the protoapiPackage constant. |
| 58 | // |
| 59 | // This special casing exists because we are unable to move InternalMessageInfo |
| 60 | // to protoapi since the implementation behind that logic is heavy and |
| 61 | // too intricately connected to other parts of the proto package. |
| 62 | // The descriptor proto is special in that it avoids using InternalMessageInfo |
| 63 | // so that it is able to depend solely on protoapi and break its dependency |
| 64 | // on the proto package. It is still semantically correct for descriptor to |
| 65 | // avoid using InternalMessageInfo, but it does incur some performance penalty. |
| 66 | // This is acceptable for descriptor, which is a single proto file and is not |
| 67 | // known to be in the hot path for any code. |
| 68 | // |
| 69 | // TODO: Remove this special-casing when the table-driven implementation has |
| 70 | // been ported over to v2. |
| 71 | func (f *fileInfo) protoPackage() protogen.GoImportPath { |
| 72 | if isDescriptor(f.File) { |
| 73 | return protoapiPackage |
| 74 | } |
| 75 | return protoPackage |
| 76 | } |
| 77 | |
Damien Neil | 9c420a6 | 2018-09-27 15:26:33 -0700 | [diff] [blame] | 78 | // GenerateFile generates the contents of a .pb.go file. |
| 79 | func GenerateFile(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile) { |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 80 | f := &fileInfo{ |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 81 | File: file, |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 84 | // Collect all enums, messages, and extensions in "flattened ordering". |
| 85 | // See fileinit.FileBuilder. |
Joe Tsai | 9667c48 | 2018-12-05 15:42:52 -0800 | [diff] [blame] | 86 | f.allEnums = append(f.allEnums, f.Enums...) |
| 87 | f.allMessages = append(f.allMessages, f.Messages...) |
| 88 | f.allExtensions = append(f.allExtensions, f.Extensions...) |
| 89 | walkMessages(f.Messages, func(m *protogen.Message) { |
| 90 | f.allEnums = append(f.allEnums, m.Enums...) |
| 91 | f.allMessages = append(f.allMessages, m.Messages...) |
| 92 | f.allExtensions = append(f.allExtensions, m.Extensions...) |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 93 | }) |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 94 | |
Joe Tsai | 9667c48 | 2018-12-05 15:42:52 -0800 | [diff] [blame] | 95 | // Derive a reverse mapping of enum and message pointers to their index |
| 96 | // in allEnums and allMessages. |
| 97 | if len(f.allEnums) > 0 { |
| 98 | f.allEnumsByPtr = make(map[*protogen.Enum]int) |
| 99 | for i, e := range f.allEnums { |
| 100 | f.allEnumsByPtr[e] = i |
| 101 | } |
| 102 | } |
| 103 | if len(f.allMessages) > 0 { |
| 104 | f.allMessagesByPtr = make(map[*protogen.Message]int) |
| 105 | for i, m := range f.allMessages { |
| 106 | f.allMessagesByPtr[m] = i |
| 107 | } |
| 108 | } |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 109 | |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 110 | // Determine the name of the var holding the file descriptor: |
| 111 | // |
| 112 | // fileDescriptor_<hash of filename> |
| 113 | filenameHash := sha256.Sum256([]byte(f.Desc.Path())) |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 114 | f.descriptorRawVar = fmt.Sprintf("fileDescriptor_%s", hex.EncodeToString(filenameHash[:8])) |
| 115 | f.descriptorGzipVar = f.descriptorRawVar + "_gzipped" |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 116 | |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 117 | g.P("// Code generated by protoc-gen-go. DO NOT EDIT.") |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 118 | if f.Proto.GetOptions().GetDeprecated() { |
| 119 | g.P("// ", f.Desc.Path(), " is a deprecated file.") |
| 120 | } else { |
| 121 | g.P("// source: ", f.Desc.Path()) |
| 122 | } |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 123 | g.P() |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 124 | const filePackageField = 2 // FileDescriptorProto.package |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 125 | g.PrintLeadingComments(protogen.Location{ |
| 126 | SourceFile: f.Proto.GetName(), |
| 127 | Path: []int32{filePackageField}, |
| 128 | }) |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 129 | g.P() |
Damien Neil | 082ce92 | 2018-09-06 10:23:53 -0700 | [diff] [blame] | 130 | g.P("package ", f.GoPackageName) |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 131 | g.P() |
Damien Neil | 1ec3315 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 132 | |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame] | 133 | if !isDescriptor(file) { |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame] | 134 | g.P("// This is a compile-time assertion to ensure that this generated file") |
| 135 | g.P("// is compatible with the proto package it is being compiled against.") |
| 136 | g.P("// A compilation error at this line likely means your copy of the") |
| 137 | g.P("// proto package needs to be updated.") |
| 138 | g.P("const _ = ", protoPackage.Ident(fmt.Sprintf("ProtoPackageIsVersion%d", generatedCodeVersion)), |
| 139 | "// please upgrade the proto package") |
| 140 | g.P() |
| 141 | } |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 142 | |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 143 | for i, imps := 0, f.Desc.Imports(); i < imps.Len(); i++ { |
| 144 | genImport(gen, g, f, imps.Get(i)) |
| 145 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 146 | for _, enum := range f.allEnums { |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 147 | genEnum(gen, g, f, enum) |
| 148 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 149 | for _, message := range f.allMessages { |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 150 | genMessage(gen, g, f, message) |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 151 | } |
Joe Tsai | 9667c48 | 2018-12-05 15:42:52 -0800 | [diff] [blame] | 152 | for _, extension := range f.allExtensions { |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 153 | genExtension(gen, g, f, extension) |
| 154 | } |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 155 | |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 156 | genInitFunction(gen, g, f) |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 157 | genFileDescriptor(gen, g, f) |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 158 | genReflectFileDescriptor(gen, g, f) |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 161 | // walkMessages calls f on each message and all of its descendants. |
| 162 | func walkMessages(messages []*protogen.Message, f func(*protogen.Message)) { |
| 163 | for _, m := range messages { |
| 164 | f(m) |
| 165 | walkMessages(m.Messages, f) |
| 166 | } |
| 167 | } |
| 168 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 169 | 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] | 170 | impFile, ok := gen.FileByName(imp.Path()) |
| 171 | if !ok { |
| 172 | return |
| 173 | } |
| 174 | if impFile.GoImportPath == f.GoImportPath { |
Damien Neil | 2e0c3da | 2018-09-19 12:51:36 -0700 | [diff] [blame] | 175 | // Don't generate imports or aliases for types in the same Go package. |
| 176 | return |
| 177 | } |
Damien Neil | 40a0805 | 2018-10-29 09:07:41 -0700 | [diff] [blame] | 178 | // Generate imports for all non-weak dependencies, even if they are not |
Damien Neil | 2e0c3da | 2018-09-19 12:51:36 -0700 | [diff] [blame] | 179 | // referenced, because other code and tools depend on having the |
| 180 | // full transitive closure of protocol buffer types in the binary. |
Damien Neil | 40a0805 | 2018-10-29 09:07:41 -0700 | [diff] [blame] | 181 | if !imp.IsWeak { |
| 182 | g.Import(impFile.GoImportPath) |
| 183 | } |
Damien Neil | 2e0c3da | 2018-09-19 12:51:36 -0700 | [diff] [blame] | 184 | if !imp.IsPublic { |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 185 | return |
| 186 | } |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 187 | |
| 188 | // Generate public imports by generating the imported file, parsing it, |
| 189 | // and extracting every symbol that should receive a forwarding declaration. |
| 190 | impGen := gen.NewGeneratedFile("temp.go", impFile.GoImportPath) |
| 191 | impGen.Skip() |
| 192 | GenerateFile(gen, impFile, impGen) |
| 193 | b, err := impGen.Content() |
| 194 | if err != nil { |
| 195 | gen.Error(err) |
| 196 | return |
| 197 | } |
| 198 | fset := token.NewFileSet() |
| 199 | astFile, err := parser.ParseFile(fset, "", b, parser.ParseComments) |
| 200 | if err != nil { |
| 201 | gen.Error(err) |
| 202 | return |
| 203 | } |
Damien Neil | a7cbd06 | 2019-01-06 16:29:14 -0800 | [diff] [blame] | 204 | genForward := func(tok token.Token, name string, expr ast.Expr) { |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 205 | // Don't import unexported symbols. |
| 206 | r, _ := utf8.DecodeRuneInString(name) |
| 207 | if !unicode.IsUpper(r) { |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 208 | return |
| 209 | } |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 210 | // Don't import the FileDescriptor. |
| 211 | if name == impFile.GoDescriptorIdent.GoName { |
| 212 | return |
| 213 | } |
Damien Neil | a7cbd06 | 2019-01-06 16:29:14 -0800 | [diff] [blame] | 214 | // Don't import decls referencing a symbol defined in another package. |
| 215 | // i.e., don't import decls which are themselves public imports: |
| 216 | // |
| 217 | // type T = somepackage.T |
| 218 | if _, ok := expr.(*ast.SelectorExpr); ok { |
| 219 | return |
| 220 | } |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 221 | g.P(tok, " ", name, " = ", impFile.GoImportPath.Ident(name)) |
| 222 | } |
| 223 | g.P("// Symbols defined in public import of ", imp.Path()) |
| 224 | g.P() |
| 225 | for _, decl := range astFile.Decls { |
| 226 | switch decl := decl.(type) { |
| 227 | case *ast.GenDecl: |
| 228 | for _, spec := range decl.Specs { |
| 229 | switch spec := spec.(type) { |
| 230 | case *ast.TypeSpec: |
Damien Neil | a7cbd06 | 2019-01-06 16:29:14 -0800 | [diff] [blame] | 231 | genForward(decl.Tok, spec.Name.Name, spec.Type) |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 232 | case *ast.ValueSpec: |
Damien Neil | a7cbd06 | 2019-01-06 16:29:14 -0800 | [diff] [blame] | 233 | for i, name := range spec.Names { |
| 234 | var expr ast.Expr |
| 235 | if i < len(spec.Values) { |
| 236 | expr = spec.Values[i] |
| 237 | } |
| 238 | genForward(decl.Tok, name.Name, expr) |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 239 | } |
| 240 | case *ast.ImportSpec: |
| 241 | default: |
| 242 | panic(fmt.Sprintf("can't generate forward for spec type %T", spec)) |
Damien Neil | 7e5c647 | 2018-11-29 08:57:07 -0800 | [diff] [blame] | 243 | } |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 244 | } |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 245 | } |
Damien Neil | 6b54131 | 2018-10-29 09:14:14 -0700 | [diff] [blame] | 246 | } |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 247 | g.P() |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 250 | func genFileDescriptor(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) { |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 251 | // Trim the source_code_info from the descriptor. |
| 252 | // Marshal and gzip it. |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 253 | descProto := proto.Clone(f.Proto).(*descriptorpb.FileDescriptorProto) |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 254 | descProto.SourceCodeInfo = nil |
| 255 | b, err := proto.Marshal(descProto) |
| 256 | if err != nil { |
| 257 | gen.Error(err) |
| 258 | return |
| 259 | } |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 260 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 261 | g.P("var ", f.descriptorRawVar, " = []byte{") |
| 262 | g.P("// ", len(b), " bytes of the wire-encoded FileDescriptorProto") |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 263 | for len(b) > 0 { |
| 264 | n := 16 |
| 265 | if n > len(b) { |
| 266 | n = len(b) |
| 267 | } |
| 268 | |
| 269 | s := "" |
| 270 | for _, c := range b[:n] { |
| 271 | s += fmt.Sprintf("0x%02x,", c) |
| 272 | } |
| 273 | g.P(s) |
| 274 | |
| 275 | b = b[n:] |
| 276 | } |
| 277 | g.P("}") |
| 278 | g.P() |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 279 | |
Joe Tsai | cf81e67 | 2019-02-28 14:08:31 -0800 | [diff] [blame^] | 280 | // TODO: Modify CompressGZIP to lazy encode? Currently, the GZIP'd form |
| 281 | // is eagerly registered in v1, preventing any benefit from lazy encoding. |
| 282 | g.P("var ", f.descriptorGzipVar, " = ", protoapiPackage.Ident("CompressGZIP"), "(", f.descriptorRawVar, ")") |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 283 | g.P() |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 284 | } |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 285 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 286 | 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] | 287 | g.PrintLeadingComments(enum.Location) |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 288 | g.Annotate(enum.GoIdent.GoName, enum.Location) |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 289 | g.P("type ", enum.GoIdent, " int32", |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 290 | deprecationComment(enum.Desc.Options().(*descriptorpb.EnumOptions).GetDeprecated())) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 291 | g.P("const (") |
| 292 | for _, value := range enum.Values { |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 293 | g.PrintLeadingComments(value.Location) |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 294 | g.Annotate(value.GoIdent.GoName, value.Location) |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 295 | g.P(value.GoIdent, " ", enum.GoIdent, " = ", value.Desc.Number(), |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 296 | deprecationComment(value.Desc.Options().(*descriptorpb.EnumValueOptions).GetDeprecated())) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 297 | } |
| 298 | g.P(")") |
| 299 | g.P() |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 300 | |
| 301 | // Generate support for protobuf reflection. |
| 302 | genReflectEnum(gen, g, f, enum) |
| 303 | |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 304 | nameMap := enum.GoIdent.GoName + "_name" |
| 305 | g.P("var ", nameMap, " = map[int32]string{") |
| 306 | generated := make(map[protoreflect.EnumNumber]bool) |
| 307 | for _, value := range enum.Values { |
| 308 | duplicate := "" |
| 309 | if _, present := generated[value.Desc.Number()]; present { |
| 310 | duplicate = "// Duplicate value: " |
| 311 | } |
| 312 | g.P(duplicate, value.Desc.Number(), ": ", strconv.Quote(string(value.Desc.Name())), ",") |
| 313 | generated[value.Desc.Number()] = true |
| 314 | } |
| 315 | g.P("}") |
| 316 | g.P() |
| 317 | valueMap := enum.GoIdent.GoName + "_value" |
| 318 | g.P("var ", valueMap, " = map[string]int32{") |
| 319 | for _, value := range enum.Values { |
| 320 | g.P(strconv.Quote(string(value.Desc.Name())), ": ", value.Desc.Number(), ",") |
| 321 | } |
| 322 | g.P("}") |
| 323 | g.P() |
| 324 | if enum.Desc.Syntax() != protoreflect.Proto3 { |
| 325 | g.P("func (x ", enum.GoIdent, ") Enum() *", enum.GoIdent, " {") |
| 326 | g.P("p := new(", enum.GoIdent, ")") |
| 327 | g.P("*p = x") |
| 328 | g.P("return p") |
| 329 | g.P("}") |
| 330 | g.P() |
| 331 | } |
| 332 | g.P("func (x ", enum.GoIdent, ") String() string {") |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame] | 333 | g.P("return ", f.protoPackage().Ident("EnumName"), "(", enum.GoIdent, "_name, int32(x))") |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 334 | g.P("}") |
| 335 | g.P() |
| 336 | |
Joe Tsai | 7390346 | 2018-12-14 12:22:41 -0800 | [diff] [blame] | 337 | if enum.Desc.Syntax() == protoreflect.Proto2 { |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 338 | g.P("func (x *", enum.GoIdent, ") UnmarshalJSON(data []byte) error {") |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame] | 339 | 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] | 340 | g.P("if err != nil {") |
| 341 | g.P("return err") |
| 342 | g.P("}") |
| 343 | g.P("*x = ", enum.GoIdent, "(value)") |
| 344 | g.P("return nil") |
| 345 | g.P("}") |
| 346 | g.P() |
| 347 | } |
| 348 | |
| 349 | var indexes []string |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 350 | for i := 1; i < len(enum.Location.Path); i += 2 { |
| 351 | indexes = append(indexes, strconv.Itoa(int(enum.Location.Path[i]))) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 352 | } |
| 353 | g.P("func (", enum.GoIdent, ") EnumDescriptor() ([]byte, []int) {") |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 354 | g.P("return ", f.descriptorGzipVar, ", []int{", strings.Join(indexes, ","), "}") |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 355 | g.P("}") |
| 356 | g.P() |
| 357 | |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 358 | genWellKnownType(g, "", enum.GoIdent, enum.Desc) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 361 | // enumRegistryName returns the name used to register an enum with the proto |
| 362 | // package registry. |
| 363 | // |
| 364 | // Confusingly, this is <proto_package>.<go_ident>. This probably should have |
| 365 | // been the full name of the proto enum type instead, but changing it at this |
| 366 | // point would require thought. |
| 367 | func enumRegistryName(enum *protogen.Enum) string { |
| 368 | // Find the FileDescriptor for this enum. |
| 369 | var desc protoreflect.Descriptor = enum.Desc |
| 370 | for { |
| 371 | p, ok := desc.Parent() |
| 372 | if !ok { |
| 373 | break |
| 374 | } |
| 375 | desc = p |
| 376 | } |
| 377 | fdesc := desc.(protoreflect.FileDescriptor) |
Damien Neil | daa4fad | 2018-10-08 14:08:27 -0700 | [diff] [blame] | 378 | if fdesc.Package() == "" { |
| 379 | return enum.GoIdent.GoName |
| 380 | } |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 381 | return string(fdesc.Package()) + "." + enum.GoIdent.GoName |
| 382 | } |
| 383 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 384 | 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] | 385 | if message.Desc.IsMapEntry() { |
| 386 | return |
| 387 | } |
| 388 | |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 389 | hasComment := g.PrintLeadingComments(message.Location) |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 390 | if message.Desc.Options().(*descriptorpb.MessageOptions).GetDeprecated() { |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 391 | if hasComment { |
| 392 | g.P("//") |
| 393 | } |
| 394 | g.P(deprecationComment(true)) |
| 395 | } |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 396 | g.Annotate(message.GoIdent.GoName, message.Location) |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 397 | g.P("type ", message.GoIdent, " struct {") |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 398 | for _, field := range message.Fields { |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 399 | if field.OneofType != nil { |
| 400 | // It would be a bit simpler to iterate over the oneofs below, |
| 401 | // but generating the field here keeps the contents of the Go |
| 402 | // struct in the same order as the contents of the source |
| 403 | // .proto file. |
| 404 | if field == field.OneofType.Fields[0] { |
| 405 | genOneofField(gen, g, f, message, field.OneofType) |
| 406 | } |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 407 | continue |
| 408 | } |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 409 | g.PrintLeadingComments(field.Location) |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 410 | goType, pointer := fieldGoType(g, field) |
| 411 | if pointer { |
| 412 | goType = "*" + goType |
| 413 | } |
Damien Neil | 0bd5a38 | 2018-09-13 15:07:10 -0700 | [diff] [blame] | 414 | tags := []string{ |
| 415 | fmt.Sprintf("protobuf:%q", fieldProtobufTag(field)), |
| 416 | fmt.Sprintf("json:%q", fieldJSONTag(field)), |
| 417 | } |
| 418 | if field.Desc.IsMap() { |
| 419 | key := field.MessageType.Fields[0] |
| 420 | val := field.MessageType.Fields[1] |
| 421 | tags = append(tags, |
| 422 | fmt.Sprintf("protobuf_key:%q", fieldProtobufTag(key)), |
| 423 | fmt.Sprintf("protobuf_val:%q", fieldProtobufTag(val)), |
| 424 | ) |
| 425 | } |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 426 | g.Annotate(message.GoIdent.GoName+"."+field.GoName, field.Location) |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 427 | g.P(field.GoName, " ", goType, " `", strings.Join(tags, " "), "`", |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 428 | deprecationComment(field.Desc.Options().(*descriptorpb.FieldOptions).GetDeprecated())) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 429 | } |
| 430 | g.P("XXX_NoUnkeyedLiteral struct{} `json:\"-\"`") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 431 | |
| 432 | if message.Desc.ExtensionRanges().Len() > 0 { |
| 433 | var tags []string |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 434 | if message.Desc.Options().(*descriptorpb.MessageOptions).GetMessageSetWireFormat() { |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 435 | tags = append(tags, `protobuf_messageset:"1"`) |
| 436 | } |
| 437 | tags = append(tags, `json:"-"`) |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame] | 438 | g.P(f.protoPackage().Ident("XXX_InternalExtensions"), " `", strings.Join(tags, " "), "`") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 439 | } |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 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) {") |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 460 | g.P("return ", f.descriptorGzipVar, ", []int{", strings.Join(indexes, ","), "}") |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 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 { |
Joe Tsai | 9667c48 | 2018-12-05 15:42:52 -0800 | [diff] [blame] | 522 | if !field.Desc.HasDefault() { |
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 | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 609 | } |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 610 | |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 611 | // fieldGoType returns the Go type used for a field. |
| 612 | // |
| 613 | // If it returns pointer=true, the struct field is a pointer to the type. |
| 614 | func fieldGoType(g *protogen.GeneratedFile, field *protogen.Field) (goType string, pointer bool) { |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 615 | pointer = true |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 616 | switch field.Desc.Kind() { |
| 617 | case protoreflect.BoolKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 618 | goType = "bool" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 619 | case protoreflect.EnumKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 620 | goType = g.QualifiedGoIdent(field.EnumType.GoIdent) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 621 | case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 622 | goType = "int32" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 623 | case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 624 | goType = "uint32" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 625 | case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 626 | goType = "int64" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 627 | case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 628 | goType = "uint64" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 629 | case protoreflect.FloatKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 630 | goType = "float32" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 631 | case protoreflect.DoubleKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 632 | goType = "float64" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 633 | case protoreflect.StringKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 634 | goType = "string" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 635 | case protoreflect.BytesKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 636 | goType = "[]byte" |
| 637 | pointer = false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 638 | case protoreflect.MessageKind, protoreflect.GroupKind: |
Damien Neil | 0bd5a38 | 2018-09-13 15:07:10 -0700 | [diff] [blame] | 639 | if field.Desc.IsMap() { |
| 640 | keyType, _ := fieldGoType(g, field.MessageType.Fields[0]) |
| 641 | valType, _ := fieldGoType(g, field.MessageType.Fields[1]) |
| 642 | return fmt.Sprintf("map[%v]%v", keyType, valType), false |
| 643 | } |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 644 | goType = "*" + g.QualifiedGoIdent(field.MessageType.GoIdent) |
| 645 | pointer = false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 646 | } |
| 647 | if field.Desc.Cardinality() == protoreflect.Repeated { |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 648 | goType = "[]" + goType |
| 649 | pointer = false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 650 | } |
Damien Neil | 44000a1 | 2018-10-24 12:31:16 -0700 | [diff] [blame] | 651 | // Extension fields always have pointer type, even when defined in a proto3 file. |
| 652 | if field.Desc.Syntax() == protoreflect.Proto3 && field.Desc.ExtendedType() == nil { |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 653 | pointer = false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 654 | } |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 655 | return goType, pointer |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | func fieldProtobufTag(field *protogen.Field) string { |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame] | 659 | var enumName string |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 660 | if field.Desc.Kind() == protoreflect.EnumKind { |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame] | 661 | enumName = enumRegistryName(field.EnumType) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 662 | } |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame] | 663 | return tag.Marshal(field.Desc, enumName) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 666 | func fieldDefaultValue(g *protogen.GeneratedFile, message *protogen.Message, field *protogen.Field) string { |
| 667 | if field.Desc.Cardinality() == protoreflect.Repeated { |
| 668 | return "nil" |
| 669 | } |
Joe Tsai | 9667c48 | 2018-12-05 15:42:52 -0800 | [diff] [blame] | 670 | if field.Desc.HasDefault() { |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 671 | defVarName := "Default_" + message.GoIdent.GoName + "_" + field.GoName |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 672 | if field.Desc.Kind() == protoreflect.BytesKind { |
| 673 | return "append([]byte(nil), " + defVarName + "...)" |
| 674 | } |
| 675 | return defVarName |
| 676 | } |
| 677 | switch field.Desc.Kind() { |
| 678 | case protoreflect.BoolKind: |
| 679 | return "false" |
| 680 | case protoreflect.StringKind: |
| 681 | return `""` |
| 682 | case protoreflect.MessageKind, protoreflect.GroupKind, protoreflect.BytesKind: |
| 683 | return "nil" |
| 684 | case protoreflect.EnumKind: |
| 685 | return g.QualifiedGoIdent(field.EnumType.Values[0].GoIdent) |
| 686 | default: |
| 687 | return "0" |
| 688 | } |
| 689 | } |
| 690 | |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 691 | func fieldJSONTag(field *protogen.Field) string { |
| 692 | return string(field.Desc.Name()) + ",omitempty" |
| 693 | } |
| 694 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 695 | 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] | 696 | // Special case for proto2 message sets: If this extension is extending |
| 697 | // proto2.bridge.MessageSet, and its final name component is "message_set_extension", |
| 698 | // then drop that last component. |
| 699 | // |
| 700 | // TODO: This should be implemented in the text formatter rather than the generator. |
| 701 | // In addition, the situation for when to apply this special case is implemented |
| 702 | // differently in other languages: |
| 703 | // https://github.com/google/protobuf/blob/aff10976/src/google/protobuf/text_format.cc#L1560 |
| 704 | name := extension.Desc.FullName() |
Damien Neil | 6238696 | 2018-10-30 10:35:48 -0700 | [diff] [blame] | 705 | if n, ok := isExtensionMessageSetElement(extension); ok { |
| 706 | name = n |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame] | 709 | g.P("var ", extensionVar(f.File, extension), " = &", f.protoPackage().Ident("ExtensionDesc"), "{") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 710 | g.P("ExtendedType: (*", extension.ExtendedType.GoIdent, ")(nil),") |
| 711 | goType, pointer := fieldGoType(g, extension) |
| 712 | if pointer { |
| 713 | goType = "*" + goType |
| 714 | } |
| 715 | g.P("ExtensionType: (", goType, ")(nil),") |
| 716 | g.P("Field: ", extension.Desc.Number(), ",") |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 717 | g.P("Name: ", strconv.Quote(string(name)), ",") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 718 | g.P("Tag: ", strconv.Quote(fieldProtobufTag(extension)), ",") |
| 719 | g.P("Filename: ", strconv.Quote(f.Desc.Path()), ",") |
| 720 | g.P("}") |
| 721 | g.P() |
| 722 | } |
| 723 | |
Damien Neil | 6238696 | 2018-10-30 10:35:48 -0700 | [diff] [blame] | 724 | // isExtensionMessageSetELement returns the adjusted name of an extension |
| 725 | // which extends proto2.bridge.MessageSet. |
| 726 | func isExtensionMessageSetElement(extension *protogen.Extension) (name protoreflect.FullName, ok bool) { |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 727 | opts := extension.ExtendedType.Desc.Options().(*descriptorpb.MessageOptions) |
Damien Neil | 6238696 | 2018-10-30 10:35:48 -0700 | [diff] [blame] | 728 | if !opts.GetMessageSetWireFormat() || extension.Desc.Name() != "message_set_extension" { |
| 729 | return "", false |
| 730 | } |
| 731 | if extension.ParentMessage == nil { |
| 732 | // This case shouldn't be given special handling at all--we're |
| 733 | // only supposed to drop the ".message_set_extension" for |
| 734 | // extensions defined within a message (i.e., the extension |
| 735 | // takes the message's name). |
| 736 | // |
| 737 | // This matches the behavior of the v1 generator, however. |
| 738 | // |
| 739 | // TODO: See if we can drop this case. |
| 740 | name = extension.Desc.FullName() |
| 741 | name = name[:len(name)-len("message_set_extension")] |
| 742 | return name, true |
| 743 | } |
| 744 | return extension.Desc.FullName().Parent(), true |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 747 | // extensionVar returns the var holding the ExtensionDesc for an extension. |
Damien Neil | 6b54131 | 2018-10-29 09:14:14 -0700 | [diff] [blame] | 748 | func extensionVar(f *protogen.File, extension *protogen.Extension) protogen.GoIdent { |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 749 | name := "E_" |
| 750 | if extension.ParentMessage != nil { |
| 751 | name += extension.ParentMessage.GoIdent.GoName + "_" |
| 752 | } |
| 753 | name += extension.GoName |
Joe Tsai | c1c17aa | 2018-11-16 11:14:14 -0800 | [diff] [blame] | 754 | return f.GoImportPath.Ident(name) |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 757 | // genInitFunction generates an init function that registers the types in the |
| 758 | // generated file with the proto package. |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 759 | func genInitFunction(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) { |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 760 | g.P("func init() {") |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 761 | g.P(f.protoPackage().Ident("RegisterFile"), "(", strconv.Quote(f.Desc.Path()), ", ", f.descriptorGzipVar, ")") |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 762 | for _, enum := range f.allEnums { |
| 763 | name := enum.GoIdent.GoName |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame] | 764 | 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] | 765 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 766 | for _, message := range f.allMessages { |
| 767 | if message.Desc.IsMapEntry() { |
| 768 | continue |
| 769 | } |
| 770 | |
| 771 | name := message.GoIdent.GoName |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame] | 772 | 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] | 773 | |
| 774 | // Types of map fields, sorted by the name of the field message type. |
| 775 | var mapFields []*protogen.Field |
| 776 | for _, field := range message.Fields { |
| 777 | if field.Desc.IsMap() { |
| 778 | mapFields = append(mapFields, field) |
| 779 | } |
| 780 | } |
| 781 | sort.Slice(mapFields, func(i, j int) bool { |
| 782 | ni := mapFields[i].MessageType.Desc.FullName() |
| 783 | nj := mapFields[j].MessageType.Desc.FullName() |
| 784 | return ni < nj |
| 785 | }) |
| 786 | for _, field := range mapFields { |
| 787 | typeName := string(field.MessageType.Desc.FullName()) |
| 788 | goType, _ := fieldGoType(g, field) |
Joe Tsai | 24ceb2b | 2018-12-04 22:53:56 -0800 | [diff] [blame] | 789 | 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] | 790 | } |
| 791 | } |
Joe Tsai | 9667c48 | 2018-12-05 15:42:52 -0800 | [diff] [blame] | 792 | for _, extension := range f.allExtensions { |
| 793 | g.P(f.protoPackage().Ident("RegisterExtension"), "(", extensionVar(f.File, extension), ")") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 794 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 795 | g.P("}") |
| 796 | g.P() |
| 797 | } |
| 798 | |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 799 | // deprecationComment returns a standard deprecation comment if deprecated is true. |
| 800 | func deprecationComment(deprecated bool) string { |
| 801 | if !deprecated { |
| 802 | return "" |
| 803 | } |
| 804 | return "// Deprecated: Do not use." |
| 805 | } |
| 806 | |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 807 | 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] | 808 | if wellKnownTypes[desc.FullName()] { |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 809 | g.P("func (", ptr, ident, `) XXX_WellKnownType() string { return "`, desc.Name(), `" }`) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 810 | g.P() |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | // Names of messages and enums for which we will generate XXX_WellKnownType methods. |
| 815 | var wellKnownTypes = map[protoreflect.FullName]bool{ |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 816 | "google.protobuf.Any": true, |
| 817 | "google.protobuf.Duration": true, |
| 818 | "google.protobuf.Empty": true, |
| 819 | "google.protobuf.Struct": true, |
| 820 | "google.protobuf.Timestamp": true, |
| 821 | |
| 822 | "google.protobuf.BoolValue": true, |
| 823 | "google.protobuf.BytesValue": true, |
| 824 | "google.protobuf.DoubleValue": true, |
| 825 | "google.protobuf.FloatValue": true, |
| 826 | "google.protobuf.Int32Value": true, |
| 827 | "google.protobuf.Int64Value": true, |
| 828 | "google.protobuf.ListValue": true, |
| 829 | "google.protobuf.NullValue": true, |
| 830 | "google.protobuf.StringValue": true, |
| 831 | "google.protobuf.UInt32Value": true, |
| 832 | "google.protobuf.UInt64Value": true, |
| 833 | "google.protobuf.Value": true, |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 834 | } |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 835 | |
| 836 | // genOneofField generates the struct field for a oneof. |
| 837 | func genOneofField(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message, oneof *protogen.Oneof) { |
| 838 | if g.PrintLeadingComments(oneof.Location) { |
| 839 | g.P("//") |
| 840 | } |
| 841 | g.P("// Types that are valid to be assigned to ", oneofFieldName(oneof), ":") |
| 842 | for _, field := range oneof.Fields { |
| 843 | g.PrintLeadingComments(field.Location) |
| 844 | g.P("//\t*", fieldOneofType(field)) |
| 845 | } |
| 846 | g.Annotate(message.GoIdent.GoName+"."+oneofFieldName(oneof), oneof.Location) |
| 847 | g.P(oneofFieldName(oneof), " ", oneofInterfaceName(oneof), " `protobuf_oneof:\"", oneof.Desc.Name(), "\"`") |
| 848 | } |
| 849 | |
| 850 | // genOneofTypes generates the interface type used for a oneof field, |
| 851 | // and the wrapper types that satisfy that interface. |
| 852 | // |
| 853 | // It also generates the getter method for the parent oneof field |
| 854 | // (but not the member fields). |
| 855 | func genOneofTypes(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message, oneof *protogen.Oneof) { |
| 856 | ifName := oneofInterfaceName(oneof) |
| 857 | g.P("type ", ifName, " interface {") |
| 858 | g.P(ifName, "()") |
| 859 | g.P("}") |
| 860 | g.P() |
| 861 | for _, field := range oneof.Fields { |
| 862 | name := fieldOneofType(field) |
| 863 | g.Annotate(name.GoName, field.Location) |
| 864 | g.Annotate(name.GoName+"."+field.GoName, field.Location) |
| 865 | g.P("type ", name, " struct {") |
| 866 | goType, _ := fieldGoType(g, field) |
| 867 | tags := []string{ |
| 868 | fmt.Sprintf("protobuf:%q", fieldProtobufTag(field)), |
| 869 | } |
| 870 | g.P(field.GoName, " ", goType, " `", strings.Join(tags, " "), "`") |
| 871 | g.P("}") |
| 872 | g.P() |
| 873 | } |
| 874 | for _, field := range oneof.Fields { |
| 875 | g.P("func (*", fieldOneofType(field), ") ", ifName, "() {}") |
| 876 | g.P() |
| 877 | } |
| 878 | g.Annotate(message.GoIdent.GoName+".Get"+oneof.GoName, oneof.Location) |
| 879 | g.P("func (m *", message.GoIdent.GoName, ") Get", oneof.GoName, "() ", ifName, " {") |
| 880 | g.P("if m != nil {") |
| 881 | g.P("return m.", oneofFieldName(oneof)) |
| 882 | g.P("}") |
| 883 | g.P("return nil") |
| 884 | g.P("}") |
| 885 | g.P() |
| 886 | } |
| 887 | |
| 888 | // oneofFieldName returns the name of the struct field holding the oneof value. |
| 889 | // |
| 890 | // This function is trivial, but pulling out the name like this makes it easier |
| 891 | // to experiment with alternative oneof implementations. |
| 892 | func oneofFieldName(oneof *protogen.Oneof) string { |
| 893 | return oneof.GoName |
| 894 | } |
| 895 | |
| 896 | // oneofInterfaceName returns the name of the interface type implemented by |
| 897 | // the oneof field value types. |
| 898 | func oneofInterfaceName(oneof *protogen.Oneof) string { |
| 899 | return fmt.Sprintf("is%s_%s", oneof.ParentMessage.GoIdent.GoName, oneof.GoName) |
| 900 | } |
| 901 | |
| 902 | // genOneofWrappers generates the XXX_OneofWrappers method for a message. |
| 903 | func genOneofWrappers(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) { |
| 904 | g.P("// XXX_OneofWrappers is for the internal use of the proto package.") |
| 905 | g.P("func (*", message.GoIdent.GoName, ") XXX_OneofWrappers() []interface{} {") |
| 906 | g.P("return []interface{}{") |
| 907 | for _, oneof := range message.Oneofs { |
| 908 | for _, field := range oneof.Fields { |
| 909 | g.P("(*", fieldOneofType(field), ")(nil),") |
| 910 | } |
| 911 | } |
| 912 | g.P("}") |
| 913 | g.P("}") |
| 914 | g.P() |
| 915 | } |
| 916 | |
| 917 | // fieldOneofType returns the wrapper type used to represent a field in a oneof. |
| 918 | func fieldOneofType(field *protogen.Field) protogen.GoIdent { |
| 919 | ident := protogen.GoIdent{ |
| 920 | GoImportPath: field.ParentMessage.GoIdent.GoImportPath, |
| 921 | GoName: field.ParentMessage.GoIdent.GoName + "_" + field.GoName, |
| 922 | } |
| 923 | // Check for collisions with nested messages or enums. |
| 924 | // |
| 925 | // This conflict resolution is incomplete: Among other things, it |
| 926 | // does not consider collisions with other oneof field types. |
| 927 | // |
| 928 | // TODO: Consider dropping this entirely. Detecting conflicts and |
| 929 | // producing an error is almost certainly better than permuting |
| 930 | // field and type names in mostly unpredictable ways. |
| 931 | Loop: |
| 932 | for { |
| 933 | for _, message := range field.ParentMessage.Messages { |
| 934 | if message.GoIdent == ident { |
| 935 | ident.GoName += "_" |
| 936 | continue Loop |
| 937 | } |
| 938 | } |
| 939 | for _, enum := range field.ParentMessage.Enums { |
| 940 | if enum.GoIdent == ident { |
| 941 | ident.GoName += "_" |
| 942 | continue Loop |
| 943 | } |
| 944 | } |
| 945 | return ident |
| 946 | } |
| 947 | } |