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