From f6b0a1057cd600256cbd921e82de1d45e9a7dd66 Mon Sep 17 00:00:00 2001 From: Yuki Yugui Sonoda Date: Tue, 30 Jun 2015 16:00:32 +0900 Subject: [PATCH 1/2] Report semantic errors in the source to protoc --- .../descriptor/services.go | 4 ++++ .../gengateway/template.go | 2 ++ protoc-gen-grpc-gateway/main.go | 24 +++++++++++++------ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/protoc-gen-grpc-gateway/descriptor/services.go b/protoc-gen-grpc-gateway/descriptor/services.go index b01f42acfdc..49ac4c317d9 100644 --- a/protoc-gen-grpc-gateway/descriptor/services.go +++ b/protoc-gen-grpc-gateway/descriptor/services.go @@ -15,17 +15,20 @@ import ( // It must be called after loadFile is called for all files so that loadServices // can resolve names of message types and their fields. func (r *Registry) loadServices(targetFile string) error { + glog.V(1).Infof("Loading services from %s", targetFile) file := r.files[targetFile] if file == nil { return fmt.Errorf("no such file: %s", targetFile) } var svcs []*Service for _, sd := range file.GetService() { + glog.V(2).Infof("Registering %s", sd.GetName()) svc := &Service{ File: file, ServiceDescriptorProto: sd, } for _, md := range sd.GetMethod() { + glog.V(2).Infof("Processing %s.%s", sd.GetName(), md.GetName()) opts, err := extractAPIOptions(md) if err != nil { glog.Errorf("Failed to extract ApiMethodOptions from %s.%s: %v", svc.GetName(), md.GetName(), err) @@ -44,6 +47,7 @@ func (r *Registry) loadServices(targetFile string) error { if len(svc.Methods) == 0 { continue } + glog.V(2).Infof("Registered %s with %d method(s)", svc.GetName(), len(svc.Methods)) svcs = append(svcs, svc) } file.Services = svcs diff --git a/protoc-gen-grpc-gateway/gengateway/template.go b/protoc-gen-grpc-gateway/gengateway/template.go index 8f4efb7eca9..79259e5ed7c 100644 --- a/protoc-gen-grpc-gateway/gengateway/template.go +++ b/protoc-gen-grpc-gateway/gengateway/template.go @@ -7,6 +7,7 @@ import ( "github.com/gengo/grpc-gateway/internal" "github.com/gengo/grpc-gateway/protoc-gen-grpc-gateway/descriptor" + "github.com/golang/glog" ) type param struct { @@ -58,6 +59,7 @@ func applyTemplate(p param) (string, error) { var methodSeen bool for _, svc := range p.Services { for _, meth := range svc.Methods { + glog.V(2).Infof("Processing %s.%s", svc.GetName(), meth.GetName()) methodSeen = true for _, b := range meth.Bindings { if err := handlerTemplate.Execute(w, binding{Binding: b}); err != nil { diff --git a/protoc-gen-grpc-gateway/main.go b/protoc-gen-grpc-gateway/main.go index 80f02e7b120..83018a45c1b 100644 --- a/protoc-gen-grpc-gateway/main.go +++ b/protoc-gen-grpc-gateway/main.go @@ -66,7 +66,9 @@ func main() { } reg.SetPrefix(*importPrefix) - reg.Load(req) + if err := reg.Load(req); err != nil { + emitError(err) + } g := gengateway.New(reg) @@ -79,16 +81,24 @@ func main() { targets = append(targets, f) } - var resp plugin.CodeGeneratorResponse out, err := g.Generate(targets) + glog.V(1).Info("Processed code generator request") if err != nil { - resp.Error = proto.String(err.Error()) - } else { - resp.File = out + emitError(err) } - glog.V(1).Info("Processed code generator request") + emitFiles(out) +} + +func emitFiles(out []*plugin.CodeGeneratorResponse_File) { + emitResp(&plugin.CodeGeneratorResponse{File: out}) +} + +func emitError(err error) { + emitResp(&plugin.CodeGeneratorResponse{Error: proto.String(err.Error())}) +} - buf, err := proto.Marshal(&resp) +func emitResp(resp *plugin.CodeGeneratorResponse) { + buf, err := proto.Marshal(resp) if err != nil { glog.Fatal(err) } From 2da30c9153a3be0ecaee33420aa3927f0227e067 Mon Sep 17 00:00:00 2001 From: Yuki Yugui Sonoda Date: Tue, 30 Jun 2015 21:23:35 +0900 Subject: [PATCH 2/2] return earlier on error --- protoc-gen-grpc-gateway/main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/protoc-gen-grpc-gateway/main.go b/protoc-gen-grpc-gateway/main.go index 83018a45c1b..454eb1fbbc3 100644 --- a/protoc-gen-grpc-gateway/main.go +++ b/protoc-gen-grpc-gateway/main.go @@ -68,6 +68,7 @@ func main() { reg.SetPrefix(*importPrefix) if err := reg.Load(req); err != nil { emitError(err) + return } g := gengateway.New(reg) @@ -85,6 +86,7 @@ func main() { glog.V(1).Info("Processed code generator request") if err != nil { emitError(err) + return } emitFiles(out) }