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 | "bytes" |
| 10 | "compress/gzip" |
| 11 | "crypto/sha256" |
| 12 | "encoding/hex" |
| 13 | "fmt" |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 14 | "math" |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 15 | "sort" |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 16 | "strconv" |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 17 | "strings" |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 18 | |
| 19 | "github.com/golang/protobuf/proto" |
| 20 | descpb "github.com/golang/protobuf/protoc-gen-go/descriptor" |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame] | 21 | "github.com/golang/protobuf/v2/internal/encoding/tag" |
Joe Tsai | 01ab296 | 2018-09-21 17:44:00 -0700 | [diff] [blame] | 22 | "github.com/golang/protobuf/v2/protogen" |
| 23 | "github.com/golang/protobuf/v2/reflect/protoreflect" |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
Damien Neil | d412792 | 2018-09-12 11:13:49 -0700 | [diff] [blame] | 26 | // generatedCodeVersion indicates a version of the generated code. |
| 27 | // It is incremented whenever an incompatibility between the generated code and |
| 28 | // proto package is introduced; the generated code references |
| 29 | // a constant, proto.ProtoPackageIsVersionN (where N is generatedCodeVersion). |
| 30 | const generatedCodeVersion = 2 |
| 31 | |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 32 | const protoPackage = "github.com/golang/protobuf/proto" |
| 33 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 34 | type fileInfo struct { |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 35 | *protogen.File |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 36 | descriptorVar string // var containing the gzipped FileDescriptorProto |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 37 | allEnums []*protogen.Enum |
| 38 | allMessages []*protogen.Message |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 39 | allExtensions []*protogen.Extension |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Damien Neil | 9c420a6 | 2018-09-27 15:26:33 -0700 | [diff] [blame] | 42 | // GenerateFile generates the contents of a .pb.go file. |
| 43 | func GenerateFile(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile) { |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 44 | f := &fileInfo{ |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 45 | File: file, |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 48 | // The different order for enums and extensions is to match the output |
| 49 | // of the previous implementation. |
| 50 | // |
| 51 | // TODO: Eventually make this consistent. |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 52 | f.allEnums = append(f.allEnums, f.File.Enums...) |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 53 | walkMessages(f.Messages, func(message *protogen.Message) { |
| 54 | f.allMessages = append(f.allMessages, message) |
| 55 | f.allEnums = append(f.allEnums, message.Enums...) |
| 56 | f.allExtensions = append(f.allExtensions, message.Extensions...) |
| 57 | }) |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 58 | f.allExtensions = append(f.allExtensions, f.File.Extensions...) |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 59 | |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 60 | // Determine the name of the var holding the file descriptor: |
| 61 | // |
| 62 | // fileDescriptor_<hash of filename> |
| 63 | filenameHash := sha256.Sum256([]byte(f.Desc.Path())) |
| 64 | f.descriptorVar = fmt.Sprintf("fileDescriptor_%s", hex.EncodeToString(filenameHash[:8])) |
| 65 | |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 66 | g.P("// Code generated by protoc-gen-go. DO NOT EDIT.") |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 67 | if f.Proto.GetOptions().GetDeprecated() { |
| 68 | g.P("// ", f.Desc.Path(), " is a deprecated file.") |
| 69 | } else { |
| 70 | g.P("// source: ", f.Desc.Path()) |
| 71 | } |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 72 | g.P() |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 73 | const filePackageField = 2 // FileDescriptorProto.package |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 74 | g.PrintLeadingComments(protogen.Location{ |
| 75 | SourceFile: f.Proto.GetName(), |
| 76 | Path: []int32{filePackageField}, |
| 77 | }) |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 78 | g.P() |
Damien Neil | 082ce92 | 2018-09-06 10:23:53 -0700 | [diff] [blame] | 79 | g.P("package ", f.GoPackageName) |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 80 | g.P() |
Damien Neil | 1ec3315 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 81 | |
| 82 | // These references are not necessary, since we automatically add |
| 83 | // all necessary imports before formatting the generated file. |
| 84 | // |
| 85 | // This section exists to generate output more consistent with |
| 86 | // the previous version of protoc-gen-go, to make it easier to |
| 87 | // detect unintended variations. |
| 88 | // |
| 89 | // TODO: Eventually remove this. |
| 90 | g.P("// Reference imports to suppress errors if they are not otherwise used.") |
| 91 | g.P("var _ = ", protogen.GoIdent{GoImportPath: protoPackage, GoName: "Marshal"}) |
| 92 | g.P("var _ = ", protogen.GoIdent{GoImportPath: "fmt", GoName: "Errorf"}) |
| 93 | g.P("var _ = ", protogen.GoIdent{GoImportPath: "math", GoName: "Inf"}) |
| 94 | g.P() |
| 95 | |
Damien Neil | d412792 | 2018-09-12 11:13:49 -0700 | [diff] [blame] | 96 | g.P("// This is a compile-time assertion to ensure that this generated file") |
| 97 | g.P("// is compatible with the proto package it is being compiled against.") |
| 98 | g.P("// A compilation error at this line likely means your copy of the") |
| 99 | g.P("// proto package needs to be updated.") |
| 100 | g.P("const _ = ", protogen.GoIdent{ |
| 101 | GoImportPath: protoPackage, |
| 102 | GoName: fmt.Sprintf("ProtoPackageIsVersion%d", generatedCodeVersion), |
| 103 | }, "// please upgrade the proto package") |
| 104 | g.P() |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 105 | |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 106 | for i, imps := 0, f.Desc.Imports(); i < imps.Len(); i++ { |
| 107 | genImport(gen, g, f, imps.Get(i)) |
| 108 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 109 | for _, enum := range f.allEnums { |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 110 | genEnum(gen, g, f, enum) |
| 111 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 112 | for _, message := range f.allMessages { |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 113 | genMessage(gen, g, f, message) |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 114 | } |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 115 | for _, extension := range f.Extensions { |
| 116 | genExtension(gen, g, f, extension) |
| 117 | } |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 118 | |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 119 | genInitFunction(gen, g, f) |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 120 | genFileDescriptor(gen, g, f) |
| 121 | } |
| 122 | |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 123 | // walkMessages calls f on each message and all of its descendants. |
| 124 | func walkMessages(messages []*protogen.Message, f func(*protogen.Message)) { |
| 125 | for _, m := range messages { |
| 126 | f(m) |
| 127 | walkMessages(m.Messages, f) |
| 128 | } |
| 129 | } |
| 130 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 131 | 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] | 132 | impFile, ok := gen.FileByName(imp.Path()) |
| 133 | if !ok { |
| 134 | return |
| 135 | } |
| 136 | if impFile.GoImportPath == f.GoImportPath { |
Damien Neil | 2e0c3da | 2018-09-19 12:51:36 -0700 | [diff] [blame] | 137 | // Don't generate imports or aliases for types in the same Go package. |
| 138 | return |
| 139 | } |
Damien Neil | 40a0805 | 2018-10-29 09:07:41 -0700 | [diff] [blame] | 140 | // Generate imports for all non-weak dependencies, even if they are not |
Damien Neil | 2e0c3da | 2018-09-19 12:51:36 -0700 | [diff] [blame] | 141 | // referenced, because other code and tools depend on having the |
| 142 | // full transitive closure of protocol buffer types in the binary. |
Damien Neil | 40a0805 | 2018-10-29 09:07:41 -0700 | [diff] [blame] | 143 | if !imp.IsWeak { |
| 144 | g.Import(impFile.GoImportPath) |
| 145 | } |
Damien Neil | 2e0c3da | 2018-09-19 12:51:36 -0700 | [diff] [blame] | 146 | if !imp.IsPublic { |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 147 | return |
| 148 | } |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 149 | // TODO: An alternate approach to generating public imports might be |
| 150 | // to generate the imported file contents, parse it, and extract all |
| 151 | // exported identifiers from the AST to build a list of forwarding |
| 152 | // declarations. |
| 153 | // |
| 154 | // TODO: Consider whether this should generate recursive aliases. e.g., |
| 155 | // if a.proto publicly imports b.proto publicly imports c.proto, should |
| 156 | // a.pb.go contain aliases for symbols defined in c.proto? |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 157 | var enums []*protogen.Enum |
| 158 | enums = append(enums, impFile.Enums...) |
| 159 | walkMessages(impFile.Messages, func(message *protogen.Message) { |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 160 | if message.Desc.IsMapEntry() { |
| 161 | return |
| 162 | } |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 163 | enums = append(enums, message.Enums...) |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 164 | for _, field := range message.Fields { |
| 165 | if !fieldHasDefault(field) { |
| 166 | continue |
| 167 | } |
| 168 | defVar := protogen.GoIdent{ |
| 169 | GoImportPath: message.GoIdent.GoImportPath, |
| 170 | GoName: "Default_" + message.GoIdent.GoName + "_" + field.GoName, |
| 171 | } |
| 172 | decl := "const" |
| 173 | if field.Desc.Kind() == protoreflect.BytesKind { |
| 174 | decl = "var" |
| 175 | } |
| 176 | g.P(decl, " ", defVar.GoName, " = ", defVar) |
| 177 | } |
Damien Neil | 73ac885 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 178 | g.P("// ", message.GoIdent.GoName, " from public import ", imp.Path()) |
| 179 | g.P("type ", message.GoIdent.GoName, " = ", message.GoIdent) |
| 180 | for _, oneof := range message.Oneofs { |
| 181 | for _, field := range oneof.Fields { |
| 182 | typ := fieldOneofType(field) |
| 183 | g.P("type ", typ.GoName, " = ", typ) |
| 184 | } |
| 185 | } |
| 186 | g.P() |
| 187 | }) |
| 188 | for _, enum := range enums { |
| 189 | g.P("// ", enum.GoIdent.GoName, " from public import ", imp.Path()) |
| 190 | g.P("type ", enum.GoIdent.GoName, " = ", enum.GoIdent) |
| 191 | g.P("var ", enum.GoIdent.GoName, "_name = ", enum.GoIdent, "_name") |
| 192 | g.P("var ", enum.GoIdent.GoName, "_value = ", enum.GoIdent, "_value") |
| 193 | g.P() |
| 194 | for _, value := range enum.Values { |
| 195 | g.P("const ", value.GoIdent.GoName, " = ", enum.GoIdent.GoName, "(", value.GoIdent, ")") |
| 196 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 197 | } |
Damien Neil | 6b54131 | 2018-10-29 09:14:14 -0700 | [diff] [blame] | 198 | for _, ext := range impFile.Extensions { |
| 199 | ident := extensionVar(impFile, ext) |
| 200 | g.P("var ", ident.GoName, " = ", ident) |
| 201 | g.P() |
| 202 | } |
Damien Neil | 2193e8d | 2018-10-09 12:49:13 -0700 | [diff] [blame] | 203 | g.P() |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 206 | func genFileDescriptor(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) { |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 207 | // Trim the source_code_info from the descriptor. |
| 208 | // Marshal and gzip it. |
| 209 | descProto := proto.Clone(f.Proto).(*descpb.FileDescriptorProto) |
| 210 | descProto.SourceCodeInfo = nil |
| 211 | b, err := proto.Marshal(descProto) |
| 212 | if err != nil { |
| 213 | gen.Error(err) |
| 214 | return |
| 215 | } |
| 216 | var buf bytes.Buffer |
| 217 | w, _ := gzip.NewWriterLevel(&buf, gzip.BestCompression) |
| 218 | w.Write(b) |
| 219 | w.Close() |
| 220 | b = buf.Bytes() |
| 221 | |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 222 | g.P("func init() { proto.RegisterFile(", strconv.Quote(f.Desc.Path()), ", ", f.descriptorVar, ") }") |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 223 | g.P() |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 224 | g.P("var ", f.descriptorVar, " = []byte{") |
Damien Neil | 7779e05 | 2018-09-07 14:14:06 -0700 | [diff] [blame] | 225 | g.P("// ", len(b), " bytes of a gzipped FileDescriptorProto") |
| 226 | for len(b) > 0 { |
| 227 | n := 16 |
| 228 | if n > len(b) { |
| 229 | n = len(b) |
| 230 | } |
| 231 | |
| 232 | s := "" |
| 233 | for _, c := range b[:n] { |
| 234 | s += fmt.Sprintf("0x%02x,", c) |
| 235 | } |
| 236 | g.P(s) |
| 237 | |
| 238 | b = b[n:] |
| 239 | } |
| 240 | g.P("}") |
| 241 | g.P() |
Damien Neil | 220c202 | 2018-08-15 11:24:18 -0700 | [diff] [blame] | 242 | } |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 243 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 244 | 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] | 245 | g.PrintLeadingComments(enum.Location) |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 246 | g.Annotate(enum.GoIdent.GoName, enum.Location) |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 247 | g.P("type ", enum.GoIdent, " int32", |
Damien Neil | 204f1c0 | 2018-10-23 15:03:38 -0700 | [diff] [blame] | 248 | deprecationComment(enum.Desc.Options().(*descpb.EnumOptions).GetDeprecated())) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 249 | g.P("const (") |
| 250 | for _, value := range enum.Values { |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 251 | g.PrintLeadingComments(value.Location) |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 252 | g.Annotate(value.GoIdent.GoName, value.Location) |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 253 | g.P(value.GoIdent, " ", enum.GoIdent, " = ", value.Desc.Number(), |
Damien Neil | 204f1c0 | 2018-10-23 15:03:38 -0700 | [diff] [blame] | 254 | deprecationComment(value.Desc.Options().(*descpb.EnumValueOptions).GetDeprecated())) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 255 | } |
| 256 | g.P(")") |
| 257 | g.P() |
| 258 | nameMap := enum.GoIdent.GoName + "_name" |
| 259 | g.P("var ", nameMap, " = map[int32]string{") |
| 260 | generated := make(map[protoreflect.EnumNumber]bool) |
| 261 | for _, value := range enum.Values { |
| 262 | duplicate := "" |
| 263 | if _, present := generated[value.Desc.Number()]; present { |
| 264 | duplicate = "// Duplicate value: " |
| 265 | } |
| 266 | g.P(duplicate, value.Desc.Number(), ": ", strconv.Quote(string(value.Desc.Name())), ",") |
| 267 | generated[value.Desc.Number()] = true |
| 268 | } |
| 269 | g.P("}") |
| 270 | g.P() |
| 271 | valueMap := enum.GoIdent.GoName + "_value" |
| 272 | g.P("var ", valueMap, " = map[string]int32{") |
| 273 | for _, value := range enum.Values { |
| 274 | g.P(strconv.Quote(string(value.Desc.Name())), ": ", value.Desc.Number(), ",") |
| 275 | } |
| 276 | g.P("}") |
| 277 | g.P() |
| 278 | if enum.Desc.Syntax() != protoreflect.Proto3 { |
| 279 | g.P("func (x ", enum.GoIdent, ") Enum() *", enum.GoIdent, " {") |
| 280 | g.P("p := new(", enum.GoIdent, ")") |
| 281 | g.P("*p = x") |
| 282 | g.P("return p") |
| 283 | g.P("}") |
| 284 | g.P() |
| 285 | } |
| 286 | g.P("func (x ", enum.GoIdent, ") String() string {") |
| 287 | g.P("return ", protogen.GoIdent{GoImportPath: protoPackage, GoName: "EnumName"}, "(", enum.GoIdent, "_name, int32(x))") |
| 288 | g.P("}") |
| 289 | g.P() |
| 290 | |
| 291 | if enum.Desc.Syntax() != protoreflect.Proto3 { |
| 292 | g.P("func (x *", enum.GoIdent, ") UnmarshalJSON(data []byte) error {") |
| 293 | g.P("value, err := ", protogen.GoIdent{GoImportPath: protoPackage, GoName: "UnmarshalJSONEnum"}, "(", enum.GoIdent, `_value, data, "`, enum.GoIdent, `")`) |
| 294 | g.P("if err != nil {") |
| 295 | g.P("return err") |
| 296 | g.P("}") |
| 297 | g.P("*x = ", enum.GoIdent, "(value)") |
| 298 | g.P("return nil") |
| 299 | g.P("}") |
| 300 | g.P() |
| 301 | } |
| 302 | |
| 303 | var indexes []string |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 304 | for i := 1; i < len(enum.Location.Path); i += 2 { |
| 305 | indexes = append(indexes, strconv.Itoa(int(enum.Location.Path[i]))) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 306 | } |
| 307 | g.P("func (", enum.GoIdent, ") EnumDescriptor() ([]byte, []int) {") |
| 308 | g.P("return ", f.descriptorVar, ", []int{", strings.Join(indexes, ","), "}") |
| 309 | g.P("}") |
| 310 | g.P() |
| 311 | |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 312 | genWellKnownType(g, "", enum.GoIdent, enum.Desc) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 315 | // enumRegistryName returns the name used to register an enum with the proto |
| 316 | // package registry. |
| 317 | // |
| 318 | // Confusingly, this is <proto_package>.<go_ident>. This probably should have |
| 319 | // been the full name of the proto enum type instead, but changing it at this |
| 320 | // point would require thought. |
| 321 | func enumRegistryName(enum *protogen.Enum) string { |
| 322 | // Find the FileDescriptor for this enum. |
| 323 | var desc protoreflect.Descriptor = enum.Desc |
| 324 | for { |
| 325 | p, ok := desc.Parent() |
| 326 | if !ok { |
| 327 | break |
| 328 | } |
| 329 | desc = p |
| 330 | } |
| 331 | fdesc := desc.(protoreflect.FileDescriptor) |
Damien Neil | daa4fad | 2018-10-08 14:08:27 -0700 | [diff] [blame] | 332 | if fdesc.Package() == "" { |
| 333 | return enum.GoIdent.GoName |
| 334 | } |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 335 | return string(fdesc.Package()) + "." + enum.GoIdent.GoName |
| 336 | } |
| 337 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 338 | 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] | 339 | if message.Desc.IsMapEntry() { |
| 340 | return |
| 341 | } |
| 342 | |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 343 | hasComment := g.PrintLeadingComments(message.Location) |
Damien Neil | 204f1c0 | 2018-10-23 15:03:38 -0700 | [diff] [blame] | 344 | if message.Desc.Options().(*descpb.MessageOptions).GetDeprecated() { |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 345 | if hasComment { |
| 346 | g.P("//") |
| 347 | } |
| 348 | g.P(deprecationComment(true)) |
| 349 | } |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 350 | g.Annotate(message.GoIdent.GoName, message.Location) |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 351 | g.P("type ", message.GoIdent, " struct {") |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 352 | for _, field := range message.Fields { |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 353 | if field.OneofType != nil { |
| 354 | // It would be a bit simpler to iterate over the oneofs below, |
| 355 | // but generating the field here keeps the contents of the Go |
| 356 | // struct in the same order as the contents of the source |
| 357 | // .proto file. |
| 358 | if field == field.OneofType.Fields[0] { |
| 359 | genOneofField(gen, g, f, message, field.OneofType) |
| 360 | } |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 361 | continue |
| 362 | } |
Damien Neil | ba1159f | 2018-10-17 12:53:18 -0700 | [diff] [blame] | 363 | g.PrintLeadingComments(field.Location) |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 364 | goType, pointer := fieldGoType(g, field) |
| 365 | if pointer { |
| 366 | goType = "*" + goType |
| 367 | } |
Damien Neil | 0bd5a38 | 2018-09-13 15:07:10 -0700 | [diff] [blame] | 368 | tags := []string{ |
| 369 | fmt.Sprintf("protobuf:%q", fieldProtobufTag(field)), |
| 370 | fmt.Sprintf("json:%q", fieldJSONTag(field)), |
| 371 | } |
| 372 | if field.Desc.IsMap() { |
| 373 | key := field.MessageType.Fields[0] |
| 374 | val := field.MessageType.Fields[1] |
| 375 | tags = append(tags, |
| 376 | fmt.Sprintf("protobuf_key:%q", fieldProtobufTag(key)), |
| 377 | fmt.Sprintf("protobuf_val:%q", fieldProtobufTag(val)), |
| 378 | ) |
| 379 | } |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 380 | g.Annotate(message.GoIdent.GoName+"."+field.GoName, field.Location) |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 381 | g.P(field.GoName, " ", goType, " `", strings.Join(tags, " "), "`", |
Damien Neil | 204f1c0 | 2018-10-23 15:03:38 -0700 | [diff] [blame] | 382 | deprecationComment(field.Desc.Options().(*descpb.FieldOptions).GetDeprecated())) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 383 | } |
| 384 | g.P("XXX_NoUnkeyedLiteral struct{} `json:\"-\"`") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 385 | |
| 386 | if message.Desc.ExtensionRanges().Len() > 0 { |
| 387 | var tags []string |
Damien Neil | 204f1c0 | 2018-10-23 15:03:38 -0700 | [diff] [blame] | 388 | if message.Desc.Options().(*descpb.MessageOptions).GetMessageSetWireFormat() { |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 389 | tags = append(tags, `protobuf_messageset:"1"`) |
| 390 | } |
| 391 | tags = append(tags, `json:"-"`) |
| 392 | g.P(protogen.GoIdent{ |
| 393 | GoImportPath: protoPackage, |
| 394 | GoName: "XXX_InternalExtensions", |
| 395 | }, " `", strings.Join(tags, " "), "`") |
| 396 | } |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 397 | // TODO XXX_InternalExtensions |
| 398 | g.P("XXX_unrecognized []byte `json:\"-\"`") |
| 399 | g.P("XXX_sizecache int32 `json:\"-\"`") |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 400 | g.P("}") |
| 401 | g.P() |
| 402 | |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 403 | // Reset |
| 404 | g.P("func (m *", message.GoIdent, ") Reset() { *m = ", message.GoIdent, "{} }") |
| 405 | // String |
| 406 | g.P("func (m *", message.GoIdent, ") String() string { return ", protogen.GoIdent{ |
| 407 | GoImportPath: protoPackage, |
| 408 | GoName: "CompactTextString", |
| 409 | }, "(m) }") |
| 410 | // ProtoMessage |
| 411 | g.P("func (*", message.GoIdent, ") ProtoMessage() {}") |
| 412 | // Descriptor |
| 413 | var indexes []string |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 414 | for i := 1; i < len(message.Location.Path); i += 2 { |
| 415 | indexes = append(indexes, strconv.Itoa(int(message.Location.Path[i]))) |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 416 | } |
| 417 | g.P("func (*", message.GoIdent, ") Descriptor() ([]byte, []int) {") |
| 418 | g.P("return ", f.descriptorVar, ", []int{", strings.Join(indexes, ","), "}") |
| 419 | g.P("}") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 420 | g.P() |
| 421 | |
| 422 | // ExtensionRangeArray |
| 423 | if extranges := message.Desc.ExtensionRanges(); extranges.Len() > 0 { |
Damien Neil | 204f1c0 | 2018-10-23 15:03:38 -0700 | [diff] [blame] | 424 | if message.Desc.Options().(*descpb.MessageOptions).GetMessageSetWireFormat() { |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 425 | g.P("func (m *", message.GoIdent, ") MarshalJSON() ([]byte, error) {") |
| 426 | g.P("return ", protogen.GoIdent{ |
| 427 | GoImportPath: protoPackage, |
| 428 | GoName: "MarshalMessageSetJSON", |
| 429 | }, "(&m.XXX_InternalExtensions)") |
| 430 | g.P("}") |
| 431 | g.P("func (m *", message.GoIdent, ") UnmarshalJSON(buf []byte) error {") |
| 432 | g.P("return ", protogen.GoIdent{ |
| 433 | GoImportPath: protoPackage, |
| 434 | GoName: "UnmarshalMessageSetJSON", |
| 435 | }, "(buf, &m.XXX_InternalExtensions)") |
| 436 | g.P("}") |
| 437 | g.P() |
| 438 | } |
| 439 | |
| 440 | protoExtRange := protogen.GoIdent{ |
| 441 | GoImportPath: protoPackage, |
| 442 | GoName: "ExtensionRange", |
| 443 | } |
| 444 | extRangeVar := "extRange_" + message.GoIdent.GoName |
| 445 | g.P("var ", extRangeVar, " = []", protoExtRange, " {") |
| 446 | for i := 0; i < extranges.Len(); i++ { |
| 447 | r := extranges.Get(i) |
| 448 | g.P("{Start:", r[0], ", End:", r[1]-1 /* inclusive */, "},") |
| 449 | } |
| 450 | g.P("}") |
| 451 | g.P() |
| 452 | g.P("func (*", message.GoIdent, ") ExtensionRangeArray() []", protoExtRange, " {") |
| 453 | g.P("return ", extRangeVar) |
| 454 | g.P("}") |
| 455 | g.P() |
| 456 | } |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 457 | |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 458 | genWellKnownType(g, "*", message.GoIdent, message.Desc) |
| 459 | |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 460 | // Table-driven proto support. |
| 461 | // |
| 462 | // TODO: It does not scale to keep adding another method for every |
| 463 | // operation on protos that we want to switch over to using the |
| 464 | // table-driven approach. Instead, we should only add a single method |
| 465 | // that allows getting access to the *InternalMessageInfo struct and then |
| 466 | // calling Unmarshal, Marshal, Merge, Size, and Discard directly on that. |
| 467 | messageInfoVar := "xxx_messageInfo_" + message.GoIdent.GoName |
| 468 | // XXX_Unmarshal |
| 469 | g.P("func (m *", message.GoIdent, ") XXX_Unmarshal(b []byte) error {") |
| 470 | g.P("return ", messageInfoVar, ".Unmarshal(m, b)") |
| 471 | g.P("}") |
| 472 | // XXX_Marshal |
| 473 | g.P("func (m *", message.GoIdent, ") XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {") |
| 474 | g.P("return ", messageInfoVar, ".Marshal(b, m, deterministic)") |
| 475 | g.P("}") |
| 476 | // XXX_Merge |
| 477 | g.P("func (m *", message.GoIdent, ") XXX_Merge(src proto.Message) {") |
| 478 | g.P(messageInfoVar, ".Merge(m, src)") |
| 479 | g.P("}") |
| 480 | // XXX_Size |
| 481 | g.P("func (m *", message.GoIdent, ") XXX_Size() int {") |
| 482 | g.P("return ", messageInfoVar, ".Size(m)") |
| 483 | g.P("}") |
| 484 | // XXX_DiscardUnknown |
| 485 | g.P("func (m *", message.GoIdent, ") XXX_DiscardUnknown() {") |
| 486 | g.P(messageInfoVar, ".DiscardUnknown(m)") |
| 487 | g.P("}") |
| 488 | g.P() |
| 489 | g.P("var ", messageInfoVar, " ", protogen.GoIdent{ |
| 490 | GoImportPath: protoPackage, |
| 491 | GoName: "InternalMessageInfo", |
| 492 | }) |
| 493 | g.P() |
| 494 | |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 495 | // Constants and vars holding the default values of fields. |
| 496 | for _, field := range message.Fields { |
Damien Neil | ccf3fa6 | 2018-09-28 14:41:45 -0700 | [diff] [blame] | 497 | if !fieldHasDefault(field) { |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 498 | continue |
| 499 | } |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 500 | defVarName := "Default_" + message.GoIdent.GoName + "_" + field.GoName |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 501 | def := field.Desc.Default() |
| 502 | switch field.Desc.Kind() { |
| 503 | case protoreflect.StringKind: |
| 504 | g.P("const ", defVarName, " string = ", strconv.Quote(def.String())) |
| 505 | case protoreflect.BytesKind: |
| 506 | g.P("var ", defVarName, " []byte = []byte(", strconv.Quote(string(def.Bytes())), ")") |
| 507 | case protoreflect.EnumKind: |
Damien Neil | a485fbd | 2018-10-26 13:28:37 -0700 | [diff] [blame] | 508 | evalueDesc := field.Desc.DefaultEnumValue() |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 509 | enum := field.EnumType |
Damien Neil | a485fbd | 2018-10-26 13:28:37 -0700 | [diff] [blame] | 510 | evalue := enum.Values[evalueDesc.Index()] |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 511 | g.P("const ", defVarName, " ", field.EnumType.GoIdent, " = ", evalue.GoIdent) |
| 512 | case protoreflect.FloatKind, protoreflect.DoubleKind: |
| 513 | // Floating point numbers need extra handling for -Inf/Inf/NaN. |
| 514 | f := field.Desc.Default().Float() |
| 515 | goType := "float64" |
| 516 | if field.Desc.Kind() == protoreflect.FloatKind { |
| 517 | goType = "float32" |
| 518 | } |
| 519 | // funcCall returns a call to a function in the math package, |
| 520 | // possibly converting the result to float32. |
| 521 | funcCall := func(fn, param string) string { |
| 522 | s := g.QualifiedGoIdent(protogen.GoIdent{ |
| 523 | GoImportPath: "math", |
| 524 | GoName: fn, |
| 525 | }) + param |
| 526 | if goType != "float64" { |
| 527 | s = goType + "(" + s + ")" |
| 528 | } |
| 529 | return s |
| 530 | } |
| 531 | switch { |
| 532 | case math.IsInf(f, -1): |
| 533 | g.P("var ", defVarName, " ", goType, " = ", funcCall("Inf", "(-1)")) |
| 534 | case math.IsInf(f, 1): |
| 535 | g.P("var ", defVarName, " ", goType, " = ", funcCall("Inf", "(1)")) |
| 536 | case math.IsNaN(f): |
| 537 | g.P("var ", defVarName, " ", goType, " = ", funcCall("NaN", "()")) |
| 538 | default: |
Damien Neil | 982684b | 2018-09-28 14:12:41 -0700 | [diff] [blame] | 539 | g.P("const ", defVarName, " ", goType, " = ", field.Desc.Default().Interface()) |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 540 | } |
| 541 | default: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 542 | goType, _ := fieldGoType(g, field) |
Damien Neil | ebc699d | 2018-09-13 08:50:13 -0700 | [diff] [blame] | 543 | g.P("const ", defVarName, " ", goType, " = ", def.Interface()) |
| 544 | } |
| 545 | } |
| 546 | g.P() |
| 547 | |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 548 | // Getters. |
| 549 | for _, field := range message.Fields { |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 550 | if field.OneofType != nil { |
| 551 | if field == field.OneofType.Fields[0] { |
| 552 | genOneofTypes(gen, g, f, message, field.OneofType) |
| 553 | } |
| 554 | } |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 555 | goType, pointer := fieldGoType(g, field) |
| 556 | defaultValue := fieldDefaultValue(g, message, field) |
Damien Neil | 204f1c0 | 2018-10-23 15:03:38 -0700 | [diff] [blame] | 557 | if field.Desc.Options().(*descpb.FieldOptions).GetDeprecated() { |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 558 | g.P(deprecationComment(true)) |
| 559 | } |
Damien Neil | 162c127 | 2018-10-04 12:42:37 -0700 | [diff] [blame] | 560 | g.Annotate(message.GoIdent.GoName+".Get"+field.GoName, field.Location) |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 561 | g.P("func (m *", message.GoIdent, ") Get", field.GoName, "() ", goType, " {") |
| 562 | if field.OneofType != nil { |
Damien Neil | 81d6d83 | 2018-09-19 12:12:17 -0700 | [diff] [blame] | 563 | 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] | 564 | g.P("return x.", field.GoName) |
| 565 | g.P("}") |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 566 | } else { |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 567 | if field.Desc.Syntax() == protoreflect.Proto3 || defaultValue == "nil" { |
| 568 | g.P("if m != nil {") |
| 569 | } else { |
| 570 | g.P("if m != nil && m.", field.GoName, " != nil {") |
| 571 | } |
| 572 | star := "" |
| 573 | if pointer { |
| 574 | star = "*" |
| 575 | } |
| 576 | g.P("return ", star, " m.", field.GoName) |
| 577 | g.P("}") |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 578 | } |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 579 | g.P("return ", defaultValue) |
| 580 | g.P("}") |
| 581 | g.P() |
| 582 | } |
Damien Neil | a1c6abc | 2018-09-12 13:36:34 -0700 | [diff] [blame] | 583 | |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 584 | if len(message.Oneofs) > 0 { |
| 585 | genOneofFuncs(gen, g, f, message) |
| 586 | } |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 587 | for _, extension := range message.Extensions { |
| 588 | genExtension(gen, g, f, extension) |
| 589 | } |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 590 | } |
Damien Neil | cab8dfe | 2018-09-06 14:51:28 -0700 | [diff] [blame] | 591 | |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 592 | // fieldGoType returns the Go type used for a field. |
| 593 | // |
| 594 | // If it returns pointer=true, the struct field is a pointer to the type. |
| 595 | func fieldGoType(g *protogen.GeneratedFile, field *protogen.Field) (goType string, pointer bool) { |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 596 | pointer = true |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 597 | switch field.Desc.Kind() { |
| 598 | case protoreflect.BoolKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 599 | goType = "bool" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 600 | case protoreflect.EnumKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 601 | goType = g.QualifiedGoIdent(field.EnumType.GoIdent) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 602 | case protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Sfixed32Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 603 | goType = "int32" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 604 | case protoreflect.Uint32Kind, protoreflect.Fixed32Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 605 | goType = "uint32" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 606 | case protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Sfixed64Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 607 | goType = "int64" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 608 | case protoreflect.Uint64Kind, protoreflect.Fixed64Kind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 609 | goType = "uint64" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 610 | case protoreflect.FloatKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 611 | goType = "float32" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 612 | case protoreflect.DoubleKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 613 | goType = "float64" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 614 | case protoreflect.StringKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 615 | goType = "string" |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 616 | case protoreflect.BytesKind: |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 617 | goType = "[]byte" |
| 618 | pointer = false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 619 | case protoreflect.MessageKind, protoreflect.GroupKind: |
Damien Neil | 0bd5a38 | 2018-09-13 15:07:10 -0700 | [diff] [blame] | 620 | if field.Desc.IsMap() { |
| 621 | keyType, _ := fieldGoType(g, field.MessageType.Fields[0]) |
| 622 | valType, _ := fieldGoType(g, field.MessageType.Fields[1]) |
| 623 | return fmt.Sprintf("map[%v]%v", keyType, valType), false |
| 624 | } |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 625 | goType = "*" + g.QualifiedGoIdent(field.MessageType.GoIdent) |
| 626 | pointer = false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 627 | } |
| 628 | if field.Desc.Cardinality() == protoreflect.Repeated { |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 629 | goType = "[]" + goType |
| 630 | pointer = false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 631 | } |
Damien Neil | 44000a1 | 2018-10-24 12:31:16 -0700 | [diff] [blame] | 632 | // Extension fields always have pointer type, even when defined in a proto3 file. |
| 633 | if field.Desc.Syntax() == protoreflect.Proto3 && field.Desc.ExtendedType() == nil { |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 634 | pointer = false |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 635 | } |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 636 | return goType, pointer |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | func fieldProtobufTag(field *protogen.Field) string { |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame] | 640 | var enumName string |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 641 | if field.Desc.Kind() == protoreflect.EnumKind { |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame] | 642 | enumName = enumRegistryName(field.EnumType) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 643 | } |
Joe Tsai | 05828db | 2018-11-01 13:52:16 -0700 | [diff] [blame] | 644 | return tag.Marshal(field.Desc, enumName) |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 647 | func fieldDefaultValue(g *protogen.GeneratedFile, message *protogen.Message, field *protogen.Field) string { |
| 648 | if field.Desc.Cardinality() == protoreflect.Repeated { |
| 649 | return "nil" |
| 650 | } |
Damien Neil | ccf3fa6 | 2018-09-28 14:41:45 -0700 | [diff] [blame] | 651 | if fieldHasDefault(field) { |
Damien Neil | 1fa78d8 | 2018-09-13 13:12:36 -0700 | [diff] [blame] | 652 | defVarName := "Default_" + message.GoIdent.GoName + "_" + field.GoName |
Damien Neil | 77f82fe | 2018-09-13 10:59:17 -0700 | [diff] [blame] | 653 | if field.Desc.Kind() == protoreflect.BytesKind { |
| 654 | return "append([]byte(nil), " + defVarName + "...)" |
| 655 | } |
| 656 | return defVarName |
| 657 | } |
| 658 | switch field.Desc.Kind() { |
| 659 | case protoreflect.BoolKind: |
| 660 | return "false" |
| 661 | case protoreflect.StringKind: |
| 662 | return `""` |
| 663 | case protoreflect.MessageKind, protoreflect.GroupKind, protoreflect.BytesKind: |
| 664 | return "nil" |
| 665 | case protoreflect.EnumKind: |
| 666 | return g.QualifiedGoIdent(field.EnumType.Values[0].GoIdent) |
| 667 | default: |
| 668 | return "0" |
| 669 | } |
| 670 | } |
| 671 | |
Damien Neil | ccf3fa6 | 2018-09-28 14:41:45 -0700 | [diff] [blame] | 672 | // fieldHasDefault returns true if we consider a field to have a default value. |
| 673 | // |
| 674 | // For consistency with the previous generator, it returns false for fields with |
| 675 | // [default=""], preventing the generation of a default const or var for these |
| 676 | // fields. |
| 677 | // |
| 678 | // TODO: Drop this special case. |
| 679 | func fieldHasDefault(field *protogen.Field) bool { |
| 680 | if !field.Desc.HasDefault() { |
| 681 | return false |
| 682 | } |
| 683 | switch field.Desc.Kind() { |
| 684 | case protoreflect.StringKind: |
| 685 | return field.Desc.Default().String() != "" |
| 686 | case protoreflect.BytesKind: |
| 687 | return len(field.Desc.Default().Bytes()) > 0 |
| 688 | } |
| 689 | return true |
| 690 | } |
| 691 | |
Damien Neil | 658051b | 2018-09-10 12:26:21 -0700 | [diff] [blame] | 692 | func fieldJSONTag(field *protogen.Field) string { |
| 693 | return string(field.Desc.Name()) + ",omitempty" |
| 694 | } |
| 695 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 696 | func genExtension(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, extension *protogen.Extension) { |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 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() |
Damien Neil | 6238696 | 2018-10-30 10:35:48 -0700 | [diff] [blame] | 706 | if n, ok := isExtensionMessageSetElement(extension); ok { |
| 707 | name = n |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Damien Neil | 6b54131 | 2018-10-29 09:14:14 -0700 | [diff] [blame] | 710 | g.P("var ", extensionVar(f.File, extension), " = &", protogen.GoIdent{ |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 711 | GoImportPath: protoPackage, |
| 712 | GoName: "ExtensionDesc", |
| 713 | }, "{") |
| 714 | g.P("ExtendedType: (*", extension.ExtendedType.GoIdent, ")(nil),") |
| 715 | goType, pointer := fieldGoType(g, extension) |
| 716 | if pointer { |
| 717 | goType = "*" + goType |
| 718 | } |
| 719 | g.P("ExtensionType: (", goType, ")(nil),") |
| 720 | g.P("Field: ", extension.Desc.Number(), ",") |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 721 | g.P("Name: ", strconv.Quote(string(name)), ",") |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 722 | g.P("Tag: ", strconv.Quote(fieldProtobufTag(extension)), ",") |
| 723 | g.P("Filename: ", strconv.Quote(f.Desc.Path()), ",") |
| 724 | g.P("}") |
| 725 | g.P() |
| 726 | } |
| 727 | |
Damien Neil | 6238696 | 2018-10-30 10:35:48 -0700 | [diff] [blame] | 728 | // isExtensionMessageSetELement returns the adjusted name of an extension |
| 729 | // which extends proto2.bridge.MessageSet. |
| 730 | func isExtensionMessageSetElement(extension *protogen.Extension) (name protoreflect.FullName, ok bool) { |
| 731 | opts := extension.ExtendedType.Desc.Options().(*descpb.MessageOptions) |
| 732 | if !opts.GetMessageSetWireFormat() || extension.Desc.Name() != "message_set_extension" { |
| 733 | return "", false |
| 734 | } |
| 735 | if extension.ParentMessage == nil { |
| 736 | // This case shouldn't be given special handling at all--we're |
| 737 | // only supposed to drop the ".message_set_extension" for |
| 738 | // extensions defined within a message (i.e., the extension |
| 739 | // takes the message's name). |
| 740 | // |
| 741 | // This matches the behavior of the v1 generator, however. |
| 742 | // |
| 743 | // TODO: See if we can drop this case. |
| 744 | name = extension.Desc.FullName() |
| 745 | name = name[:len(name)-len("message_set_extension")] |
| 746 | return name, true |
| 747 | } |
| 748 | return extension.Desc.FullName().Parent(), true |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 751 | // extensionVar returns the var holding the ExtensionDesc for an extension. |
Damien Neil | 6b54131 | 2018-10-29 09:14:14 -0700 | [diff] [blame] | 752 | func extensionVar(f *protogen.File, extension *protogen.Extension) protogen.GoIdent { |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 753 | name := "E_" |
| 754 | if extension.ParentMessage != nil { |
| 755 | name += extension.ParentMessage.GoIdent.GoName + "_" |
| 756 | } |
| 757 | name += extension.GoName |
| 758 | return protogen.GoIdent{ |
| 759 | GoImportPath: f.GoImportPath, |
| 760 | GoName: name, |
| 761 | } |
| 762 | } |
| 763 | |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 764 | // genInitFunction generates an init function that registers the types in the |
| 765 | // generated file with the proto package. |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 766 | func genInitFunction(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) { |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 767 | if len(f.allMessages) == 0 && len(f.allEnums) == 0 && len(f.allExtensions) == 0 { |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 768 | return |
| 769 | } |
| 770 | |
| 771 | g.P("func init() {") |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 772 | for _, enum := range f.allEnums { |
| 773 | name := enum.GoIdent.GoName |
| 774 | g.P(protogen.GoIdent{ |
| 775 | GoImportPath: protoPackage, |
| 776 | GoName: "RegisterEnum", |
| 777 | }, fmt.Sprintf("(%q, %s_name, %s_value)", enumRegistryName(enum), name, name)) |
| 778 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 779 | for _, message := range f.allMessages { |
| 780 | if message.Desc.IsMapEntry() { |
| 781 | continue |
| 782 | } |
| 783 | |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 784 | for _, extension := range message.Extensions { |
| 785 | genRegisterExtension(gen, g, f, extension) |
| 786 | } |
| 787 | |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 788 | name := message.GoIdent.GoName |
| 789 | g.P(protogen.GoIdent{ |
| 790 | GoImportPath: protoPackage, |
| 791 | GoName: "RegisterType", |
| 792 | }, fmt.Sprintf("((*%s)(nil), %q)", name, message.Desc.FullName())) |
| 793 | |
| 794 | // Types of map fields, sorted by the name of the field message type. |
| 795 | var mapFields []*protogen.Field |
| 796 | for _, field := range message.Fields { |
| 797 | if field.Desc.IsMap() { |
| 798 | mapFields = append(mapFields, field) |
| 799 | } |
| 800 | } |
| 801 | sort.Slice(mapFields, func(i, j int) bool { |
| 802 | ni := mapFields[i].MessageType.Desc.FullName() |
| 803 | nj := mapFields[j].MessageType.Desc.FullName() |
| 804 | return ni < nj |
| 805 | }) |
| 806 | for _, field := range mapFields { |
| 807 | typeName := string(field.MessageType.Desc.FullName()) |
| 808 | goType, _ := fieldGoType(g, field) |
| 809 | g.P(protogen.GoIdent{ |
| 810 | GoImportPath: protoPackage, |
| 811 | GoName: "RegisterMapType", |
| 812 | }, fmt.Sprintf("((%v)(nil), %q)", goType, typeName)) |
| 813 | } |
| 814 | } |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 815 | for _, extension := range f.Extensions { |
| 816 | genRegisterExtension(gen, g, f, extension) |
Damien Neil | 993c04d | 2018-09-14 15:41:11 -0700 | [diff] [blame] | 817 | } |
Damien Neil | ce36f8d | 2018-09-13 15:19:08 -0700 | [diff] [blame] | 818 | g.P("}") |
| 819 | g.P() |
| 820 | } |
| 821 | |
Damien Neil | d39efc8 | 2018-09-24 12:38:10 -0700 | [diff] [blame] | 822 | func genRegisterExtension(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, extension *protogen.Extension) { |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 823 | g.P(protogen.GoIdent{ |
| 824 | GoImportPath: protoPackage, |
| 825 | GoName: "RegisterExtension", |
Damien Neil | 6b54131 | 2018-10-29 09:14:14 -0700 | [diff] [blame] | 826 | }, "(", extensionVar(f.File, extension), ")") |
Damien Neil | 6238696 | 2018-10-30 10:35:48 -0700 | [diff] [blame] | 827 | if name, ok := isExtensionMessageSetElement(extension); ok { |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 828 | goType, pointer := fieldGoType(g, extension) |
| 829 | if pointer { |
| 830 | goType = "*" + goType |
| 831 | } |
| 832 | g.P(protogen.GoIdent{ |
| 833 | GoImportPath: protoPackage, |
| 834 | GoName: "RegisterMessageSetType", |
Damien Neil | 6238696 | 2018-10-30 10:35:48 -0700 | [diff] [blame] | 835 | }, "((", goType, ")(nil), ", extension.Desc.Number(), ",", strconv.Quote(string(name)), ")") |
Damien Neil | 154da98 | 2018-09-19 13:21:58 -0700 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | |
Damien Neil | 55fe1c0 | 2018-09-17 15:11:24 -0700 | [diff] [blame] | 839 | // deprecationComment returns a standard deprecation comment if deprecated is true. |
| 840 | func deprecationComment(deprecated bool) string { |
| 841 | if !deprecated { |
| 842 | return "" |
| 843 | } |
| 844 | return "// Deprecated: Do not use." |
| 845 | } |
| 846 | |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 847 | 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] | 848 | if wellKnownTypes[desc.FullName()] { |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 849 | g.P("func (", ptr, ident, `) XXX_WellKnownType() string { return "`, desc.Name(), `" }`) |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 850 | g.P() |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | // Names of messages and enums for which we will generate XXX_WellKnownType methods. |
| 855 | var wellKnownTypes = map[protoreflect.FullName]bool{ |
Damien Neil | ea7baf4 | 2018-09-28 14:23:44 -0700 | [diff] [blame] | 856 | "google.protobuf.Any": true, |
| 857 | "google.protobuf.Duration": true, |
| 858 | "google.protobuf.Empty": true, |
| 859 | "google.protobuf.Struct": true, |
| 860 | "google.protobuf.Timestamp": true, |
| 861 | |
| 862 | "google.protobuf.BoolValue": true, |
| 863 | "google.protobuf.BytesValue": true, |
| 864 | "google.protobuf.DoubleValue": true, |
| 865 | "google.protobuf.FloatValue": true, |
| 866 | "google.protobuf.Int32Value": true, |
| 867 | "google.protobuf.Int64Value": true, |
| 868 | "google.protobuf.ListValue": true, |
| 869 | "google.protobuf.NullValue": true, |
| 870 | "google.protobuf.StringValue": true, |
| 871 | "google.protobuf.UInt32Value": true, |
| 872 | "google.protobuf.UInt64Value": true, |
| 873 | "google.protobuf.Value": true, |
Damien Neil | 46abb57 | 2018-09-07 12:45:37 -0700 | [diff] [blame] | 874 | } |