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