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 | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 14 | "strconv" |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 15 | "strings" |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 16 | "unicode" |
| 17 | "unicode/utf8" |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 18 | |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 19 | "google.golang.org/protobuf/compiler/protogen" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 20 | "google.golang.org/protobuf/internal/encoding/tag" |
| 21 | "google.golang.org/protobuf/internal/fieldnum" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 22 | "google.golang.org/protobuf/reflect/protoreflect" |
Joe Tsai | 58b42d8 | 2019-05-22 16:27:51 -0400 | [diff] [blame] | 23 | "google.golang.org/protobuf/runtime/protoimpl" |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 24 | |
Joe Tsai | a95b29f | 2019-05-16 12:47:20 -0700 | [diff] [blame] | 25 | "google.golang.org/protobuf/types/descriptorpb" |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
Joe Tsai | c1c17aa | 2018-11-16 11:14:14 -0800 | [diff] [blame] | 28 | const ( |
Joe Tsai | ab61d41 | 2019-04-16 15:23:29 -0700 | [diff] [blame] | 29 | // generateEnumMapVars specifies whether to generate enum maps, |
| 30 | // which provide a bi-directional mapping between enum numbers and names. |
| 31 | generateEnumMapVars = true |
| 32 | |
| 33 | // generateRawDescMethods specifies whether to generate EnumDescriptor and |
| 34 | // Descriptor methods for enums and messages. These methods return the |
| 35 | // GZIP'd contents of the raw file descriptor and the path from the root |
| 36 | // to the given enum or message descriptor. |
| 37 | generateRawDescMethods = true |
Joe Tsai | 0991227 | 2019-07-08 10:38:11 -0700 | [diff] [blame] | 38 | |
| 39 | // generateOneofWrapperMethods specifies whether to generate |
| 40 | // XXX_OneofWrappers methods on messages with oneofs. |
| 41 | generateOneofWrapperMethods = false |
Joe Tsai | c0e4bb2 | 2019-07-06 13:05:11 -0700 | [diff] [blame] | 42 | |
| 43 | // generateNoUnkeyedLiteralFields specifies whether to generate |
| 44 | // the XXX_NoUnkeyedLiteral field. |
| 45 | generateNoUnkeyedLiteralFields = false |
| 46 | |
| 47 | // generateExportedSizeCacheFields specifies whether to generate an exported |
| 48 | // XXX_sizecache field instead of an unexported sizeCache field. |
| 49 | generateExportedSizeCacheFields = false |
| 50 | |
| 51 | // generateExportedUnknownFields specifies whether to generate an exported |
| 52 | // XXX_unrecognized field instead of an unexported unknownFields field. |
| 53 | generateExportedUnknownFields = false |
| 54 | |
| 55 | // generateExportedExtensionFields specifies whether to generate an exported |
| 56 | // XXX_InternalExtensions field instead of an unexported extensionFields field. |
| 57 | generateExportedExtensionFields = false |
Joe Tsai | ab61d41 | 2019-04-16 15:23:29 -0700 | [diff] [blame] | 58 | ) |
| 59 | |
| 60 | const ( |
Joe Tsai | 5d72cc2 | 2019-03-28 01:13:26 -0700 | [diff] [blame] | 61 | syncPackage = protogen.GoImportPath("sync") |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 62 | mathPackage = protogen.GoImportPath("math") |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 63 | protoifacePackage = protogen.GoImportPath("google.golang.org/protobuf/runtime/protoiface") |
| 64 | protoimplPackage = protogen.GoImportPath("google.golang.org/protobuf/runtime/protoimpl") |
| 65 | protoreflectPackage = protogen.GoImportPath("google.golang.org/protobuf/reflect/protoreflect") |
| 66 | protoregistryPackage = protogen.GoImportPath("google.golang.org/protobuf/reflect/protoregistry") |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 67 | prototypePackage = protogen.GoImportPath("google.golang.org/protobuf/reflect/prototype") |
Joe Tsai | c1c17aa | 2018-11-16 11:14:14 -0800 | [diff] [blame] | 68 | ) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 69 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 70 | type fileInfo struct { |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 71 | *protogen.File |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 72 | |
Joe Tsai | c0e4bb2 | 2019-07-06 13:05:11 -0700 | [diff] [blame] | 73 | allEnums []*protogen.Enum |
| 74 | allMessages []*protogen.Message |
| 75 | allExtensions []*protogen.Extension |
| 76 | |
| 77 | allEnumsByPtr map[*protogen.Enum]int // value is index into allEnums |
| 78 | allMessagesByPtr map[*protogen.Message]int // value is index into allMessages |
| 79 | allMessageFieldsByPtr map[*protogen.Message]*structFields |
| 80 | } |
| 81 | |
| 82 | type structFields struct { |
| 83 | count int |
| 84 | unexported map[int]string |
| 85 | } |
| 86 | |
| 87 | func (sf *structFields) append(name string) { |
| 88 | if r, _ := utf8.DecodeRuneInString(name); !unicode.IsUpper(r) { |
| 89 | if sf.unexported == nil { |
| 90 | sf.unexported = make(map[int]string) |
| 91 | } |
| 92 | sf.unexported[sf.count] = name |
| 93 | } |
| 94 | sf.count++ |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Damien Neil | 9c420a6 | 2018-09-27 15:26:33 -0700 | [diff] [blame] | 97 | // GenerateFile generates the contents of a .pb.go file. |
Joe Tsai | 1905843 | 2019-02-27 21:46:29 -0800 | [diff] [blame] | 98 | func GenerateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile { |
| 99 | filename := file.GeneratedFilenamePrefix + ".pb.go" |
| 100 | g := gen.NewGeneratedFile(filename, file.GoImportPath) |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 101 | f := &fileInfo{ |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 102 | File: file, |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Damien Neil | 8012b44 | 2019-01-18 09:32:24 -0800 | [diff] [blame] | 105 | // Collect all enums, messages, and extensions in "flattened ordering". |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 106 | // See filetype.TypeBuilder. |
Joe Tsai | 9667c48 | 2018-12-05 15:42:52 -0800 | [diff] [blame] | 107 | f.allEnums = append(f.allEnums, f.Enums...) |
| 108 | f.allMessages = append(f.allMessages, f.Messages...) |
| 109 | f.allExtensions = append(f.allExtensions, f.Extensions...) |
| 110 | walkMessages(f.Messages, func(m *protogen.Message) { |
| 111 | f.allEnums = append(f.allEnums, m.Enums...) |
| 112 | f.allMessages = append(f.allMessages, m.Messages...) |
| 113 | f.allExtensions = append(f.allExtensions, m.Extensions...) |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 114 | }) |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 115 | |
Joe Tsai | 9667c48 | 2018-12-05 15:42:52 -0800 | [diff] [blame] | 116 | // Derive a reverse mapping of enum and message pointers to their index |
| 117 | // in allEnums and allMessages. |
| 118 | if len(f.allEnums) > 0 { |
| 119 | f.allEnumsByPtr = make(map[*protogen.Enum]int) |
| 120 | for i, e := range f.allEnums { |
| 121 | f.allEnumsByPtr[e] = i |
| 122 | } |
| 123 | } |
| 124 | if len(f.allMessages) > 0 { |
| 125 | f.allMessagesByPtr = make(map[*protogen.Message]int) |
Joe Tsai | c0e4bb2 | 2019-07-06 13:05:11 -0700 | [diff] [blame] | 126 | f.allMessageFieldsByPtr = make(map[*protogen.Message]*structFields) |
Joe Tsai | 9667c48 | 2018-12-05 15:42:52 -0800 | [diff] [blame] | 127 | for i, m := range f.allMessages { |
| 128 | f.allMessagesByPtr[m] = i |
Joe Tsai | c0e4bb2 | 2019-07-06 13:05:11 -0700 | [diff] [blame] | 129 | f.allMessageFieldsByPtr[m] = new(structFields) |
Joe Tsai | 9667c48 | 2018-12-05 15:42:52 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 132 | |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 133 | g.P("// Code generated by protoc-gen-go. DO NOT EDIT.") |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 134 | if f.Proto.GetOptions().GetDeprecated() { |
| 135 | g.P("// ", f.Desc.Path(), " is a deprecated file.") |
| 136 | } else { |
| 137 | g.P("// source: ", f.Desc.Path()) |
| 138 | } |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 139 | g.P() |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 140 | g.PrintLeadingComments(protogen.Location{ |
| 141 | SourceFile: f.Proto.GetName(), |
Joe Tsai | ca46d8c | 2019-03-20 16:51:09 -0700 | [diff] [blame] | 142 | Path: []int32{fieldnum.FileDescriptorProto_Package}, |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 143 | }) |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 144 | g.P() |
Damien Neil | 082ce92 | 2018-09-06 10:23:53 -0700 | [diff] [blame] | 145 | g.P("package ", f.GoPackageName) |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 146 | g.P() |
Damien Neil | 1ec3315 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 147 | |
Joe Tsai | 5d72cc2 | 2019-03-28 01:13:26 -0700 | [diff] [blame] | 148 | // Emit a static check that enforces a minimum version of the proto package. |
Joe Tsai | 58b42d8 | 2019-05-22 16:27:51 -0400 | [diff] [blame] | 149 | g.P("const (") |
| 150 | g.P("// Verify that runtime/protoimpl is sufficiently up-to-date.") |
| 151 | g.P("_ = ", protoimplPackage.Ident("EnforceVersion"), "(", protoimplPackage.Ident("MaxVersion"), " - ", protoimpl.Version, ")") |
| 152 | g.P("// Verify that this generated code is sufficiently up-to-date.") |
| 153 | g.P("_ = ", protoimplPackage.Ident("EnforceVersion"), "(", protoimpl.Version, " - ", protoimplPackage.Ident("MinVersion"), ")") |
| 154 | g.P(")") |
Joe Tsai | 5d72cc2 | 2019-03-28 01:13:26 -0700 | [diff] [blame] | 155 | g.P() |
| 156 | |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 157 | for i, imps := 0, f.Desc.Imports(); i < imps.Len(); i++ { |
| 158 | genImport(gen, g, f, imps.Get(i)) |
| 159 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 160 | for _, enum := range f.allEnums { |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 161 | genEnum(gen, g, f, enum) |
| 162 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 163 | for _, message := range f.allMessages { |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 164 | genMessage(gen, g, f, message) |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 165 | } |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 166 | genExtensions(gen, g, f) |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 167 | |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 168 | genReflectFileDescriptor(gen, g, f) |
Joe Tsai | 1905843 | 2019-02-27 21:46:29 -0800 | [diff] [blame] | 169 | |
| 170 | return g |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 173 | // walkMessages calls f on each message and all of its descendants. |
| 174 | func walkMessages(messages []*protogen.Message, f func(*protogen.Message)) { |
| 175 | for _, m := range messages { |
| 176 | f(m) |
| 177 | walkMessages(m.Messages, f) |
| 178 | } |
| 179 | } |
| 180 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 181 | 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] | 182 | impFile, ok := gen.FileByName(imp.Path()) |
| 183 | if !ok { |
| 184 | return |
| 185 | } |
| 186 | if impFile.GoImportPath == f.GoImportPath { |
Damien Neil | 2e0c3da | 2018-09-19 12:51:36 -0700 | [diff] [blame] | 187 | // Don't generate imports or aliases for types in the same Go package. |
| 188 | return |
| 189 | } |
Damien Neil | 40a0805 | 2018-10-29 09:07:41 -0700 | [diff] [blame] | 190 | // Generate imports for all non-weak dependencies, even if they are not |
Damien Neil | 2e0c3da | 2018-09-19 12:51:36 -0700 | [diff] [blame] | 191 | // referenced, because other code and tools depend on having the |
| 192 | // full transitive closure of protocol buffer types in the binary. |
Damien Neil | 40a0805 | 2018-10-29 09:07:41 -0700 | [diff] [blame] | 193 | if !imp.IsWeak { |
| 194 | g.Import(impFile.GoImportPath) |
| 195 | } |
Damien Neil | 2e0c3da | 2018-09-19 12:51:36 -0700 | [diff] [blame] | 196 | if !imp.IsPublic { |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 197 | return |
| 198 | } |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 199 | |
| 200 | // Generate public imports by generating the imported file, parsing it, |
| 201 | // and extracting every symbol that should receive a forwarding declaration. |
Joe Tsai | 1905843 | 2019-02-27 21:46:29 -0800 | [diff] [blame] | 202 | impGen := GenerateFile(gen, impFile) |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 203 | impGen.Skip() |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 204 | b, err := impGen.Content() |
| 205 | if err != nil { |
| 206 | gen.Error(err) |
| 207 | return |
| 208 | } |
| 209 | fset := token.NewFileSet() |
| 210 | astFile, err := parser.ParseFile(fset, "", b, parser.ParseComments) |
| 211 | if err != nil { |
| 212 | gen.Error(err) |
| 213 | return |
| 214 | } |
Damien Neil | a7cbd06 | 2019-01-06 16:29:14 -0800 | [diff] [blame] | 215 | genForward := func(tok token.Token, name string, expr ast.Expr) { |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 216 | // Don't import unexported symbols. |
| 217 | r, _ := utf8.DecodeRuneInString(name) |
| 218 | if !unicode.IsUpper(r) { |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 219 | return |
| 220 | } |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 221 | // Don't import the FileDescriptor. |
| 222 | if name == impFile.GoDescriptorIdent.GoName { |
| 223 | return |
| 224 | } |
Damien Neil | a7cbd06 | 2019-01-06 16:29:14 -0800 | [diff] [blame] | 225 | // Don't import decls referencing a symbol defined in another package. |
| 226 | // i.e., don't import decls which are themselves public imports: |
| 227 | // |
| 228 | // type T = somepackage.T |
| 229 | if _, ok := expr.(*ast.SelectorExpr); ok { |
| 230 | return |
| 231 | } |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 232 | g.P(tok, " ", name, " = ", impFile.GoImportPath.Ident(name)) |
| 233 | } |
| 234 | g.P("// Symbols defined in public import of ", imp.Path()) |
| 235 | g.P() |
| 236 | for _, decl := range astFile.Decls { |
| 237 | switch decl := decl.(type) { |
| 238 | case *ast.GenDecl: |
| 239 | for _, spec := range decl.Specs { |
| 240 | switch spec := spec.(type) { |
| 241 | case *ast.TypeSpec: |
Damien Neil | a7cbd06 | 2019-01-06 16:29:14 -0800 | [diff] [blame] | 242 | genForward(decl.Tok, spec.Name.Name, spec.Type) |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 243 | case *ast.ValueSpec: |
Damien Neil | a7cbd06 | 2019-01-06 16:29:14 -0800 | [diff] [blame] | 244 | for i, name := range spec.Names { |
| 245 | var expr ast.Expr |
| 246 | if i < len(spec.Values) { |
| 247 | expr = spec.Values[i] |
| 248 | } |
| 249 | genForward(decl.Tok, name.Name, expr) |
Damien Neil | 7bf3ce2 | 2018-12-21 15:54:06 -0800 | [diff] [blame] | 250 | } |
| 251 | case *ast.ImportSpec: |
| 252 | default: |
| 253 | panic(fmt.Sprintf("can't generate forward for spec type %T", spec)) |
Damien Neil | 7e5c647 | 2018-11-29 08:57:07 -0800 | [diff] [blame] | 254 | } |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 255 | } |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 256 | } |
Damien Neil | 6b54131 | 2018-10-29 09:14:14 -0700 | [diff] [blame] | 257 | } |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 258 | g.P() |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 261 | func genEnum(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, enum *protogen.Enum) { |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 262 | // Enum type declaration. |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 263 | g.PrintLeadingComments(enum.Location) |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 264 | g.Annotate(enum.GoIdent.GoName, enum.Location) |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 265 | g.P("type ", enum.GoIdent, " int32", |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 266 | deprecationComment(enum.Desc.Options().(*descriptorpb.EnumOptions).GetDeprecated())) |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 267 | |
| 268 | // Enum value constants. |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 269 | g.P("const (") |
| 270 | for _, value := range enum.Values { |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 271 | g.PrintLeadingComments(value.Location) |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 272 | g.Annotate(value.GoIdent.GoName, value.Location) |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 273 | g.P(value.GoIdent, " ", enum.GoIdent, " = ", value.Desc.Number(), |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 274 | deprecationComment(value.Desc.Options().(*descriptorpb.EnumValueOptions).GetDeprecated())) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 275 | } |
| 276 | g.P(")") |
| 277 | g.P() |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 278 | |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 279 | // Enum value mapping (number -> name). |
Joe Tsai | ab61d41 | 2019-04-16 15:23:29 -0700 | [diff] [blame] | 280 | if generateEnumMapVars { |
| 281 | nameMap := enum.GoIdent.GoName + "_name" |
Joe Tsai | ab61d41 | 2019-04-16 15:23:29 -0700 | [diff] [blame] | 282 | g.P("var ", nameMap, " = map[int32]string{") |
| 283 | generated := make(map[protoreflect.EnumNumber]bool) |
| 284 | for _, value := range enum.Values { |
| 285 | duplicate := "" |
| 286 | if _, present := generated[value.Desc.Number()]; present { |
| 287 | duplicate = "// Duplicate value: " |
| 288 | } |
| 289 | g.P(duplicate, value.Desc.Number(), ": ", strconv.Quote(string(value.Desc.Name())), ",") |
| 290 | generated[value.Desc.Number()] = true |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 291 | } |
Joe Tsai | ab61d41 | 2019-04-16 15:23:29 -0700 | [diff] [blame] | 292 | g.P("}") |
| 293 | g.P() |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 294 | } |
Joe Tsai | 8e506a8 | 2019-03-16 00:05:34 -0700 | [diff] [blame] | 295 | |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 296 | // Enum value mapping (name -> number). |
Joe Tsai | ab61d41 | 2019-04-16 15:23:29 -0700 | [diff] [blame] | 297 | if generateEnumMapVars { |
| 298 | valueMap := enum.GoIdent.GoName + "_value" |
Joe Tsai | ab61d41 | 2019-04-16 15:23:29 -0700 | [diff] [blame] | 299 | g.P("var ", valueMap, " = map[string]int32{") |
| 300 | for _, value := range enum.Values { |
| 301 | g.P(strconv.Quote(string(value.Desc.Name())), ": ", value.Desc.Number(), ",") |
| 302 | } |
| 303 | g.P("}") |
| 304 | g.P() |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 305 | } |
Joe Tsai | 8e506a8 | 2019-03-16 00:05:34 -0700 | [diff] [blame] | 306 | |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 307 | // Enum method. |
Joe Tsai | dbab6c0 | 2019-05-14 15:06:03 -0700 | [diff] [blame] | 308 | // |
| 309 | // NOTE: A pointer value is needed to represent presence in proto2. |
| 310 | // Since a proto2 message can reference a proto3 enum, it is useful to |
| 311 | // always generate this method (even on proto3 enums) to support that case. |
| 312 | g.P("func (x ", enum.GoIdent, ") Enum() *", enum.GoIdent, " {") |
| 313 | g.P("p := new(", enum.GoIdent, ")") |
| 314 | g.P("*p = x") |
| 315 | g.P("return p") |
| 316 | g.P("}") |
| 317 | g.P() |
| 318 | |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 319 | // String method. |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 320 | g.P("func (x ", enum.GoIdent, ") String() string {") |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 321 | g.P("return ", protoimplPackage.Ident("X"), ".EnumStringOf(x.Descriptor(), ", protoreflectPackage.Ident("EnumNumber"), "(x))") |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 322 | g.P("}") |
| 323 | g.P() |
| 324 | |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 325 | genReflectEnum(gen, g, f, enum) |
| 326 | |
| 327 | // UnmarshalJSON method. |
Joe Tsai | 7390346 | 2018-12-14 12:22:41 -0800 | [diff] [blame] | 328 | if enum.Desc.Syntax() == protoreflect.Proto2 { |
Joe Tsai | 8e506a8 | 2019-03-16 00:05:34 -0700 | [diff] [blame] | 329 | g.P("// Deprecated: Do not use.") |
| 330 | g.P("func (x *", enum.GoIdent, ") UnmarshalJSON(b []byte) error {") |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 331 | g.P("num, err := ", protoimplPackage.Ident("X"), ".UnmarshalJSONEnum(x.Descriptor(), b)") |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 332 | g.P("if err != nil {") |
| 333 | g.P("return err") |
| 334 | g.P("}") |
Joe Tsai | 8e506a8 | 2019-03-16 00:05:34 -0700 | [diff] [blame] | 335 | g.P("*x = ", enum.GoIdent, "(num)") |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 336 | g.P("return nil") |
| 337 | g.P("}") |
| 338 | g.P() |
| 339 | } |
| 340 | |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 341 | // EnumDescriptor method. |
Joe Tsai | ab61d41 | 2019-04-16 15:23:29 -0700 | [diff] [blame] | 342 | if generateRawDescMethods { |
| 343 | var indexes []string |
| 344 | for i := 1; i < len(enum.Location.Path); i += 2 { |
| 345 | indexes = append(indexes, strconv.Itoa(int(enum.Location.Path[i]))) |
| 346 | } |
| 347 | g.P("// Deprecated: Use ", enum.GoIdent, ".Type instead.") |
| 348 | g.P("func (", enum.GoIdent, ") EnumDescriptor() ([]byte, []int) {") |
| 349 | g.P("return ", rawDescVarName(f), "GZIP(), []int{", strings.Join(indexes, ","), "}") |
| 350 | g.P("}") |
| 351 | g.P() |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 352 | } |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 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 | |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 357 | // enumLegacyName returns the name used by the v1 proto package. |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 358 | // |
| 359 | // Confusingly, this is <proto_package>.<go_ident>. This probably should have |
| 360 | // been the full name of the proto enum type instead, but changing it at this |
| 361 | // point would require thought. |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 362 | func enumLegacyName(enum *protogen.Enum) string { |
Joe Tsai | 67c1d9b | 2019-05-12 02:27:46 -0700 | [diff] [blame] | 363 | fdesc := enum.Desc.ParentFile() |
Damien Neil | daa4fad | 2018-10-08 14:08:27 -0700 | [diff] [blame] | 364 | if fdesc.Package() == "" { |
| 365 | return enum.GoIdent.GoName |
| 366 | } |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 367 | return string(fdesc.Package()) + "." + enum.GoIdent.GoName |
| 368 | } |
| 369 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 370 | 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] | 371 | if message.Desc.IsMapEntry() { |
| 372 | return |
| 373 | } |
| 374 | |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 375 | // Message type declaration. |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 376 | hasComment := g.PrintLeadingComments(message.Location) |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 377 | if message.Desc.Options().(*descriptorpb.MessageOptions).GetDeprecated() { |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 378 | if hasComment { |
| 379 | g.P("//") |
| 380 | } |
| 381 | g.P(deprecationComment(true)) |
| 382 | } |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 383 | g.Annotate(message.GoIdent.GoName, message.Location) |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 384 | g.P("type ", message.GoIdent, " struct {") |
Joe Tsai | c0e4bb2 | 2019-07-06 13:05:11 -0700 | [diff] [blame] | 385 | sf := f.allMessageFieldsByPtr[message] |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 386 | for _, field := range message.Fields { |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 387 | if field.Oneof != nil { |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 388 | // It would be a bit simpler to iterate over the oneofs below, |
| 389 | // but generating the field here keeps the contents of the Go |
| 390 | // struct in the same order as the contents of the source |
| 391 | // .proto file. |
Joe Tsai | c0e4bb2 | 2019-07-06 13:05:11 -0700 | [diff] [blame] | 392 | oneof := field.Oneof |
| 393 | if field != oneof.Fields[0] { |
| 394 | continue // already generated oneof field for first entry |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 395 | } |
Joe Tsai | c0e4bb2 | 2019-07-06 13:05:11 -0700 | [diff] [blame] | 396 | if g.PrintLeadingComments(oneof.Location) { |
| 397 | g.P("//") |
| 398 | } |
| 399 | g.P("// Types that are valid to be assigned to ", oneofFieldName(oneof), ":") |
| 400 | for _, field := range oneof.Fields { |
| 401 | g.PrintLeadingComments(field.Location) |
| 402 | g.P("//\t*", fieldOneofType(field)) |
| 403 | } |
| 404 | g.Annotate(message.GoIdent.GoName+"."+oneofFieldName(oneof), oneof.Location) |
| 405 | g.P(oneofFieldName(oneof), " ", oneofInterfaceName(oneof), " `protobuf_oneof:\"", oneof.Desc.Name(), "\"`") |
| 406 | sf.append(oneofFieldName(oneof)) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 407 | continue |
| 408 | } |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 409 | g.PrintLeadingComments(field.Location) |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 410 | goType, pointer := fieldGoType(g, field) |
| 411 | if pointer { |
| 412 | goType = "*" + goType |
| 413 | } |
Damien Neil | 0bd5a38 | 2018-09-13 15:07:10 -0700 | [diff] [blame] | 414 | tags := []string{ |
| 415 | fmt.Sprintf("protobuf:%q", fieldProtobufTag(field)), |
| 416 | fmt.Sprintf("json:%q", fieldJSONTag(field)), |
| 417 | } |
| 418 | if field.Desc.IsMap() { |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 419 | key := field.Message.Fields[0] |
| 420 | val := field.Message.Fields[1] |
Damien Neil | 0bd5a38 | 2018-09-13 15:07:10 -0700 | [diff] [blame] | 421 | tags = append(tags, |
| 422 | fmt.Sprintf("protobuf_key:%q", fieldProtobufTag(key)), |
| 423 | fmt.Sprintf("protobuf_val:%q", fieldProtobufTag(val)), |
| 424 | ) |
| 425 | } |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 426 | g.Annotate(message.GoIdent.GoName+"."+field.GoName, field.Location) |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 427 | g.P(field.GoName, " ", goType, " `", strings.Join(tags, " "), "`", |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 428 | deprecationComment(field.Desc.Options().(*descriptorpb.FieldOptions).GetDeprecated())) |
Joe Tsai | c0e4bb2 | 2019-07-06 13:05:11 -0700 | [diff] [blame] | 429 | sf.append(field.GoName) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 430 | } |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 431 | |
Joe Tsai | c0e4bb2 | 2019-07-06 13:05:11 -0700 | [diff] [blame] | 432 | if generateNoUnkeyedLiteralFields { |
| 433 | g.P("XXX_NoUnkeyedLiteral", " struct{} `json:\"-\"`") |
| 434 | sf.append("XXX_NoUnkeyedLiteral") |
| 435 | } |
| 436 | if generateExportedSizeCacheFields { |
| 437 | g.P("XXX_sizecache", " ", protoimplPackage.Ident("SizeCache"), " `json:\"-\"`") |
| 438 | sf.append("XXX_sizecache") |
| 439 | } else { |
| 440 | g.P("sizeCache", " ", protoimplPackage.Ident("SizeCache")) |
| 441 | sf.append("sizeCache") |
| 442 | } |
| 443 | if generateExportedUnknownFields { |
| 444 | g.P("XXX_unrecognized", " ", protoimplPackage.Ident("UnknownFields"), " `json:\"-\"`") |
| 445 | sf.append("XXX_unrecognized") |
| 446 | } else { |
| 447 | g.P("unknownFields", " ", protoimplPackage.Ident("UnknownFields")) |
| 448 | sf.append("unknownFields") |
| 449 | } |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 450 | if message.Desc.ExtensionRanges().Len() > 0 { |
Joe Tsai | c0e4bb2 | 2019-07-06 13:05:11 -0700 | [diff] [blame] | 451 | if generateExportedExtensionFields { |
Joe Tsai | 6ceeaab | 2019-07-08 12:31:21 -0700 | [diff] [blame^] | 452 | g.P("XXX_InternalExtensions", " ", protoimplPackage.Ident("ExtensionFields"), " `json:\"-\"`") |
Joe Tsai | c0e4bb2 | 2019-07-06 13:05:11 -0700 | [diff] [blame] | 453 | sf.append("XXX_InternalExtensions") |
| 454 | } else { |
Joe Tsai | 6ceeaab | 2019-07-08 12:31:21 -0700 | [diff] [blame^] | 455 | g.P("extensionFields", " ", protoimplPackage.Ident("ExtensionFields")) |
Joe Tsai | c0e4bb2 | 2019-07-06 13:05:11 -0700 | [diff] [blame] | 456 | sf.append("extensionFields") |
| 457 | } |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 458 | } |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 459 | g.P("}") |
| 460 | g.P() |
| 461 | |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 462 | // Reset method. |
| 463 | g.P("func (x *", message.GoIdent, ") Reset() {") |
| 464 | g.P("*x = ", message.GoIdent, "{}") |
| 465 | g.P("}") |
| 466 | g.P() |
| 467 | // String method. |
| 468 | g.P("func (x *", message.GoIdent, ") String() string {") |
| 469 | g.P("return ", protoimplPackage.Ident("X"), ".MessageStringOf(x)") |
| 470 | g.P("}") |
| 471 | g.P() |
| 472 | // ProtoMessage method. |
| 473 | g.P("func (*", message.GoIdent, ") ProtoMessage() {}") |
| 474 | g.P() |
| 475 | |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 476 | genReflectMessage(gen, g, f, message) |
| 477 | |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 478 | // Descriptor method. |
Joe Tsai | ab61d41 | 2019-04-16 15:23:29 -0700 | [diff] [blame] | 479 | if generateRawDescMethods { |
| 480 | var indexes []string |
| 481 | for i := 1; i < len(message.Location.Path); i += 2 { |
| 482 | indexes = append(indexes, strconv.Itoa(int(message.Location.Path[i]))) |
| 483 | } |
| 484 | g.P("// Deprecated: Use ", message.GoIdent, ".ProtoReflect.Type instead.") |
| 485 | g.P("func (*", message.GoIdent, ") Descriptor() ([]byte, []int) {") |
| 486 | g.P("return ", rawDescVarName(f), "GZIP(), []int{", strings.Join(indexes, ","), "}") |
| 487 | g.P("}") |
| 488 | g.P() |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 489 | } |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 490 | |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 491 | // ExtensionRangeArray method. |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 492 | if extranges := message.Desc.ExtensionRanges(); extranges.Len() > 0 { |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 493 | protoExtRange := protoifacePackage.Ident("ExtensionRangeV1") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 494 | extRangeVar := "extRange_" + message.GoIdent.GoName |
| 495 | g.P("var ", extRangeVar, " = []", protoExtRange, " {") |
| 496 | for i := 0; i < extranges.Len(); i++ { |
| 497 | r := extranges.Get(i) |
| 498 | g.P("{Start:", r[0], ", End:", r[1]-1 /* inclusive */, "},") |
| 499 | } |
| 500 | g.P("}") |
| 501 | g.P() |
Joe Tsai | 8e506a8 | 2019-03-16 00:05:34 -0700 | [diff] [blame] | 502 | g.P("// Deprecated: Use ", message.GoIdent, ".ProtoReflect.Type.ExtensionRanges instead.") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 503 | g.P("func (*", message.GoIdent, ") ExtensionRangeArray() []", protoExtRange, " {") |
| 504 | g.P("return ", extRangeVar) |
| 505 | g.P("}") |
| 506 | g.P() |
| 507 | } |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 508 | |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 509 | genWellKnownType(g, "*", message.GoIdent, message.Desc) |
| 510 | |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 511 | // Constants and vars holding the default values of fields. |
| 512 | for _, field := range message.Fields { |
Joe Tsai | 9667c48 | 2018-12-05 15:42:52 -0800 | [diff] [blame] | 513 | if !field.Desc.HasDefault() { |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 514 | continue |
| 515 | } |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 516 | defVarName := "Default_" + message.GoIdent.GoName + "_" + field.GoName |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 517 | def := field.Desc.Default() |
| 518 | switch field.Desc.Kind() { |
| 519 | case protoreflect.StringKind: |
| 520 | g.P("const ", defVarName, " string = ", strconv.Quote(def.String())) |
| 521 | case protoreflect.BytesKind: |
| 522 | g.P("var ", defVarName, " []byte = []byte(", strconv.Quote(string(def.Bytes())), ")") |
| 523 | case protoreflect.EnumKind: |
Damien Neil | a485fbd | 2018-10-26 13:28:37 -0700 | [diff] [blame] | 524 | evalueDesc := field.Desc.DefaultEnumValue() |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 525 | enum := field.Enum |
Damien Neil | a485fbd | 2018-10-26 13:28:37 -0700 | [diff] [blame] | 526 | evalue := enum.Values[evalueDesc.Index()] |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 527 | g.P("const ", defVarName, " ", field.Enum.GoIdent, " = ", evalue.GoIdent) |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 528 | case protoreflect.FloatKind, protoreflect.DoubleKind: |
| 529 | // Floating point numbers need extra handling for -Inf/Inf/NaN. |
| 530 | f := field.Desc.Default().Float() |
| 531 | goType := "float64" |
| 532 | if field.Desc.Kind() == protoreflect.FloatKind { |
| 533 | goType = "float32" |
| 534 | } |
| 535 | // funcCall returns a call to a function in the math package, |
| 536 | // possibly converting the result to float32. |
| 537 | funcCall := func(fn, param string) string { |
Joe Tsai | c1c17aa | 2018-11-16 11:14:14 -0800 | [diff] [blame] | 538 | s := g.QualifiedGoIdent(mathPackage.Ident(fn)) + param |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 539 | if goType != "float64" { |
| 540 | s = goType + "(" + s + ")" |
| 541 | } |
| 542 | return s |
| 543 | } |
| 544 | switch { |
| 545 | case math.IsInf(f, -1): |
| 546 | g.P("var ", defVarName, " ", goType, " = ", funcCall("Inf", "(-1)")) |
| 547 | case math.IsInf(f, 1): |
| 548 | g.P("var ", defVarName, " ", goType, " = ", funcCall("Inf", "(1)")) |
| 549 | case math.IsNaN(f): |
| 550 | g.P("var ", defVarName, " ", goType, " = ", funcCall("NaN", "()")) |
| 551 | default: |
Damien Neil | 982684b | 2018-09-28 14:12:41 -0700 | [diff] [blame] | 552 | g.P("const ", defVarName, " ", goType, " = ", field.Desc.Default().Interface()) |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 553 | } |
| 554 | default: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 555 | goType, _ := fieldGoType(g, field) |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 556 | g.P("const ", defVarName, " ", goType, " = ", def.Interface()) |
| 557 | } |
| 558 | } |
| 559 | g.P() |
| 560 | |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 561 | // Getter methods. |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 562 | for _, field := range message.Fields { |
Joe Tsai | 872b500 | 2019-04-08 14:03:15 -0700 | [diff] [blame] | 563 | if isFirstOneofField(field) { |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 564 | genOneofGetter(gen, g, f, message, field.Oneof) |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 565 | } |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 566 | goType, pointer := fieldGoType(g, field) |
| 567 | defaultValue := fieldDefaultValue(g, message, field) |
Joe Tsai | e1f8d50 | 2018-11-26 18:55:29 -0800 | [diff] [blame] | 568 | if field.Desc.Options().(*descriptorpb.FieldOptions).GetDeprecated() { |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 569 | g.P(deprecationComment(true)) |
| 570 | } |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 571 | g.Annotate(message.GoIdent.GoName+".Get"+field.GoName, field.Location) |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 572 | g.P("func (x *", message.GoIdent, ") Get", field.GoName, "() ", goType, " {") |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 573 | if field.Oneof != nil { |
| 574 | g.P("if x, ok := x.Get", field.Oneof.GoName, "().(*", fieldOneofType(field), "); ok {") |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 575 | g.P("return x.", field.GoName) |
| 576 | g.P("}") |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 577 | } else { |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 578 | if field.Desc.Syntax() == protoreflect.Proto3 || defaultValue == "nil" { |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 579 | g.P("if x != nil {") |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 580 | } else { |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 581 | g.P("if x != nil && x.", field.GoName, " != nil {") |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 582 | } |
| 583 | star := "" |
| 584 | if pointer { |
| 585 | star = "*" |
| 586 | } |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 587 | g.P("return ", star, " x.", field.GoName) |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 588 | g.P("}") |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 589 | } |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 590 | g.P("return ", defaultValue) |
| 591 | g.P("}") |
| 592 | g.P() |
| 593 | } |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 594 | |
Joe Tsai | 0991227 | 2019-07-08 10:38:11 -0700 | [diff] [blame] | 595 | // Oneof wrapper types. |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 596 | if len(message.Oneofs) > 0 { |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 597 | genOneofWrappers(gen, g, f, message) |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 598 | } |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 599 | } |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 600 | |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 601 | // fieldGoType returns the Go type used for a field. |
| 602 | // |
| 603 | // If it returns pointer=true, the struct field is a pointer to the type. |
| 604 | func fieldGoType(g *protogen.GeneratedFile, field *protogen.Field) (goType string, pointer bool) { |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 605 | pointer = true |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 606 | switch field.Desc.Kind() { |
| 607 | case protoreflect.BoolKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 608 | goType = "bool" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 609 | case protoreflect.EnumKind: |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 610 | goType = g.QualifiedGoIdent(field.Enum.GoIdent) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 611 | case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 612 | goType = "int32" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 613 | case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 614 | goType = "uint32" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 615 | case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 616 | goType = "int64" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 617 | case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 618 | goType = "uint64" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 619 | case protoreflect.FloatKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 620 | goType = "float32" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 621 | case protoreflect.DoubleKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 622 | goType = "float64" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 623 | case protoreflect.StringKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 624 | goType = "string" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 625 | case protoreflect.BytesKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 626 | goType = "[]byte" |
| 627 | pointer = false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 628 | case protoreflect.MessageKind, protoreflect.GroupKind: |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 629 | goType = "*" + g.QualifiedGoIdent(field.Message.GoIdent) |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 630 | pointer = false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 631 | } |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 632 | switch { |
| 633 | case field.Desc.IsList(): |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 634 | goType = "[]" + goType |
| 635 | pointer = false |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 636 | case field.Desc.IsMap(): |
| 637 | keyType, _ := fieldGoType(g, field.Message.Fields[0]) |
| 638 | valType, _ := fieldGoType(g, field.Message.Fields[1]) |
| 639 | return fmt.Sprintf("map[%v]%v", keyType, valType), false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 640 | } |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 641 | |
Damien Neil | 44000a1 | 2018-10-24 12:31:16 -0700 | [diff] [blame] | 642 | // Extension fields always have pointer type, even when defined in a proto3 file. |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 643 | if field.Desc.Syntax() == protoreflect.Proto3 && !field.Desc.IsExtension() { |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 644 | pointer = false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 645 | } |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 646 | return goType, pointer |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | func fieldProtobufTag(field *protogen.Field) string { |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame] | 650 | var enumName string |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 651 | if field.Desc.Kind() == protoreflect.EnumKind { |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 652 | enumName = enumLegacyName(field.Enum) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 653 | } |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame] | 654 | return tag.Marshal(field.Desc, enumName) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 657 | func fieldDefaultValue(g *protogen.GeneratedFile, message *protogen.Message, field *protogen.Field) string { |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 658 | if field.Desc.IsList() { |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 659 | return "nil" |
| 660 | } |
Joe Tsai | 9667c48 | 2018-12-05 15:42:52 -0800 | [diff] [blame] | 661 | if field.Desc.HasDefault() { |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 662 | defVarName := "Default_" + message.GoIdent.GoName + "_" + field.GoName |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 663 | if field.Desc.Kind() == protoreflect.BytesKind { |
| 664 | return "append([]byte(nil), " + defVarName + "...)" |
| 665 | } |
| 666 | return defVarName |
| 667 | } |
| 668 | switch field.Desc.Kind() { |
| 669 | case protoreflect.BoolKind: |
| 670 | return "false" |
| 671 | case protoreflect.StringKind: |
| 672 | return `""` |
| 673 | case protoreflect.MessageKind, protoreflect.GroupKind, protoreflect.BytesKind: |
| 674 | return "nil" |
| 675 | case protoreflect.EnumKind: |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 676 | return g.QualifiedGoIdent(field.Enum.Values[0].GoIdent) |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 677 | default: |
| 678 | return "0" |
| 679 | } |
| 680 | } |
| 681 | |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 682 | func fieldJSONTag(field *protogen.Field) string { |
| 683 | return string(field.Desc.Name()) + ",omitempty" |
| 684 | } |
| 685 | |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 686 | func genExtensions(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) { |
| 687 | if len(f.allExtensions) == 0 { |
| 688 | return |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 691 | g.P("var ", extDescsVarName(f), " = []", protoifacePackage.Ident("ExtensionDescV1"), "{") |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 692 | for _, extension := range f.allExtensions { |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 693 | g.P("{") |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 694 | g.P("ExtendedType: (*", extension.Extendee.GoIdent, ")(nil),") |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 695 | goType, pointer := fieldGoType(g, extension) |
| 696 | if pointer { |
| 697 | goType = "*" + goType |
| 698 | } |
| 699 | g.P("ExtensionType: (", goType, ")(nil),") |
| 700 | g.P("Field: ", extension.Desc.Number(), ",") |
Joe Tsai | 6ceeaab | 2019-07-08 12:31:21 -0700 | [diff] [blame^] | 701 | g.P("Name: ", strconv.Quote(string(extension.Desc.FullName())), ",") |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 702 | g.P("Tag: ", strconv.Quote(fieldProtobufTag(extension)), ",") |
| 703 | g.P("Filename: ", strconv.Quote(f.Desc.Path()), ",") |
| 704 | g.P("},") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 705 | } |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 706 | g.P("}") |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 707 | |
| 708 | g.P("var (") |
| 709 | for i, extension := range f.allExtensions { |
| 710 | ed := extension.Desc |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 711 | targetName := string(ed.ContainingMessage().FullName()) |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 712 | typeName := ed.Kind().String() |
| 713 | switch ed.Kind() { |
| 714 | case protoreflect.EnumKind: |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 715 | typeName = string(ed.Enum().FullName()) |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 716 | case protoreflect.MessageKind, protoreflect.GroupKind: |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 717 | typeName = string(ed.Message().FullName()) |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 718 | } |
| 719 | fieldName := string(ed.Name()) |
| 720 | g.P("// extend ", targetName, " { ", ed.Cardinality().String(), " ", typeName, " ", fieldName, " = ", ed.Number(), "; }") |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame] | 721 | g.P(extensionVar(f.File, extension), " = &", extDescsVarName(f), "[", i, "]") |
Joe Tsai | afb455e | 2019-03-14 16:08:22 -0700 | [diff] [blame] | 722 | g.P() |
| 723 | } |
| 724 | g.P(")") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | // extensionVar returns the var holding the ExtensionDesc for an extension. |
Damien Neil | 6b54131 | 2018-10-29 09:14:14 -0700 | [diff] [blame] | 728 | func extensionVar(f *protogen.File, extension *protogen.Extension) protogen.GoIdent { |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 729 | name := "E_" |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 730 | if extension.Parent != nil { |
| 731 | name += extension.Parent.GoIdent.GoName + "_" |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 732 | } |
| 733 | name += extension.GoName |
Joe Tsai | c1c17aa | 2018-11-16 11:14:14 -0800 | [diff] [blame] | 734 | return f.GoImportPath.Ident(name) |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 737 | // deprecationComment returns a standard deprecation comment if deprecated is true. |
| 738 | func deprecationComment(deprecated bool) string { |
| 739 | if !deprecated { |
| 740 | return "" |
| 741 | } |
| 742 | return "// Deprecated: Do not use." |
| 743 | } |
| 744 | |
Damien Neil | 5c5b531 | 2019-05-14 12:44:37 -0700 | [diff] [blame] | 745 | // TODO: Remove this. This was added to aid protojson, but protojson does this work |
Joe Tsai | 61968ce | 2019-04-01 12:59:24 -0700 | [diff] [blame] | 746 | // through the use of protobuf reflection now. |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 747 | 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] | 748 | if wellKnownTypes[desc.FullName()] { |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 749 | g.P("func (", ptr, ident, `) XXX_WellKnownType() string { return "`, desc.Name(), `" }`) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 750 | g.P() |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | // Names of messages and enums for which we will generate XXX_WellKnownType methods. |
| 755 | var wellKnownTypes = map[protoreflect.FullName]bool{ |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 756 | "google.protobuf.Any": true, |
| 757 | "google.protobuf.Duration": true, |
| 758 | "google.protobuf.Empty": true, |
| 759 | "google.protobuf.Struct": true, |
| 760 | "google.protobuf.Timestamp": true, |
| 761 | |
| 762 | "google.protobuf.BoolValue": true, |
| 763 | "google.protobuf.BytesValue": true, |
| 764 | "google.protobuf.DoubleValue": true, |
| 765 | "google.protobuf.FloatValue": true, |
| 766 | "google.protobuf.Int32Value": true, |
| 767 | "google.protobuf.Int64Value": true, |
| 768 | "google.protobuf.ListValue": true, |
| 769 | "google.protobuf.NullValue": true, |
| 770 | "google.protobuf.StringValue": true, |
| 771 | "google.protobuf.UInt32Value": true, |
| 772 | "google.protobuf.UInt64Value": true, |
| 773 | "google.protobuf.Value": true, |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 774 | } |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 775 | |
Joe Tsai | 872b500 | 2019-04-08 14:03:15 -0700 | [diff] [blame] | 776 | // genOneofGetter generate a Get method for a oneof. |
| 777 | func genOneofGetter(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message, oneof *protogen.Oneof) { |
| 778 | g.Annotate(message.GoIdent.GoName+".Get"+oneof.GoName, oneof.Location) |
| 779 | g.P("func (m *", message.GoIdent.GoName, ") Get", oneof.GoName, "() ", oneofInterfaceName(oneof), " {") |
| 780 | g.P("if m != nil {") |
| 781 | g.P("return m.", oneofFieldName(oneof)) |
| 782 | g.P("}") |
| 783 | g.P("return nil") |
| 784 | g.P("}") |
| 785 | g.P() |
| 786 | } |
| 787 | |
Joe Tsai | 0991227 | 2019-07-08 10:38:11 -0700 | [diff] [blame] | 788 | // genOneofWrappers generates the oneof wrapper types and associates the types |
| 789 | // with the parent message type. |
Joe Tsai | 872b500 | 2019-04-08 14:03:15 -0700 | [diff] [blame] | 790 | func genOneofWrappers(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message) { |
Joe Tsai | 0991227 | 2019-07-08 10:38:11 -0700 | [diff] [blame] | 791 | idx := f.allMessagesByPtr[message] |
| 792 | typesVar := messageTypesVarName(f) |
| 793 | |
| 794 | // Associate the wrapper types through a XXX_OneofWrappers method. |
| 795 | if generateOneofWrapperMethods { |
| 796 | g.P("// XXX_OneofWrappers is for the internal use of the proto package.") |
| 797 | g.P("func (*", message.GoIdent.GoName, ") XXX_OneofWrappers() []interface{} {") |
| 798 | g.P("return ", typesVar, "[", idx, "].OneofWrappers") |
| 799 | g.P("}") |
| 800 | g.P() |
Joe Tsai | 872b500 | 2019-04-08 14:03:15 -0700 | [diff] [blame] | 801 | } |
Joe Tsai | 0991227 | 2019-07-08 10:38:11 -0700 | [diff] [blame] | 802 | |
| 803 | // Generate the oneof wrapper types. |
| 804 | for _, oneof := range message.Oneofs { |
| 805 | genOneofTypes(gen, g, f, message, oneof) |
| 806 | } |
Joe Tsai | 872b500 | 2019-04-08 14:03:15 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 809 | // genOneofTypes generates the interface type used for a oneof field, |
| 810 | // and the wrapper types that satisfy that interface. |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 811 | func genOneofTypes(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, message *protogen.Message, oneof *protogen.Oneof) { |
| 812 | ifName := oneofInterfaceName(oneof) |
| 813 | g.P("type ", ifName, " interface {") |
| 814 | g.P(ifName, "()") |
| 815 | g.P("}") |
| 816 | g.P() |
| 817 | for _, field := range oneof.Fields { |
| 818 | name := fieldOneofType(field) |
| 819 | g.Annotate(name.GoName, field.Location) |
| 820 | g.Annotate(name.GoName+"."+field.GoName, field.Location) |
| 821 | g.P("type ", name, " struct {") |
| 822 | goType, _ := fieldGoType(g, field) |
| 823 | tags := []string{ |
| 824 | fmt.Sprintf("protobuf:%q", fieldProtobufTag(field)), |
| 825 | } |
| 826 | g.P(field.GoName, " ", goType, " `", strings.Join(tags, " "), "`") |
| 827 | g.P("}") |
| 828 | g.P() |
| 829 | } |
| 830 | for _, field := range oneof.Fields { |
| 831 | g.P("func (*", fieldOneofType(field), ") ", ifName, "() {}") |
| 832 | g.P() |
| 833 | } |
Joe Tsai | 872b500 | 2019-04-08 14:03:15 -0700 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | // isFirstOneofField reports whether this is the first field in a oneof. |
| 837 | func isFirstOneofField(field *protogen.Field) bool { |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 838 | return field.Oneof != nil && field.Oneof.Fields[0] == field |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | // oneofFieldName returns the name of the struct field holding the oneof value. |
| 842 | // |
| 843 | // This function is trivial, but pulling out the name like this makes it easier |
| 844 | // to experiment with alternative oneof implementations. |
| 845 | func oneofFieldName(oneof *protogen.Oneof) string { |
| 846 | return oneof.GoName |
| 847 | } |
| 848 | |
| 849 | // oneofInterfaceName returns the name of the interface type implemented by |
| 850 | // the oneof field value types. |
| 851 | func oneofInterfaceName(oneof *protogen.Oneof) string { |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 852 | return fmt.Sprintf("is%s_%s", oneof.Parent.GoIdent.GoName, oneof.GoName) |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 853 | } |
| 854 | |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 855 | // fieldOneofType returns the wrapper type used to represent a field in a oneof. |
| 856 | func fieldOneofType(field *protogen.Field) protogen.GoIdent { |
| 857 | ident := protogen.GoIdent{ |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 858 | GoImportPath: field.Parent.GoIdent.GoImportPath, |
| 859 | GoName: field.Parent.GoIdent.GoName + "_" + field.GoName, |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 860 | } |
| 861 | // Check for collisions with nested messages or enums. |
| 862 | // |
| 863 | // This conflict resolution is incomplete: Among other things, it |
| 864 | // does not consider collisions with other oneof field types. |
| 865 | // |
| 866 | // TODO: Consider dropping this entirely. Detecting conflicts and |
| 867 | // producing an error is almost certainly better than permuting |
| 868 | // field and type names in mostly unpredictable ways. |
| 869 | Loop: |
| 870 | for { |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 871 | for _, message := range field.Parent.Messages { |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 872 | if message.GoIdent == ident { |
| 873 | ident.GoName += "_" |
| 874 | continue Loop |
| 875 | } |
| 876 | } |
Joe Tsai | d24bc72 | 2019-04-15 23:39:09 -0700 | [diff] [blame] | 877 | for _, enum := range field.Parent.Enums { |
Joe Tsai | d7e97bc | 2018-11-26 12:57:27 -0800 | [diff] [blame] | 878 | if enum.GoIdent == ident { |
| 879 | ident.GoName += "_" |
| 880 | continue Loop |
| 881 | } |
| 882 | } |
| 883 | return ident |
| 884 | } |
| 885 | } |