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

Fix wrong method names #603

Merged
merged 2 commits into from
Apr 16, 2018
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
6 changes: 3 additions & 3 deletions protoc-gen-grpc-gateway/descriptor/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func extractAPIOptions(meth *descriptor.MethodDescriptorProto) (*options.HttpRul

func (r *Registry) newParam(meth *Method, path string) (Parameter, error) {
msg := meth.RequestType
fields, err := r.resolveFiledPath(msg, path)
fields, err := r.resolveFieldPath(msg, path)
if err != nil {
return Parameter{}, err
}
Expand Down Expand Up @@ -216,7 +216,7 @@ func (r *Registry) newBody(meth *Method, path string) (*Body, error) {
case "*":
return &Body{FieldPath: nil}, nil
}
fields, err := r.resolveFiledPath(msg, path)
fields, err := r.resolveFieldPath(msg, path)
if err != nil {
return nil, err
}
Expand All @@ -235,7 +235,7 @@ func lookupField(msg *Message, name string) *Field {
}

// resolveFieldPath resolves "path" into a list of fieldDescriptor, starting from "msg".
func (r *Registry) resolveFiledPath(msg *Message, path string) ([]FieldPathComponent, error) {
func (r *Registry) resolveFieldPath(msg *Message, path string) ([]FieldPathComponent, error) {
if path == "" {
return nil, nil
}
Expand Down
2 changes: 1 addition & 1 deletion protoc-gen-grpc-gateway/descriptor/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ func TestResolveFieldPath(t *testing.T) {
if err != nil {
t.Fatalf("reg.LookupFile(%q) failed with %v; want success; on file=%s", file.GetName(), err, spec.src)
}
_, err = reg.resolveFiledPath(f.Messages[0], spec.path)
_, err = reg.resolveFieldPath(f.Messages[0], spec.path)
if got, want := err != nil, spec.wantErr; got != want {
if want {
t.Errorf("reg.resolveFiledPath(%q, %q) succeeded; want an error", f.Messages[0].GetName(), spec.path)
Expand Down
22 changes: 11 additions & 11 deletions protoc-gen-grpc-gateway/descriptor/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ type Body struct {
FieldPath FieldPath
}

// RHS returns a right-hand-side expression in go to be used to initialize method request object.
// AssignableExpr returns an assignable expression in Go to be used to initialize method request object.
// It starts with "msgExpr", which is the go expression of the method request object.
func (b Body) RHS(msgExpr string) string {
return b.FieldPath.RHS(msgExpr)
func (b Body) AssignableExpr(msgExpr string) string {
return b.FieldPath.AssignableExpr(msgExpr)
}

// FieldPath is a path to a field from a request message.
Expand All @@ -242,20 +242,20 @@ func (p FieldPath) IsNestedProto3() bool {
return false
}

// RHS is a right-hand-side expression in go to be used to assign a value to the target field.
// AssignableExpr is an assignable expression in Go to be used to assign a value to the target field.
// It starts with "msgExpr", which is the go expression of the method request object.
func (p FieldPath) RHS(msgExpr string) string {
func (p FieldPath) AssignableExpr(msgExpr string) string {
l := len(p)
if l == 0 {
return msgExpr
}
components := []string{msgExpr}
for i, c := range p {
if i == l-1 {
components = append(components, c.RHS())
components = append(components, c.AssignableExpr())
continue
}
components = append(components, c.LHS())
components = append(components, c.ValueExpr())
}
return strings.Join(components, ".")
}
Expand All @@ -269,13 +269,13 @@ type FieldPathComponent struct {
Target *Field
}

// RHS returns a right-hand-side expression in go for this field.
func (c FieldPathComponent) RHS() string {
// AssignableExpr returns an assignable expression in go for this field.
func (c FieldPathComponent) AssignableExpr() string {
return gogen.CamelCase(c.Name)
}

// LHS returns a left-hand-side expression in go for this field.
func (c FieldPathComponent) LHS() string {
// ValueExpr returns an expression in go for this field.
func (c FieldPathComponent) ValueExpr() string {
if c.Target.Message.File.proto2() {
return fmt.Sprintf("Get%s()", gogen.CamelCase(c.Name))
}
Expand Down
28 changes: 14 additions & 14 deletions protoc-gen-grpc-gateway/descriptor/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,22 @@ func TestFieldPath(t *testing.T) {
Name: "nest_field",
Target: nest2.Fields[0],
}
if got, want := c1.LHS(), "GetNestField()"; got != want {
t.Errorf("c1.LHS() = %q; want %q", got, want)
if got, want := c1.ValueExpr(), "GetNestField()"; got != want {
t.Errorf("c1.ValueExpr() = %q; want %q", got, want)
}
if got, want := c1.RHS(), "NestField"; got != want {
t.Errorf("c1.RHS() = %q; want %q", got, want)
if got, want := c1.AssignableExpr(), "NestField"; got != want {
t.Errorf("c1.AssignableExpr() = %q; want %q", got, want)
}

c2 := FieldPathComponent{
Name: "nest2_field",
Target: nest.Fields[0],
}
if got, want := c2.LHS(), "Nest2Field"; got != want {
t.Errorf("c2.LHS() = %q; want %q", got, want)
if got, want := c2.ValueExpr(), "Nest2Field"; got != want {
t.Errorf("c2.ValueExpr() = %q; want %q", got, want)
}
if got, want := c2.LHS(), "Nest2Field"; got != want {
t.Errorf("c2.LHS() = %q; want %q", got, want)
if got, want := c2.ValueExpr(), "Nest2Field"; got != want {
t.Errorf("c2.ValueExpr() = %q; want %q", got, want)
}

fp := FieldPath{
Expand All @@ -185,8 +185,8 @@ func TestFieldPath(t *testing.T) {
Target: nest.Fields[1],
},
}
if got, want := fp.RHS("resp"), "resp.GetNestField().Nest2Field.GetNestField().TerminalField"; got != want {
t.Errorf("fp.RHS(%q) = %q; want %q", "resp", got, want)
if got, want := fp.AssignableExpr("resp"), "resp.GetNestField().Nest2Field.GetNestField().TerminalField"; got != want {
t.Errorf("fp.AssignableExpr(%q) = %q; want %q", "resp", got, want)
}

fp2 := FieldPath{
Expand All @@ -195,12 +195,12 @@ func TestFieldPath(t *testing.T) {
Target: nest2.Fields[1],
},
}
if got, want := fp2.RHS("resp"), "resp.Nest2Field.GetNestField().Nest2Field.TerminalField"; got != want {
t.Errorf("fp2.RHS(%q) = %q; want %q", "resp", got, want)
if got, want := fp2.AssignableExpr("resp"), "resp.Nest2Field.GetNestField().Nest2Field.TerminalField"; got != want {
t.Errorf("fp2.AssignableExpr(%q) = %q; want %q", "resp", got, want)
}

var fpEmpty FieldPath
if got, want := fpEmpty.RHS("resp"), "resp"; got != want {
t.Errorf("fpEmpty.RHS(%q) = %q; want %q", "resp", got, want)
if got, want := fpEmpty.AssignableExpr("resp"), "resp"; got != want {
t.Errorf("fpEmpty.AssignableExpr(%q) = %q; want %q", "resp", got, want)
}
}
4 changes: 2 additions & 2 deletions protoc-gen-grpc-gateway/gengateway/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ var (
var metadata runtime.ServerMetadata
{{if .Body}}
if req.ContentLength > 0 {
if err := marshaler.NewDecoder(req.Body).Decode(&{{.Body.RHS "protoReq"}}); err != nil {
if err := marshaler.NewDecoder(req.Body).Decode(&{{.Body.AssignableExpr "protoReq"}}); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
}
Expand All @@ -228,7 +228,7 @@ var (
{{if $param.IsNestedProto3 }}
err = runtime.PopulateFieldFromPath(&protoReq, {{$param | printf "%q"}}, val)
{{else}}
{{$param.RHS "protoReq"}}, err = {{$param.ConvertFuncExpr}}(val)
{{$param.AssignableExpr "protoReq"}}, err = {{$param.ConvertFuncExpr}}(val)
{{end}}
if err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "type mismatch, parameter: %s, error: %v", {{$param | printf "%q"}}, err)
Expand Down