func (p *test) Generate(imports generator.PluginImports, file *generator.FileDescriptor) bool { used := false randPkg := imports.NewImport("math/rand") timePkg := imports.NewImport("time") testingPkg := imports.NewImport("testing") fmtPkg := imports.NewImport("fmt") for _, message := range file.Messages() { ccTypeName := generator.CamelCaseSlice(message.TypeName()) if !gogoproto.IsStringer(file.FileDescriptorProto, message.DescriptorProto) { continue } if gogoproto.HasTestGen(file.FileDescriptorProto, message.DescriptorProto) { used = true p.P(`func Test`, ccTypeName, `Stringer(t *`, testingPkg.Use(), `.T) {`) p.In() p.P(`popr := `, randPkg.Use(), `.New(`, randPkg.Use(), `.NewSource(`, timePkg.Use(), `.Now().UnixNano()))`) p.P(`p := NewPopulated`, ccTypeName, `(popr, false)`) p.P(`s1 := p.String()`) p.P(`s2 := `, fmtPkg.Use(), `.Sprintf("%v", p)`) p.P(`if s1 != s2 {`) p.In() p.P(`t.Fatalf("String want %v got %v", s1, s2)`) p.Out() p.P(`}`) p.Out() p.P(`}`) } } return used }
func (p *stringer) Generate(file *generator.FileDescriptor) { p.PluginImports = generator.NewPluginImports(p.Generator) p.atleastOne = false p.localName = generator.FileName(file) stringsPkg := p.NewImport("strings") for _, message := range file.Messages() { if !gogoproto.IsStringer(file.FileDescriptorProto, message.DescriptorProto) { continue } p.atleastOne = true ccTypeName := generator.CamelCaseSlice(message.TypeName()) p.P(`func (this *`, ccTypeName, `) String() string {`) p.In() p.P(`if this == nil {`) p.In() p.P(`return "nil"`) p.Out() p.P(`}`) p.P("s := ", stringsPkg.Use(), ".Join([]string{`&", ccTypeName, "{`,") for _, field := range message.Field { fieldname := p.GetFieldName(message, field) if field.IsMessage() || p.IsGroup(field) { desc := p.ObjectNamed(field.GetTypeName()) msgname := p.TypeName(desc) msgnames := strings.Split(msgname, ".") typeName := msgnames[len(msgnames)-1] if field.IsRepeated() { p.P("`", fieldname, ":`", ` + `, stringsPkg.Use(), `.Replace(`, p.Pkg["fmt"], `.Sprintf("%v", this.`, fieldname, `[:this.`, generator.SizerName(fieldname), `]), "`, typeName, `","`, msgname, `"`, ", 1) + `,", "`,") } else { fieldValue := "this.Get" + generator.CamelCase(fieldname) + "()" if gogoproto.IsCustomType(field) || gogoproto.IsEmbed(field) { fieldValue = "this." + fieldname } p.P("`", fieldname, ":`", ` + `, stringsPkg.Use(), `.Replace(`, p.Pkg["fmt"], `.Sprintf("%v", `, fieldValue, `), "`, typeName, `","`, msgname, `"`, ", 1) + `,", "`,") } } else if field.IsRepeated() { p.P("`", fieldname, ":`", ` + `, p.Pkg["fmt"], `.Sprintf("%v", this.`, fieldname, "[:this.", generator.SizerName(fieldname), "]) + `,", "`,") } else { fieldValue := "this.Get" + generator.CamelCase(fieldname) + "()" if gogoproto.IsCustomType(field) || gogoproto.IsEmbed(field) { fieldValue = "this." + fieldname } p.P("`", fieldname, ":`", ` + `, p.Pkg["fmt"], `.Sprintf("%v", `, fieldValue, ") + `,", "`,") } } if message.DescriptorProto.HasExtension() { if gogoproto.HasExtensionsMap(file.FileDescriptorProto, message.DescriptorProto) { p.P("`XXX_extensions:` + proto.StringFromExtensionsMap(this.XXX_extensions) + `,`,") } else { p.P("`XXX_extensions:` + proto.StringFromExtensionsBytes(this.XXX_extensions) + `,`,") } } p.P("`XXX_unrecognized:` + ", p.Pkg["fmt"], `.Sprintf("%v", this.XXX_unrecognized) + `, "`,`,") p.P("`}`,") p.P(`}`, `,""`, ")") p.P(`return s`) p.Out() p.P(`}`) } if !p.atleastOne { return } }