Skip to content

Commit

Permalink
Merge pull request #96 from gengo/feature/avoid-custom-pascal-snake
Browse files Browse the repository at this point in the history
Better compatibility to field names generated by protoc-gen-go
  • Loading branch information
yugui committed Feb 4, 2016
2 parents 5efc4bf + 1da1435 commit 97a6b0b
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 115 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ GO_PLUGIN=bin/protoc-gen-go
GO_PLUGIN_PKG=github.com/golang/protobuf/protoc-gen-go
SWAGGER_PLUGIN=bin/protoc-gen-swagger
SWAGGER_PLUGIN_SRC= utilities/doc.go \
utilities/name.go \
utilities/pattern.go \
utilities/trie.go \
protoc-gen-swagger/genswagger/generator.go \
protoc-gen-swagger/genswagger/template.go \
protoc-gen-swagger/main.go
Expand Down
2 changes: 1 addition & 1 deletion examples/examplepb/a_bit_of_everything.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/examplepb/echo_service.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/examplepb/flow_combination.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions protoc-gen-grpc-gateway/descriptor/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"

"github.com/gengo/grpc-gateway/protoc-gen-grpc-gateway/httprule"
"github.com/gengo/grpc-gateway/utilities"
descriptor "github.com/golang/protobuf/protoc-gen-go/descriptor"
gogen "github.com/golang/protobuf/protoc-gen-go/generator"
)

// GoPackage represents a golang package
Expand Down Expand Up @@ -235,15 +235,15 @@ type FieldPathComponent struct {

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

// LHS returns a left-hand-side expression in go for this field.
func (c FieldPathComponent) LHS() string {
if c.Target.Message.File.proto2() {
return fmt.Sprintf("Get%s()", utilities.PascalFromSnake(c.Name))
return fmt.Sprintf("Get%s()", gogen.CamelCase(c.Name))
}
return utilities.PascalFromSnake(c.Name)
return gogen.CamelCase(c.Name)
}

var (
Expand Down
2 changes: 1 addition & 1 deletion protoc-gen-grpc-gateway/gengateway/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ var _ codes.Code
var _ io.Reader
var _ = runtime.String
var _ = json.Marshal
var _ = utilities.PascalFromSnake
var _ = utilities.NewDoubleArray
`))

handlerTemplate = template.Must(template.New("handler").Parse(`
Expand Down
16 changes: 14 additions & 2 deletions runtime/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// PopulateQueryParameters populates "values" into "msg".
// A value is ignored if its key starts with one of the elements in "filters".
// A value is ignored if its key starts with one of the elements in "filter".
func PopulateQueryParameters(msg proto.Message, values url.Values, filter *utilities.DoubleArray) error {
for key, values := range values {
fieldPath := strings.Split(key, ".")
Expand Down Expand Up @@ -44,7 +44,7 @@ func populateFieldValueFromPath(msg proto.Message, fieldPath []string, values []
if !isLast && m.Kind() != reflect.Struct {
return fmt.Errorf("non-aggregate type in the mid of path: %s", strings.Join(fieldPath, "."))
}
f := m.FieldByName(utilities.PascalFromSnake(fieldName))
f := fieldByProtoName(m, fieldName)
if !f.IsValid() {
glog.Warningf("field not found in %T: %s", msg, strings.Join(fieldPath, "."))
return nil
Expand Down Expand Up @@ -83,6 +83,18 @@ func populateFieldValueFromPath(msg proto.Message, fieldPath []string, values []
return populateField(m, values[0])
}

// fieldByProtoName looks up a field whose corresponding protobuf field name is "name".
// "m" must be a struct value. It returns zero reflect.Value if no such field found.
func fieldByProtoName(m reflect.Value, name string) reflect.Value {
props := proto.GetProperties(m.Type())
for _, p := range props.Prop {
if p.OrigName == name {
return m.FieldByName(p.Name)
}
}
return reflect.Value{}
}

func populateRepeatedField(f reflect.Value, values []string) error {
elemType := f.Type().Elem()
conv, ok := convFromType[elemType.Kind()]
Expand Down
82 changes: 0 additions & 82 deletions utilities/name.go

This file was deleted.

22 changes: 0 additions & 22 deletions utilities/name_test.go

This file was deleted.

0 comments on commit 97a6b0b

Please sign in to comment.