Skip to content

Commit 41e5029

Browse files
committed
fix: add support for nested message names
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
1 parent 384246b commit 41e5029

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

  • protoc-gen-twirp_php/internal/php

protoc-gen-twirp_php/internal/php/func.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package php
22

33
import (
4+
"slices"
45
"strings"
56

67
"google.golang.org/protobuf/compiler/protogen"
78
"google.golang.org/protobuf/reflect/protoreflect"
89
"google.golang.org/protobuf/types/descriptorpb"
910
)
1011

12+
// Code in this file is based on the original protoc compiler code for PHP:
13+
// https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/compiler/php/names.cc
14+
1115
// Reserved PHP keywords that must be prefixed with something.
1216
var reservedNames = []string{
1317
"abstract", "and", "array", "as", "break",
@@ -43,16 +47,7 @@ func classNamePrefix(className string, file protoreflect.FileDescriptor) string
4347
}
4448

4549
// Check if the class name matches a reserved name
46-
isReserved := false
47-
for _, name := range reservedNames {
48-
if name == strings.ToLower(className) {
49-
isReserved = true
50-
51-
break
52-
}
53-
}
54-
55-
if isReserved {
50+
if slices.Contains(reservedNames, strings.ToLower(className)) {
5651
// Google internal classes receive a different prefix
5752
if file.Package() == "google.protobuf" {
5853
return "GPB"
@@ -85,12 +80,12 @@ func namespace(file protoreflect.FileDescriptor) string {
8580

8681
// Path guesses the path of the file based on the (internally calculated) namespace.
8782
func Path(file *protogen.File) string {
88-
return strings.Replace(Namespace(file), "\\", "/", -1)
83+
return strings.ReplaceAll(Namespace(file), "\\", "/")
8984
}
9085

9186
// PathFromNamespace guesses the path of the file based on the namespace.
9287
func PathFromNamespace(ns string) string {
93-
return strings.Replace(ns, "\\", "/", -1)
88+
return strings.ReplaceAll(ns, "\\", "/")
9489
}
9590

9691
// Name generates a name from a proto reference.
@@ -130,8 +125,12 @@ func ServiceName(file *protogen.File, svc *protogen.Service) string {
130125

131126
// MessageName transforms a message name into a PHP compatible one.
132127
func MessageName(file *protogen.File, message *protogen.Message) string {
133-
className := string(message.Desc.Name())
134128
parentFile := message.Desc.ParentFile()
129+
className := classNamePrefix(string(message.Desc.Name()), parentFile) + string(message.Desc.Name())
130+
131+
for parent, ok := message.Desc.Parent().(protoreflect.MessageDescriptor); ok && parent != nil; parent, ok = parent.Parent().(protoreflect.MessageDescriptor) {
132+
className = classNamePrefix(string(parent.Name()), parentFile) + string(parent.Name()) + "\\" + className
133+
}
135134

136-
return "\\" + namespacedName(classNamePrefix(className, parentFile)+className, parentFile)
135+
return "\\" + namespacedName(className, parentFile)
137136
}

0 commit comments

Comments
 (0)