Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit 0900613

Browse files
committed
do not use protobuf v1, update protoc-gen-php-grpc to v2
Signed-off-by: Valery Piashchynski <[email protected]>
1 parent 70ad5ff commit 0900613

File tree

6 files changed

+73
-43
lines changed

6 files changed

+73
-43
lines changed

cmd/protoc-gen-php-grpc/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import (
2727
"io/ioutil"
2828
"os"
2929

30-
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
3130
"github.com/spiral/php-grpc/cmd/protoc-gen-php-grpc/php"
3231
"google.golang.org/protobuf/proto"
32+
plugin "google.golang.org/protobuf/types/pluginpb"
3333
)
3434

3535
func main() {

cmd/protoc-gen-php-grpc/php/generate.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
package php
2424

2525
import (
26-
"github.com/golang/protobuf/protoc-gen-go/descriptor"
27-
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
26+
desc "google.golang.org/protobuf/types/descriptorpb"
27+
plugin "google.golang.org/protobuf/types/pluginpb"
2828
)
2929

3030
// Generate generates needed service classes
@@ -42,8 +42,8 @@ func Generate(req *plugin.CodeGeneratorRequest) *plugin.CodeGeneratorResponse {
4242

4343
func generate(
4444
req *plugin.CodeGeneratorRequest,
45-
file *descriptor.FileDescriptorProto,
46-
service *descriptor.ServiceDescriptorProto,
45+
file *desc.FileDescriptorProto,
46+
service *desc.ServiceDescriptorProto,
4747
) *plugin.CodeGeneratorResponse_File {
4848
return &plugin.CodeGeneratorResponse_File{
4949
Name: str(filename(file, service.Name)),

cmd/protoc-gen-php-grpc/php/keywords.go

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ package php
2525
import (
2626
"bytes"
2727
"strings"
28-
29-
"github.com/c9s/inflect"
28+
"unicode"
3029
)
3130

3231
// @see https://github.com/protocolbuffers/protobuf/blob/master/php/ext/google/protobuf/protobuf.c#L168
32+
// immutable
3333
var reservedKeywords = []string{ //nolint:gochecknoglobals
3434
"abstract", "and", "array", "as", "break",
3535
"callable", "case", "catch", "class", "clone",
@@ -77,9 +77,9 @@ func namespace(pkg *string, sep string) string {
7777

7878
// create php identifier for class or message
7979
func identifier(name string, suffix string) string {
80-
name = inflect.Camelize(name)
80+
name = Camelize(name)
8181
if suffix != "" {
82-
return name + inflect.Camelize(suffix)
82+
return name + Camelize(suffix)
8383
}
8484

8585
return name
@@ -95,3 +95,46 @@ func resolveReserved(identifier string, pkg string) string {
9595

9696
return identifier
9797
}
98+
99+
// Camelize "dino_party" -> "DinoParty"
100+
func Camelize(word string) string {
101+
words := splitAtCaseChangeWithTitlecase(word)
102+
return strings.Join(words, "")
103+
}
104+
105+
func splitAtCaseChangeWithTitlecase(s string) []string {
106+
words := make([]string, 0)
107+
word := make([]rune, 0)
108+
for _, c := range s {
109+
spacer := isSpacerChar(c)
110+
if len(word) > 0 {
111+
if unicode.IsUpper(c) || spacer {
112+
words = append(words, string(word))
113+
word = make([]rune, 0)
114+
}
115+
}
116+
if !spacer {
117+
if len(word) > 0 {
118+
word = append(word, unicode.ToLower(c))
119+
} else {
120+
word = append(word, unicode.ToUpper(c))
121+
}
122+
}
123+
}
124+
words = append(words, string(word))
125+
return words
126+
}
127+
128+
func isSpacerChar(c rune) bool {
129+
switch {
130+
case c == rune("_"[0]):
131+
return true
132+
case c == rune(" "[0]):
133+
return true
134+
case c == rune(":"[0]):
135+
return true
136+
case c == rune("-"[0]):
137+
return true
138+
}
139+
return false
140+
}

cmd/protoc-gen-php-grpc/php/ns.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"fmt"
66
"strings"
77

8-
"github.com/golang/protobuf/protoc-gen-go/descriptor"
9-
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
8+
desc "google.golang.org/protobuf/types/descriptorpb"
9+
plugin "google.golang.org/protobuf/types/pluginpb"
1010
)
1111

1212
// manages internal name representation of the package
@@ -22,11 +22,7 @@ type ns struct {
2222
}
2323

2424
// newNamespace creates new work namespace.
25-
func newNamespace(
26-
req *plugin.CodeGeneratorRequest,
27-
file *descriptor.FileDescriptorProto,
28-
service *descriptor.ServiceDescriptorProto,
29-
) *ns {
25+
func newNamespace(req *plugin.CodeGeneratorRequest, file *desc.FileDescriptorProto, service *desc.ServiceDescriptorProto) *ns {
3026
ns := &ns{
3127
Package: *file.Package,
3228
Namespace: namespace(file.Package, "\\"),
@@ -37,9 +33,9 @@ func newNamespace(
3733
ns.Namespace = *file.Options.PhpNamespace
3834
}
3935

40-
for _, m := range service.Method {
41-
ns.importMessage(req, m.InputType)
42-
ns.importMessage(req, m.OutputType)
36+
for k := range service.Method {
37+
ns.importMessage(req, service.Method[k].InputType)
38+
ns.importMessage(req, service.Method[k].OutputType)
4339
}
4440

4541
return ns

cmd/protoc-gen-php-grpc/php/template.go

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import (
2828
"strings"
2929
"text/template"
3030

31-
"github.com/golang/protobuf/protoc-gen-go/descriptor"
32-
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
31+
desc "google.golang.org/protobuf/types/descriptorpb"
32+
plugin "google.golang.org/protobuf/types/pluginpb"
3333
)
3434

3535
const phpBody = `<?php
@@ -61,21 +61,8 @@ interface {{ .Service.Name | interface }} extends GRPC\ServiceInterface
6161
}
6262
`
6363

64-
var tpl *template.Template //nolint:gochecknoglobals
65-
66-
func init() {
67-
tpl = template.Must(template.New("phpBody").Funcs(template.FuncMap{
68-
"interface": func(name *string) string {
69-
return identifier(*name, "interface")
70-
},
71-
"name": func(ns *ns, name *string) string {
72-
return ns.resolve(name)
73-
},
74-
}).Parse(phpBody))
75-
}
76-
7764
// generate php filename
78-
func filename(file *descriptor.FileDescriptorProto, name *string) string {
65+
func filename(file *desc.FileDescriptorProto, name *string) string {
7966
ns := namespace(file.Package, "/")
8067
if file.Options != nil && file.Options.PhpNamespace != nil {
8168
ns = strings.ReplaceAll(*file.Options.PhpNamespace, `\`, `/`)
@@ -85,23 +72,28 @@ func filename(file *descriptor.FileDescriptorProto, name *string) string {
8572
}
8673

8774
// generate php file body
88-
func body(
89-
req *plugin.CodeGeneratorRequest,
90-
file *descriptor.FileDescriptorProto,
91-
service *descriptor.ServiceDescriptorProto,
92-
) string {
75+
func body(req *plugin.CodeGeneratorRequest, file *desc.FileDescriptorProto, service *desc.ServiceDescriptorProto) string {
9376
out := bytes.NewBuffer(nil)
9477

9578
data := struct {
9679
Namespace *ns
97-
File *descriptor.FileDescriptorProto
98-
Service *descriptor.ServiceDescriptorProto
80+
File *desc.FileDescriptorProto
81+
Service *desc.ServiceDescriptorProto
9982
}{
10083
Namespace: newNamespace(req, file, service),
10184
File: file,
10285
Service: service,
10386
}
10487

88+
tpl := template.Must(template.New("phpBody").Funcs(template.FuncMap{
89+
"interface": func(name *string) string {
90+
return identifier(*name, "interface")
91+
},
92+
"name": func(ns *ns, name *string) string {
93+
return ns.resolve(name)
94+
},
95+
}).Parse(phpBody))
96+
10597
err := tpl.Execute(out, data)
10698
if err != nil {
10799
panic(err)

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.17
44

55
require (
66
github.com/buger/goterm v1.0.1
7-
github.com/c9s/inflect v0.0.0-20130402162822-006c50878f3f
87
github.com/emicklei/proto v1.9.1
98
github.com/golang/protobuf v1.5.2
109
github.com/prometheus/client_golang v1.11.0

0 commit comments

Comments
 (0)