Parse parameters (fix #4)
This commit is contained in:
		| @@ -15,6 +15,7 @@ type GenericTemplateBasedEncoder struct { | |||||||
| 	templateDir string | 	templateDir string | ||||||
| 	service     *descriptor.ServiceDescriptorProto | 	service     *descriptor.ServiceDescriptorProto | ||||||
| 	file        *descriptor.FileDescriptorProto | 	file        *descriptor.FileDescriptorProto | ||||||
|  | 	debug       bool | ||||||
| } | } | ||||||
|  |  | ||||||
| type Ast struct { | type Ast struct { | ||||||
| @@ -23,11 +24,12 @@ type Ast struct { | |||||||
| 	File     *descriptor.FileDescriptorProto | 	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{ | 	e = &GenericTemplateBasedEncoder{ | ||||||
| 		service:     service, | 		service:     service, | ||||||
| 		file:        file, | 		file:        file, | ||||||
| 		templateDir: templateDir, | 		templateDir: templateDir, | ||||||
|  | 		debug:       debug, | ||||||
| 	} | 	} | ||||||
| 	return | 	return | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,7 +1,7 @@ | |||||||
| .PHONY: build | .PHONY: build | ||||||
| build: | build: | ||||||
| 	mkdir -p output | 	mkdir -p output | ||||||
| 	protoc -I. --gotemplate_out=output *.proto | 	protoc -I. --gotemplate_out=template_dir=templates,debug=true:output *.proto | ||||||
|  |  | ||||||
|  |  | ||||||
| .PHONY: re | .PHONY: re | ||||||
|   | |||||||
							
								
								
									
										33
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								main.go
									
									
									
									
									
								
							| @@ -2,7 +2,9 @@ package main | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
|  | 	"log" | ||||||
| 	"os" | 	"os" | ||||||
|  | 	"strings" | ||||||
|  |  | ||||||
| 	"github.com/golang/protobuf/proto" | 	"github.com/golang/protobuf/proto" | ||||||
| 	"github.com/golang/protobuf/protoc-gen-go/generator" | 	"github.com/golang/protobuf/protoc-gen-go/generator" | ||||||
| @@ -26,10 +28,37 @@ func main() { | |||||||
|  |  | ||||||
| 	g.CommandLineParameters(g.Request.GetParameter()) | 	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 _, file := range g.Request.GetProtoFile() { | ||||||
| 		for _, service := range file.GetService() { | 		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()...) | 			g.Response.File = append(g.Response.File, encoder.Files()...) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user