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