Add all arguments

This commit is contained in:
gfanton 2017-01-12 14:46:43 +01:00
parent 4149bd5c25
commit 72880bafe5
2 changed files with 35 additions and 2 deletions

View File

@ -37,7 +37,7 @@ type Ast struct {
Environment []string `json:"environment"` Environment []string `json:"environment"`
} }
func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) { func NewGenericServiceTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) {
e = &GenericTemplateBasedEncoder{ e = &GenericTemplateBasedEncoder{
service: service, service: service,
file: file, file: file,
@ -53,6 +53,22 @@ func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.Serv
return return
} }
func NewGenericTemplateBasedEncoder(templateDir string, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) {
e = &GenericTemplateBasedEncoder{
service: nil,
file: file,
templateDir: templateDir,
debug: debug,
destinationDir: destinationDir,
}
if debug {
log.Printf("new encoder: file=%q template-dir=%q", file.GetName(), templateDir)
}
return
}
func (e *GenericTemplateBasedEncoder) templates() ([]string, error) { func (e *GenericTemplateBasedEncoder) templates() ([]string, error) {
filenames := []string{} filenames := []string{}

19
main.go
View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/golang/protobuf/protoc-gen-go/generator" "github.com/golang/protobuf/protoc-gen-go/generator"
) )
@ -32,6 +33,7 @@ func main() {
templateDir := "./templates" templateDir := "./templates"
destinationDir := "." destinationDir := "."
debug := false debug := false
all := false
if parameter := g.Request.GetParameter(); parameter != "" { if parameter := g.Request.GetParameter(); parameter != "" {
for _, param := range strings.Split(parameter, ",") { for _, param := range strings.Split(parameter, ",") {
parts := strings.Split(param, "=") parts := strings.Split(param, "=")
@ -55,6 +57,15 @@ func main() {
log.Printf("Err: invalid value for debug: %q", parts[1]) log.Printf("Err: invalid value for debug: %q", parts[1])
} }
break break
case "all":
switch strings.ToLower(parts[1]) {
case "true", "t":
all = true
case "false", "f":
default:
log.Printf("Err: invalid value for debug: %q", parts[1])
}
break
default: default:
log.Printf("Err: unknown parameter: %q", param) log.Printf("Err: unknown parameter: %q", param)
} }
@ -63,8 +74,14 @@ func main() {
// Generate the encoders // Generate the encoders
for _, file := range g.Request.GetProtoFile() { for _, file := range g.Request.GetProtoFile() {
if all {
encoder := NewGenericTemplateBasedEncoder(templateDir, file, debug, destinationDir)
g.Response.File = append(g.Response.File, encoder.Files()...)
continue
}
for _, service := range file.GetService() { for _, service := range file.GetService() {
encoder := NewGenericTemplateBasedEncoder(templateDir, service, file, debug, destinationDir) encoder := NewGenericServiceTemplateBasedEncoder(templateDir, service, file, debug, destinationDir)
g.Response.File = append(g.Response.File, encoder.Files()...) g.Response.File = append(g.Response.File, encoder.Files()...)
} }
} }