Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report semantic errors in the source to protoc #27

Merged
merged 2 commits into from
Jul 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions protoc-gen-grpc-gateway/descriptor/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions protoc-gen-grpc-gateway/gengateway/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 19 additions & 7 deletions protoc-gen-grpc-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ func main() {
}

reg.SetPrefix(*importPrefix)
reg.Load(req)
if err := reg.Load(req); err != nil {
emitError(err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a return here needed? Is it safe to continue if an error occurred?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should process exit code be 0 or non-zero when it emits an error?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for confirming.

return
}

g := gengateway.New(reg)

Expand All @@ -79,16 +82,25 @@ 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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return
}
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)
}
Expand Down