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

Remove uses of protoc-gen-go/generator #1260

Merged
merged 1 commit into from
Apr 30, 2020
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
2 changes: 1 addition & 1 deletion examples/internal/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ go_library(
"//examples/internal/proto/examplepb:go_default_library",
"//examples/internal/proto/sub:go_default_library",
"//examples/internal/proto/sub2:go_default_library",
"//internal/casing:go_default_library",
"//runtime:go_default_library",
"@com_github_golang_glog//:go_default_library",
"@com_github_golang_protobuf//proto:go_default_library",
"@com_github_golang_protobuf//protoc-gen-go/generator:go_default_library_gen",
"@com_github_rogpeppe_fastuuid//:go_default_library",
"@go_googleapis//google/rpc:errdetails_go_proto",
"@io_bazel_rules_go//proto/wkt:duration_go_proto",
Expand Down
4 changes: 2 additions & 2 deletions examples/internal/server/fieldmask_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"
"strings"

"github.com/golang/protobuf/protoc-gen-go/generator"
"github.com/grpc-ecosystem/grpc-gateway/internal/casing"
"google.golang.org/genproto/protobuf/field_mask"
)

Expand Down Expand Up @@ -40,7 +40,7 @@ func getField(obj interface{}, path string) (val reflect.Value) {
if v.Kind() == reflect.Ptr {
v = reflect.Indirect(v)
}
v = v.FieldByName(generator.CamelCase(s))
v = v.FieldByName(casing.Camel(s))
}

return v
Expand Down
8 changes: 8 additions & 0 deletions internal/casing/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["camel.go"],
importpath = "github.com/grpc-ecosystem/grpc-gateway/internal/casing",
visibility = ["//:__subpackages__"],
)
28 changes: 28 additions & 0 deletions internal/casing/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Copyright 2010 The Go Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

5 changes: 5 additions & 0 deletions internal/casing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Case conversion

This package contains a single function, copied from the
`github.com/golang/protobuf/protoc-gen-go/generator` package. That
modules LICENSE is referenced in its entirety in this package.
63 changes: 63 additions & 0 deletions internal/casing/camel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package casing

// Camel returns the CamelCased name.
//
// This was moved from the now deprecated github.com/golang/protobuf/protoc-gen-go/generator package
//
// If there is an interior underscore followed by a lower case letter,
// drop the underscore and convert the letter to upper case.
// There is a remote possibility of this rewrite causing a name collision,
// but it's so remote we're prepared to pretend it's nonexistent - since the
// C++ generator lowercases names, it's extremely unlikely to have two fields
// with different capitalizations.
// In short, _my_field_name_2 becomes XMyFieldName_2.
func Camel(s string) string {
if s == "" {
return ""
}
t := make([]byte, 0, 32)
i := 0
if s[0] == '_' {
// Need a capital letter; drop the '_'.
t = append(t, 'X')
i++
}
// Invariant: if the next letter is lower case, it must be converted
// to upper case.
// That is, we process a word at a time, where words are marked by _ or
// upper case letter. Digits are treated as words.
for ; i < len(s); i++ {
c := s[i]
if c == '_' && i+1 < len(s) && isASCIILower(s[i+1]) {
continue // Skip the underscore in s.
}
if isASCIIDigit(c) {
t = append(t, c)
continue
}
// Assume we have a letter now - if not, it's a bogus identifier.
// The next word is a sequence of characters that must start upper case.
if isASCIILower(c) {
c ^= ' ' // Make it a capital letter.
}
t = append(t, c) // Guaranteed not lower case.
// Accept lower case sequence that follows.
for i+1 < len(s) && isASCIILower(s[i+1]) {
i++
t = append(t, s[i])
}
}
return string(t)
}

// And now lots of helper functions.

// Is c an ASCII lower-case letter?
func isASCIILower(c byte) bool {
return 'a' <= c && c <= 'z'
}

// Is c an ASCII digit?
func isASCIIDigit(c byte) bool {
return '0' <= c && c <= '9'
}
2 changes: 1 addition & 1 deletion protoc-gen-grpc-gateway/descriptor/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ go_library(
],
importpath = "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor",
deps = [
"//internal/casing:go_default_library",
"//protoc-gen-grpc-gateway/httprule:go_default_library",
"@com_github_ghodss_yaml//:go_default_library",
"@com_github_golang_glog//:go_default_library",
"@com_github_golang_protobuf//jsonpb:go_default_library_gen",
"@com_github_golang_protobuf//proto:go_default_library",
"@com_github_golang_protobuf//protoc-gen-go/generator:go_default_library_gen",
"@go_googleapis//google/api:annotations_go_proto",
"@io_bazel_rules_go//proto/wkt:compiler_plugin_go_proto",
"@io_bazel_rules_go//proto/wkt:descriptor_go_proto",
Expand Down
10 changes: 5 additions & 5 deletions protoc-gen-grpc-gateway/descriptor/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/golang/protobuf/protoc-gen-go/descriptor"
gogen "github.com/golang/protobuf/protoc-gen-go/generator"
"github.com/grpc-ecosystem/grpc-gateway/internal/casing"
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/httprule"
)

Expand Down Expand Up @@ -316,7 +316,7 @@ func (p FieldPath) AssignableExpr(msgExpr string) string {
if c.Target.OneofIndex != nil {
index := c.Target.OneofIndex
msg := c.Target.Message
oneOfName := gogen.CamelCase(msg.GetOneofDecl()[*index].GetName())
oneOfName := casing.Camel(msg.GetOneofDecl()[*index].GetName())
oneofFieldName := msg.GetName() + "_" + c.AssignableExpr()

components = components + "." + oneOfName
Expand Down Expand Up @@ -352,15 +352,15 @@ type FieldPathComponent struct {

// AssignableExpr returns an assignable expression in go for this field.
func (c FieldPathComponent) AssignableExpr() string {
return gogen.CamelCase(c.Name)
return casing.Camel(c.Name)
}

// 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))
return fmt.Sprintf("Get%s()", casing.Camel(c.Name))
}
return gogen.CamelCase(c.Name)
return casing.Camel(c.Name)
}

var (
Expand Down
2 changes: 1 addition & 1 deletion protoc-gen-grpc-gateway/internal/gengateway/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ go_library(
],
importpath = "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/internal/gengateway",
deps = [
"//internal/casing:go_default_library",
"//protoc-gen-grpc-gateway/descriptor:go_default_library",
"//protoc-gen-grpc-gateway/generator:go_default_library",
"//utilities:go_default_library",
"@com_github_golang_glog//:go_default_library",
"@com_github_golang_protobuf//proto:go_default_library",
"@com_github_golang_protobuf//protoc-gen-go/generator:go_default_library_gen",
"@io_bazel_rules_go//proto/wkt:compiler_plugin_go_proto",
],
)
Expand Down
12 changes: 6 additions & 6 deletions protoc-gen-grpc-gateway/internal/gengateway/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"text/template"

"github.com/golang/glog"
generator2 "github.com/golang/protobuf/protoc-gen-go/generator"
"github.com/grpc-ecosystem/grpc-gateway/internal/casing"
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
"github.com/grpc-ecosystem/grpc-gateway/utilities"
)
Expand Down Expand Up @@ -38,7 +38,7 @@ func (b binding) GetBodyFieldPath() string {
// GetBodyFieldPath returns the binding body's struct field name.
func (b binding) GetBodyFieldStructName() (string, error) {
if b.Body != nil && len(b.Body.FieldPath) != 0 {
return generator2.CamelCase(b.Body.FieldPath.String()), nil
return casing.Camel(b.Body.FieldPath.String()), nil
}
return "", errors.New("No body field found")
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func (b binding) FieldMaskField() string {
}
}
if fieldMaskField != nil {
return generator2.CamelCase(fieldMaskField.GetName())
return casing.Camel(fieldMaskField.GetName())
}
return ""
}
Expand Down Expand Up @@ -156,16 +156,16 @@ func applyTemplate(p param, reg *descriptor.Registry) (string, error) {
var targetServices []*descriptor.Service

for _, msg := range p.Messages {
msgName := generator2.CamelCase(*msg.Name)
msgName := casing.Camel(*msg.Name)
msg.Name = &msgName
}
for _, svc := range p.Services {
var methodWithBindingsSeen bool
svcName := generator2.CamelCase(*svc.Name)
svcName := casing.Camel(*svc.Name)
svc.Name = &svcName
for _, meth := range svc.Methods {
glog.V(2).Infof("Processing %s.%s", svc.GetName(), meth.GetName())
methName := generator2.CamelCase(*meth.Name)
methName := casing.Camel(*meth.Name)
meth.Name = &methName
for _, b := range meth.Bindings {
methodWithBindingsSeen = true
Expand Down
1 change: 1 addition & 0 deletions protoc-gen-swagger/genswagger/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go_library(
importpath = "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger/genswagger",
deps = [
"//internal:go_default_library",
"//internal/casing:go_default_library",
"//protoc-gen-grpc-gateway/descriptor:go_default_library",
"//protoc-gen-grpc-gateway/generator:go_default_library",
"//protoc-gen-swagger/options:go_default_library",
Expand Down
65 changes: 2 additions & 63 deletions protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/golang/protobuf/proto"
pbdescriptor "github.com/golang/protobuf/protoc-gen-go/descriptor"
structpb "github.com/golang/protobuf/ptypes/struct"
"github.com/grpc-ecosystem/grpc-gateway/internal/casing"
"github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
swagger_options "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger/options"
)
Expand Down Expand Up @@ -1968,7 +1969,7 @@ func lowerCamelCase(fieldName string, fields []*descriptor.Field, msgs []*descri
}

func doCamelCase(input string) string {
parameterString := camelCase(input)
parameterString := casing.Camel(input)
builder := &strings.Builder{}
builder.WriteString(strings.ToLower(string(parameterString[0])))
builder.WriteString(parameterString[1:])
Expand All @@ -1987,65 +1988,3 @@ func getReservedJSONName(fieldName string, messageNameToFieldsToJSONName map[str
fieldNames := strings.Split(fieldName, ".")
return getReservedJSONName(strings.Join(fieldNames[1:], "."), messageNameToFieldsToJSONName, fieldNameToType)
}

// CamelCase returns the CamelCased name.
//
// This was moved from the now deprecated github.com/golang/protobuf/protoc-gen-go/generator package
//
// If there is an interior underscore followed by a lower case letter,
// drop the underscore and convert the letter to upper case.
// There is a remote possibility of this rewrite causing a name collision,
// but it's so remote we're prepared to pretend it's nonexistent - since the
// C++ generator lowercases names, it's extremely unlikely to have two fields
// with different capitalizations.
// In short, _my_field_name_2 becomes XMyFieldName_2.
func camelCase(s string) string {
if s == "" {
return ""
}
t := make([]byte, 0, 32)
i := 0
if s[0] == '_' {
// Need a capital letter; drop the '_'.
t = append(t, 'X')
i++
}
// Invariant: if the next letter is lower case, it must be converted
// to upper case.
// That is, we process a word at a time, where words are marked by _ or
// upper case letter. Digits are treated as words.
for ; i < len(s); i++ {
c := s[i]
if c == '_' && i+1 < len(s) && isASCIILower(s[i+1]) {
continue // Skip the underscore in s.
}
if isASCIIDigit(c) {
t = append(t, c)
continue
}
// Assume we have a letter now - if not, it's a bogus identifier.
// The next word is a sequence of characters that must start upper case.
if isASCIILower(c) {
c ^= ' ' // Make it a capital letter.
}
t = append(t, c) // Guaranteed not lower case.
// Accept lower case sequence that follows.
for i+1 < len(s) && isASCIILower(s[i+1]) {
i++
t = append(t, s[i])
}
}
return string(t)
}

// And now lots of helper functions.

// Is c an ASCII lower-case letter?
func isASCIILower(c byte) bool {
return 'a' <= c && c <= 'z'
}

// Is c an ASCII digit?
func isASCIIDigit(c byte) bool {
return '0' <= c && c <= '9'
}