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