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