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

chore: strip extra slashes when there is no package defined in the protofile #134

Merged
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
31 changes: 27 additions & 4 deletions protoc_plugins/protoc-gen-php-grpc/php/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ use Spiral\RoadRunner\GRPC;
interface {{ .Service.Name | interface }} extends GRPC\ServiceInterface
{
// GRPC specific service name.
public const NAME = "{{ .File.Package }}.{{ .Service.Name }}";{{ "\n" }}
public const NAME = "{{ resolve_name_const .File.Package .Service.Name }}";{{ "\n" }}
{{- range $m := .Service.Method}}
{{- $inName := name $ns $m.InputType }}
{{- $outName := name $ns $m.OutputType }}
/**
* @param GRPC\ContextInterface $ctx
* @param {{ name $ns $m.InputType }} $in
* @return {{ name $ns $m.OutputType }}
* @param {{ strip_slashes $inName }} $in
* @return {{ strip_slashes $outName }}
*
* @throws GRPC\Exception\InvokeException
*/
public function {{ $m.Name }}(GRPC\ContextInterface $ctx, {{ name $ns $m.InputType }} $in): {{ name $ns $m.OutputType }};
public function {{ $m.Name }}(GRPC\ContextInterface $ctx, {{ strip_slashes $inName }} $in): {{ strip_slashes $outName }};
{{end -}}
}
`
Expand Down Expand Up @@ -89,6 +91,18 @@ func body(req *plugin.CodeGeneratorRequest, file *desc.FileDescriptorProto, serv
"name": func(ns *ns, name *string) string {
return ns.resolve(name)
},
"strip_slashes": func(name string) string {
return strings.ReplaceAll(name, "\\\\", "\\")
},
"resolve_name_const": func(packagePath, serviceName *string) string {

if defaultOrVal(packagePath) == "" {
return defaultOrVal(serviceName)
}

return *packagePath + "." + defaultOrVal(serviceName)

},
}).Parse(phpBody))

err := tpl.Execute(out, data)
Expand All @@ -98,3 +112,12 @@ func body(req *plugin.CodeGeneratorRequest, file *desc.FileDescriptorProto, serv

return out.String()
}

func defaultOrVal[T any](val *T) T {
if val != nil {
return *val
}

var def T
return def
}
22 changes: 17 additions & 5 deletions protoc_plugins/protoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func protoc(t *testing.T, args []string) {
}

func Test_Simple(t *testing.T) {
workdir, _ := os.Getwd()
workdir, err := os.Getwd()
require.NoError(t, err)
tmpdir, err := os.MkdirTemp("", "proto-test")
require.NoError(t, err)

Expand All @@ -61,7 +62,8 @@ func Test_Simple(t *testing.T) {
}

func Test_PhpNamespaceOption(t *testing.T) {
workdir, _ := os.Getwd()
workdir, err := os.Getwd()
require.NoError(t, err)
tmpdir, err := os.MkdirTemp("", "proto-test")
require.NoError(t, err)

Expand All @@ -87,7 +89,8 @@ func Test_PhpNamespaceOption(t *testing.T) {
}

func Test_UseImportedMessage(t *testing.T) {
workdir, _ := os.Getwd()
workdir, err := os.Getwd()
require.NoError(t, err)
tmpdir, err := os.MkdirTemp("", "proto-test")
require.NoError(t, err)

Expand All @@ -113,7 +116,8 @@ func Test_UseImportedMessage(t *testing.T) {
}

func Test_PhpNamespaceOptionInUse(t *testing.T) {
workdir, _ := os.Getwd()
workdir, err := os.Getwd()
require.NoError(t, err)
tmpdir, err := os.MkdirTemp("", "proto-test")
require.NoError(t, err)

Expand All @@ -139,7 +143,8 @@ func Test_PhpNamespaceOptionInUse(t *testing.T) {
}

func Test_UseOfGoogleEmptyMessage(t *testing.T) {
workdir, _ := os.Getwd()
workdir, err := os.Getwd()
require.NoError(t, err)
tmpdir, err := os.MkdirTemp("", "proto-test")
require.NoError(t, err)

Expand All @@ -166,6 +171,8 @@ func Test_UseOfGoogleEmptyMessage(t *testing.T) {
}

func Test_NoPackage(t *testing.T) {
workdir, err := os.Getwd()
require.NoError(t, err)
tmpdir, err := os.MkdirTemp("", "proto-test")
require.NoError(t, err)

Expand All @@ -182,6 +189,11 @@ func Test_NoPackage(t *testing.T) {
}
protoc(t, args)

assertEqualFiles(
t,
workdir+"/testdata/NoPackage/Test/CustomImport/ServiceInterface.php",
tmpdir+"/Test/CustomImport/ServiceInterface.php",
)
assert.NoError(t, os.RemoveAll("plugin"))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
# Generated by the protocol buffer compiler (roadrunner-server/grpc). DO NOT EDIT!
# source: NoPackage/nopackage.proto

namespace Test\CustomImport;

use Spiral\RoadRunner\GRPC;

interface ServiceInterface extends GRPC\ServiceInterface
{
// GRPC specific service name.
public const NAME = "Service";

/**
* @param GRPC\ContextInterface $ctx
* @param \Message $in
* @return \Message
*
* @throws GRPC\Exception\InvokeException
*/
public function SimpleMethod(GRPC\ContextInterface $ctx, \Message $in): \Message;
}
Loading