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