diff --git a/encoder.go b/encoder.go new file mode 100644 index 0000000..ef7b7b5 --- /dev/null +++ b/encoder.go @@ -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, + }, + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..e4d54f5 --- /dev/null +++ b/main.go @@ -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") + } +}