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