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