Parse parameters (fix #4)
This commit is contained in:
parent
efcd958d6b
commit
83232e17d7
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
33
main.go
33
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()...)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user