From 83232e17d7de68f1dddbe8fc0ff6f2eb5ebdeed7 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Mon, 7 Nov 2016 08:45:02 +0100 Subject: [PATCH] Parse parameters (fix #4) --- encoder.go | 4 +++- examples/dummy/Makefile | 2 +- main.go | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/encoder.go b/encoder.go index bb60090..99e4644 100644 --- a/encoder.go +++ b/encoder.go @@ -15,6 +15,7 @@ type GenericTemplateBasedEncoder struct { templateDir string service *descriptor.ServiceDescriptorProto file *descriptor.FileDescriptorProto + debug bool } type Ast struct { @@ -23,11 +24,12 @@ type Ast struct { File *descriptor.FileDescriptorProto } -func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto) (e *GenericTemplateBasedEncoder) { +func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool) (e *GenericTemplateBasedEncoder) { e = &GenericTemplateBasedEncoder{ service: service, file: file, templateDir: templateDir, + debug: debug, } return } diff --git a/examples/dummy/Makefile b/examples/dummy/Makefile index 01e9488..cc27729 100644 --- a/examples/dummy/Makefile +++ b/examples/dummy/Makefile @@ -1,7 +1,7 @@ .PHONY: build build: mkdir -p output - protoc -I. --gotemplate_out=output *.proto + protoc -I. --gotemplate_out=template_dir=templates,debug=true:output *.proto .PHONY: re diff --git a/main.go b/main.go index 59e60d2..89f157e 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,9 @@ package main import ( "io/ioutil" + "log" "os" + "strings" "github.com/golang/protobuf/proto" "github.com/golang/protobuf/protoc-gen-go/generator" @@ -26,10 +28,37 @@ func main() { g.CommandLineParameters(g.Request.GetParameter()) - // Generate the clients + // Parse parameters + templateDir := "./templates" + debug := false + if parameter := g.Request.GetParameter(); parameter != "" { + for _, param := range strings.Split(parameter, ",") { + parts := strings.Split(param, "=") + if len(parts) != 2 { + log.Printf("Err: invalid parameter: %q", param) + continue + } + switch parts[0] { + case "template_dir": + templateDir = parts[1] + break + case "debug": + if parts[1] == "true" { + debug = true + } else { + log.Printf("Err: invalid value for debug: %q", parts[1]) + } + break + default: + log.Printf("Err: unknown parameter: %q", param) + } + } + } + + // Generate the encoders for _, file := range g.Request.GetProtoFile() { for _, service := range file.GetService() { - encoder := NewGenericTemplateBasedEncoder("templates", service, file) + encoder := NewGenericTemplateBasedEncoder(templateDir, service, file, debug) g.Response.File = append(g.Response.File, encoder.Files()...) } }