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