Initial boilerplate

This commit is contained in:
Manfred Touron 2016-11-04 21:45:25 +01:00
parent 557b4bcf7f
commit 1773f1b44d
No known key found for this signature in database
GPG Key ID: 9CCF47DF1FD978A1
2 changed files with 79 additions and 0 deletions

30
encoder.go Normal file
View File

@ -0,0 +1,30 @@
package main
import (
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/golang/protobuf/protoc-gen-go/plugin"
)
type GenericTemplateBasedEncoder struct {
service *descriptor.ServiceDescriptorProto
file *descriptor.FileDescriptorProto
}
func NewGenericTemplateBasedEncoder(service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto) (e *GenericTemplateBasedEncoder) {
e = &GenericTemplateBasedEncoder{
service: service,
file: file,
}
return
}
func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File {
var content string = "hello world"
var fileName string = "test.txt"
return []*plugin_go.CodeGeneratorResponse_File{
&plugin_go.CodeGeneratorResponse_File{
Content: &content,
Name: &fileName,
},
}
}

49
main.go Normal file
View File

@ -0,0 +1,49 @@
package main
import (
"io/ioutil"
"os"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/generator"
)
func main() {
g := generator.New()
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
g.Error(err, "reading input")
}
if err := proto.Unmarshal(data, g.Request); err != nil {
g.Error(err, "parsing input proto")
}
if len(g.Request.FileToGenerate) == 0 {
g.Fail("no files to generate")
}
g.CommandLineParameters(g.Request.GetParameter())
// Generate the clients
for _, file := range g.Request.GetProtoFile() {
for _, service := range file.GetService() {
encoder := NewGenericTemplateBasedEncoder(service, file)
g.Response.File = append(g.Response.File, encoder.Files()...)
}
}
// Generate the protobufs
g.GenerateAllFiles()
data, err = proto.Marshal(g.Response)
if err != nil {
g.Error(err, "failed to marshal output proto")
}
_, err = os.Stdout.Write(data)
if err != nil {
g.Error(err, "failed to write output proto")
}
}