| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
package generator |
| 30 |
|
| 31 |
import ( |
| 32 |
"path" |
| 33 |
"strings" |
| 34 |
|
| 35 |
"bytes" |
| 36 |
"go/parser" |
| 37 |
"go/printer" |
| 38 |
"go/token" |
| 39 |
|
| 40 |
"github.com/gogo/protobuf/gogoproto" |
| 41 |
"github.com/gogo/protobuf/proto" |
| 42 |
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor" |
| 43 |
plugin "github.com/gogo/protobuf/protoc-gen-gogo/plugin" |
| 44 |
) |
| 45 |
|
| 46 |
func (d *FileDescriptor) Messages() []*Descriptor { |
| 47 |
return d.desc |
| 48 |
} |
| 49 |
|
| 50 |
func (d *FileDescriptor) Enums() []*EnumDescriptor { |
| 51 |
return d.enum |
| 52 |
} |
| 53 |
|
| 54 |
func (d *Descriptor) IsGroup() bool { |
| 55 |
return d.group |
| 56 |
} |
| 57 |
|
| 58 |
func (g *Generator) IsGroup(field *descriptor.FieldDescriptorProto) bool { |
| 59 |
if d, ok := g.typeNameToObject[field.GetTypeName()].(*Descriptor); ok { |
| 60 |
return d.IsGroup() |
| 61 |
} |
| 62 |
return false |
| 63 |
} |
| 64 |
|
| 65 |
func (g *Generator) TypeNameByObject(typeName string) Object { |
| 66 |
o, ok := g.typeNameToObject[typeName] |
| 67 |
if !ok { |
| 68 |
g.Fail("can't find object with type", typeName) |
| 69 |
} |
| 70 |
return o |
| 71 |
} |
| 72 |
|
| 73 |
func (g *Generator) OneOfTypeName(message *Descriptor, field *descriptor.FieldDescriptorProto) string { |
| 74 |
typeName := message.TypeName() |
| 75 |
ccTypeName := CamelCaseSlice(typeName) |
| 76 |
fieldName := g.GetOneOfFieldName(message, field) |
| 77 |
tname := ccTypeName + "_" + fieldName |
| 78 |
|
| 79 |
|
| 80 |
ok := true |
| 81 |
for _, desc := range message.nested { |
| 82 |
if strings.Join(desc.TypeName(), "_") == tname { |
| 83 |
ok = false |
| 84 |
break |
| 85 |
} |
| 86 |
} |
| 87 |
for _, enum := range message.enums { |
| 88 |
if strings.Join(enum.TypeName(), "_") == tname { |
| 89 |
ok = false |
| 90 |
break |
| 91 |
} |
| 92 |
} |
| 93 |
if !ok { |
| 94 |
tname += "_" |
| 95 |
} |
| 96 |
return tname |
| 97 |
} |
| 98 |
|
| 99 |
type PluginImports interface { |
| 100 |
NewImport(pkg string) Single |
| 101 |
GenerateImports(file *FileDescriptor) |
| 102 |
} |
| 103 |
|
| 104 |
type pluginImports struct { |
| 105 |
generator *Generator |
| 106 |
singles []Single |
| 107 |
} |
| 108 |
|
| 109 |
func NewPluginImports(generator *Generator) *pluginImports { |
| 110 |
return &pluginImports{generator, make([]Single, 0)} |
| 111 |
} |
| 112 |
|
| 113 |
func (this *pluginImports) NewImport(pkg string) Single { |
| 114 |
imp := newImportedPackage(this.generator.ImportPrefix, pkg) |
| 115 |
this.singles = append(this.singles, imp) |
| 116 |
return imp |
| 117 |
} |
| 118 |
|
| 119 |
func (this *pluginImports) GenerateImports(file *FileDescriptor) { |
| 120 |
for _, s := range this.singles { |
| 121 |
if s.IsUsed() { |
| 122 |
this.generator.PrintImport(s.Name(), s.Location()) |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
type Single interface { |
| 128 |
Use() string |
| 129 |
IsUsed() bool |
| 130 |
Name() string |
| 131 |
Location() string |
| 132 |
} |
| 133 |
|
| 134 |
type importedPackage struct { |
| 135 |
used bool |
| 136 |
pkg string |
| 137 |
name string |
| 138 |
importPrefix string |
| 139 |
} |
| 140 |
|
| 141 |
func newImportedPackage(importPrefix, pkg string) *importedPackage { |
| 142 |
return &importedPackage{ |
| 143 |
pkg: pkg, |
| 144 |
importPrefix: importPrefix, |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
func (this *importedPackage) Use() string { |
| 149 |
if !this.used { |
| 150 |
this.name = RegisterUniquePackageName(this.pkg, nil) |
| 151 |
this.used = true |
| 152 |
} |
| 153 |
return this.name |
| 154 |
} |
| 155 |
|
| 156 |
func (this *importedPackage) IsUsed() bool { |
| 157 |
return this.used |
| 158 |
} |
| 159 |
|
| 160 |
func (this *importedPackage) Name() string { |
| 161 |
return this.name |
| 162 |
} |
| 163 |
|
| 164 |
func (this *importedPackage) Location() string { |
| 165 |
return this.importPrefix + this.pkg |
| 166 |
} |
| 167 |
|
| 168 |
func (g *Generator) GetFieldName(message *Descriptor, field *descriptor.FieldDescriptorProto) string { |
| 169 |
goTyp, _ := g.GoType(message, field) |
| 170 |
fieldname := CamelCase(*field.Name) |
| 171 |
if gogoproto.IsCustomName(field) { |
| 172 |
fieldname = gogoproto.GetCustomName(field) |
| 173 |
} |
| 174 |
if gogoproto.IsEmbed(field) { |
| 175 |
fieldname = EmbedFieldName(goTyp) |
| 176 |
} |
| 177 |
if field.OneofIndex != nil { |
| 178 |
fieldname = message.OneofDecl[int(*field.OneofIndex)].GetName() |
| 179 |
fieldname = CamelCase(fieldname) |
| 180 |
} |
| 181 |
for _, f := range methodNames { |
| 182 |
if f == fieldname { |
| 183 |
return fieldname + "_" |
| 184 |
} |
| 185 |
} |
| 186 |
if !gogoproto.IsProtoSizer(message.file, message.DescriptorProto) { |
| 187 |
if fieldname == "Size" { |
| 188 |
return fieldname + "_" |
| 189 |
} |
| 190 |
} |
| 191 |
return fieldname |
| 192 |
} |
| 193 |
|
| 194 |
func (g *Generator) GetOneOfFieldName(message *Descriptor, field *descriptor.FieldDescriptorProto) string { |
| 195 |
goTyp, _ := g.GoType(message, field) |
| 196 |
fieldname := CamelCase(*field.Name) |
| 197 |
if gogoproto.IsCustomName(field) { |
| 198 |
fieldname = gogoproto.GetCustomName(field) |
| 199 |
} |
| 200 |
if gogoproto.IsEmbed(field) { |
| 201 |
fieldname = EmbedFieldName(goTyp) |
| 202 |
} |
| 203 |
for _, f := range methodNames { |
| 204 |
if f == fieldname { |
| 205 |
return fieldname + "_" |
| 206 |
} |
| 207 |
} |
| 208 |
if !gogoproto.IsProtoSizer(message.file, message.DescriptorProto) { |
| 209 |
if fieldname == "Size" { |
| 210 |
return fieldname + "_" |
| 211 |
} |
| 212 |
} |
| 213 |
return fieldname |
| 214 |
} |
| 215 |
|
| 216 |
func (g *Generator) IsMap(field *descriptor.FieldDescriptorProto) bool { |
| 217 |
if !field.IsMessage() { |
| 218 |
return false |
| 219 |
} |
| 220 |
byName := g.ObjectNamed(field.GetTypeName()) |
| 221 |
desc, ok := byName.(*Descriptor) |
| 222 |
if byName == nil || !ok || !desc.GetOptions().GetMapEntry() { |
| 223 |
return false |
| 224 |
} |
| 225 |
return true |
| 226 |
} |
| 227 |
|
| 228 |
func (g *Generator) GetMapKeyField(field, keyField *descriptor.FieldDescriptorProto) *descriptor.FieldDescriptorProto { |
| 229 |
if !gogoproto.IsCastKey(field) { |
| 230 |
return keyField |
| 231 |
} |
| 232 |
keyField = proto.Clone(keyField).(*descriptor.FieldDescriptorProto) |
| 233 |
if keyField.Options == nil { |
| 234 |
keyField.Options = &descriptor.FieldOptions{} |
| 235 |
} |
| 236 |
keyType := gogoproto.GetCastKey(field) |
| 237 |
if err := proto.SetExtension(keyField.Options, gogoproto.E_Casttype, &keyType); err != nil { |
| 238 |
g.Fail(err.Error()) |
| 239 |
} |
| 240 |
return keyField |
| 241 |
} |
| 242 |
|
| 243 |
func (g *Generator) GetMapValueField(field, valField *descriptor.FieldDescriptorProto) *descriptor.FieldDescriptorProto { |
| 244 |
if gogoproto.IsCustomType(field) && gogoproto.IsCastValue(field) { |
| 245 |
g.Fail("cannot have a customtype and casttype: ", field.String()) |
| 246 |
} |
| 247 |
valField = proto.Clone(valField).(*descriptor.FieldDescriptorProto) |
| 248 |
if valField.Options == nil { |
| 249 |
valField.Options = &descriptor.FieldOptions{} |
| 250 |
} |
| 251 |
|
| 252 |
stdtime := gogoproto.IsStdTime(field) |
| 253 |
if stdtime { |
| 254 |
if err := proto.SetExtension(valField.Options, gogoproto.E_Stdtime, &stdtime); err != nil { |
| 255 |
g.Fail(err.Error()) |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
stddur := gogoproto.IsStdDuration(field) |
| 260 |
if stddur { |
| 261 |
if err := proto.SetExtension(valField.Options, gogoproto.E_Stdduration, &stddur); err != nil { |
| 262 |
g.Fail(err.Error()) |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
if valType := gogoproto.GetCastValue(field); len(valType) > 0 { |
| 267 |
if err := proto.SetExtension(valField.Options, gogoproto.E_Casttype, &valType); err != nil { |
| 268 |
g.Fail(err.Error()) |
| 269 |
} |
| 270 |
} |
| 271 |
if valType := gogoproto.GetCustomType(field); len(valType) > 0 { |
| 272 |
if err := proto.SetExtension(valField.Options, gogoproto.E_Customtype, &valType); err != nil { |
| 273 |
g.Fail(err.Error()) |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
nullable := gogoproto.IsNullable(field) |
| 278 |
if err := proto.SetExtension(valField.Options, gogoproto.E_Nullable, &nullable); err != nil { |
| 279 |
g.Fail(err.Error()) |
| 280 |
} |
| 281 |
return valField |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
func GoMapValueTypes(mapField, valueField *descriptor.FieldDescriptorProto, goValueType, goValueAliasType string) (nullable bool, outGoType string, outGoAliasType string) { |
| 287 |
nullable = gogoproto.IsNullable(mapField) && (valueField.IsMessage() || gogoproto.IsCustomType(mapField)) |
| 288 |
if nullable { |
| 289 |
|
| 290 |
if strings.HasPrefix(goValueType, "*") { |
| 291 |
outGoType = goValueType |
| 292 |
} else { |
| 293 |
outGoType = "*" + goValueType |
| 294 |
} |
| 295 |
outGoAliasType = goValueAliasType |
| 296 |
} else { |
| 297 |
outGoType = strings.Replace(goValueType, "*", "", 1) |
| 298 |
outGoAliasType = strings.Replace(goValueAliasType, "*", "", 1) |
| 299 |
} |
| 300 |
return |
| 301 |
} |
| 302 |
|
| 303 |
func GoTypeToName(goTyp string) string { |
| 304 |
return strings.Replace(strings.Replace(goTyp, "*", "", -1), "[]", "", -1) |
| 305 |
} |
| 306 |
|
| 307 |
func EmbedFieldName(goTyp string) string { |
| 308 |
goTyp = GoTypeToName(goTyp) |
| 309 |
goTyps := strings.Split(goTyp, ".") |
| 310 |
if len(goTyps) == 1 { |
| 311 |
return goTyp |
| 312 |
} |
| 313 |
if len(goTyps) == 2 { |
| 314 |
return goTyps[1] |
| 315 |
} |
| 316 |
panic("unreachable") |
| 317 |
} |
| 318 |
|
| 319 |
func (g *Generator) GeneratePlugin(p Plugin) { |
| 320 |
p.Init(g) |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
genFileMap := make(map[*FileDescriptor]bool, len(g.genFiles)) |
| 325 |
for _, file := range g.genFiles { |
| 326 |
genFileMap[file] = true |
| 327 |
} |
| 328 |
for _, file := range g.allFiles { |
| 329 |
g.Reset() |
| 330 |
g.writeOutput = genFileMap[file] |
| 331 |
g.generatePlugin(file, p) |
| 332 |
if !g.writeOutput { |
| 333 |
continue |
| 334 |
} |
| 335 |
g.Response.File = append(g.Response.File, &plugin.CodeGeneratorResponse_File{ |
| 336 |
Name: proto.String(file.goFileName()), |
| 337 |
Content: proto.String(g.String()), |
| 338 |
}) |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
func (g *Generator) SetFile(file *descriptor.FileDescriptorProto) { |
| 343 |
g.file = g.FileOf(file) |
| 344 |
} |
| 345 |
|
| 346 |
func (g *Generator) generatePlugin(file *FileDescriptor, p Plugin) { |
| 347 |
g.writtenImports = make(map[string]bool) |
| 348 |
g.file = g.FileOf(file.FileDescriptorProto) |
| 349 |
g.usedPackages = make(map[string]bool) |
| 350 |
|
| 351 |
|
| 352 |
p.Generate(file) |
| 353 |
|
| 354 |
|
| 355 |
rem := g.Buffer |
| 356 |
g.Buffer = new(bytes.Buffer) |
| 357 |
g.generateHeader() |
| 358 |
p.GenerateImports(g.file) |
| 359 |
g.generateImports() |
| 360 |
if !g.writeOutput { |
| 361 |
return |
| 362 |
} |
| 363 |
g.Write(rem.Bytes()) |
| 364 |
|
| 365 |
|
| 366 |
contents := string(g.Buffer.Bytes()) |
| 367 |
fset := token.NewFileSet() |
| 368 |
ast, err := parser.ParseFile(fset, "", g, parser.ParseComments) |
| 369 |
if err != nil { |
| 370 |
g.Fail("bad Go source code was generated:", contents, err.Error()) |
| 371 |
return |
| 372 |
} |
| 373 |
g.Reset() |
| 374 |
err = (&printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}).Fprint(g, fset, ast) |
| 375 |
if err != nil { |
| 376 |
g.Fail("generated Go source code could not be reformatted:", err.Error()) |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
func GetCustomType(field *descriptor.FieldDescriptorProto) (packageName string, typ string, err error) { |
| 381 |
return getCustomType(field) |
| 382 |
} |
| 383 |
|
| 384 |
func getCustomType(field *descriptor.FieldDescriptorProto) (packageName string, typ string, err error) { |
| 385 |
if field.Options != nil { |
| 386 |
var v interface{} |
| 387 |
v, err = proto.GetExtension(field.Options, gogoproto.E_Customtype) |
| 388 |
if err == nil && v.(*string) != nil { |
| 389 |
ctype := *(v.(*string)) |
| 390 |
packageName, typ = splitCPackageType(ctype) |
| 391 |
return packageName, typ, nil |
| 392 |
} |
| 393 |
} |
| 394 |
return "", "", err |
| 395 |
} |
| 396 |
|
| 397 |
func splitCPackageType(ctype string) (packageName string, typ string) { |
| 398 |
ss := strings.Split(ctype, ".") |
| 399 |
if len(ss) == 1 { |
| 400 |
return "", ctype |
| 401 |
} |
| 402 |
packageName = strings.Join(ss[0:len(ss)-1], ".") |
| 403 |
typeName := ss[len(ss)-1] |
| 404 |
importStr := strings.Map(badToUnderscore, packageName) |
| 405 |
typ = importStr + "." + typeName |
| 406 |
return packageName, typ |
| 407 |
} |
| 408 |
|
| 409 |
func getCastType(field *descriptor.FieldDescriptorProto) (packageName string, typ string, err error) { |
| 410 |
if field.Options != nil { |
| 411 |
var v interface{} |
| 412 |
v, err = proto.GetExtension(field.Options, gogoproto.E_Casttype) |
| 413 |
if err == nil && v.(*string) != nil { |
| 414 |
ctype := *(v.(*string)) |
| 415 |
packageName, typ = splitCPackageType(ctype) |
| 416 |
return packageName, typ, nil |
| 417 |
} |
| 418 |
} |
| 419 |
return "", "", err |
| 420 |
} |
| 421 |
|
| 422 |
func FileName(file *FileDescriptor) string { |
| 423 |
fname := path.Base(file.FileDescriptorProto.GetName()) |
| 424 |
fname = strings.Replace(fname, ".proto", "", -1) |
| 425 |
fname = strings.Replace(fname, "-", "_", -1) |
| 426 |
fname = strings.Replace(fname, ".", "_", -1) |
| 427 |
return CamelCase(fname) |
| 428 |
} |
| 429 |
|
| 430 |
func (g *Generator) AllFiles() *descriptor.FileDescriptorSet { |
| 431 |
set := &descriptor.FileDescriptorSet{} |
| 432 |
set.File = make([]*descriptor.FileDescriptorProto, len(g.allFiles)) |
| 433 |
for i := range g.allFiles { |
| 434 |
set.File[i] = g.allFiles[i].FileDescriptorProto |
| 435 |
} |
| 436 |
return set |
| 437 |
} |
| 438 |
|
| 439 |
func (d *Descriptor) Path() string { |
| 440 |
return d.path |
| 441 |
} |
| 442 |
|
| 443 |
func (g *Generator) useTypes() string { |
| 444 |
pkg := strings.Map(badToUnderscore, "github.com/gogo/protobuf/types") |
| 445 |
g.customImports = append(g.customImports, "github.com/gogo/protobuf/types") |
| 446 |
return pkg |
| 447 |
} |