Пример #1
0
func (p *test) Generate(imports generator.PluginImports, file *generator.FileDescriptor) bool {
	used := false
	testingPkg := imports.NewImport("testing")
	for _, message := range file.Messages() {
		if !gogoproto.HasDescription(file.FileDescriptorProto, message.DescriptorProto) ||
			!gogoproto.HasTestGen(file.FileDescriptorProto, message.DescriptorProto) {
			continue
		}
		used = true
	}

	if used {
		localName := generator.FileName(file)
		p.P(`func Test`, localName, `Description(t *`, testingPkg.Use(), `.T) {`)
		p.In()
		p.P(localName, `Description()`)
		p.Out()
		p.P(`}`)

	}
	return used
}
Пример #2
0
func (p *plugin) Generate(file *generator.FileDescriptor) {
	p.used = false
	localName := generator.FileName(file)
	for _, message := range file.Messages() {
		if !gogoproto.HasDescription(file.FileDescriptorProto, message.DescriptorProto) {
			continue
		}
		p.used = true
		ccTypeName := generator.CamelCaseSlice(message.TypeName())
		p.P(`func (this *`, ccTypeName, `) Description() (desc *google_protobuf.FileDescriptorSet) {`)
		p.In()
		p.P(`return `, localName, `Description()`)
		p.Out()
		p.P(`}`)
	}

	if p.used {

		p.P(`func `, localName, `Description() (desc *google_protobuf.FileDescriptorSet) {`)
		p.In()
		//Don't generate SourceCodeInfo, since it will create too much code.

		ss := make([]*descriptor.SourceCodeInfo, 0)
		for _, f := range p.Generator.AllFiles().GetFile() {
			ss = append(ss, f.SourceCodeInfo)
			f.SourceCodeInfo = nil
		}
		s := fmt.Sprintf("%#v", p.Generator.AllFiles())
		for i, f := range p.Generator.AllFiles().GetFile() {
			f.SourceCodeInfo = ss[i]
		}
		p.P(`return `, s)
		p.Out()
		p.P(`}`)
	}
}