Beispiel #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
		}
		if message.DescriptorProto.GetOptions().GetMapEntry() {
			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
}
Beispiel #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
		}
		if message.DescriptorProto.GetOptions().GetMapEntry() {
			continue
		}
		p.used = true
		ccTypeName := generator.CamelCaseSlice(message.TypeName())
		p.P(`func (this *`, ccTypeName, `) Description() (desc *descriptor.FileDescriptorSet) {`)
		p.In()
		p.P(`return `, localName, `Description()`)
		p.Out()
		p.P(`}`)
	}

	if p.used {

		p.P(`func `, localName, `Description() (desc *descriptor.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(`}`)
	}
}
Beispiel #3
0
func (p *plugin) Generate(file *generator.FileDescriptor) {
	used := false
	localName := generator.FileName(file)

	p.PluginImports = generator.NewPluginImports(p.Generator)
	descriptorPkg := p.NewImport("github.com/gogo/protobuf/protoc-gen-gogo/descriptor")
	protoPkg := p.NewImport("github.com/gogo/protobuf/proto")
	gzipPkg := p.NewImport("compress/gzip")
	bytesPkg := p.NewImport("bytes")
	ioutilPkg := p.NewImport("io/ioutil")

	for _, message := range file.Messages() {
		if !gogoproto.HasDescription(file.FileDescriptorProto, message.DescriptorProto) {
			continue
		}
		if message.DescriptorProto.GetOptions().GetMapEntry() {
			continue
		}
		used = true
		ccTypeName := generator.CamelCaseSlice(message.TypeName())
		p.P(`func (this *`, ccTypeName, `) Description() (desc *`, descriptorPkg.Use(), `.FileDescriptorSet) {`)
		p.In()
		p.P(`return `, localName, `Description()`)
		p.Out()
		p.P(`}`)
	}

	if used {

		p.P(`func `, localName, `Description() (desc *`, descriptorPkg.Use(), `.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
		}
		b, err := proto.Marshal(p.Generator.AllFiles())
		if err != nil {
			panic(err)
		}
		for i, f := range p.Generator.AllFiles().GetFile() {
			f.SourceCodeInfo = ss[i]
		}
		p.P(`d := &`, descriptorPkg.Use(), `.FileDescriptorSet{}`)
		var buf bytes.Buffer
		w, _ := gzip.NewWriterLevel(&buf, gzip.BestCompression)
		w.Write(b)
		w.Close()
		b = buf.Bytes()
		p.P("var gzipped = []byte{")
		p.In()
		p.P("// ", len(b), " bytes of a gzipped FileDescriptorSet")
		for len(b) > 0 {
			n := 16
			if n > len(b) {
				n = len(b)
			}

			s := ""
			for _, c := range b[:n] {
				s += fmt.Sprintf("0x%02x,", c)
			}
			p.P(s)

			b = b[n:]
		}
		p.Out()
		p.P("}")
		p.P(`r := `, bytesPkg.Use(), `.NewReader(gzipped)`)
		p.P(`gzipr, err := `, gzipPkg.Use(), `.NewReader(r)`)
		p.P(`if err != nil {`)
		p.In()
		p.P(`panic(err)`)
		p.Out()
		p.P(`}`)
		p.P(`ungzipped, err := `, ioutilPkg.Use(), `.ReadAll(gzipr)`)
		p.P(`if err != nil {`)
		p.In()
		p.P(`panic(err)`)
		p.Out()
		p.P(`}`)
		p.P(`if err := `, protoPkg.Use(), `.Unmarshal(ungzipped, d); err != nil {`)
		p.In()
		p.P(`panic(err)`)
		p.Out()
		p.P(`}`)
		p.P(`return d`)
		p.Out()
		p.P(`}`)
	}
}