Skip to content

Commit 67783dd

Browse files
committed
entproto: Added PhpNamespace() message option
Signed-off-by: Vincent Composieux <[email protected]>
1 parent 8a7f182 commit 67783dd

19 files changed

+1894
-17
lines changed

entproto/README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,23 @@ func (User) Annotations() []schema.Annotation {
174174
By default the proto package name for the generated files will be `entpb` but it can be specified using a functional option:
175175

176176
```go
177-
178177
func (MessageWithPackageName) Annotations() []schema.Annotation {
179178
return []schema.Annotation{entproto.Message(
180179
entproto.PackageName("io.entgo.apps.todo"),
181180
)}
182181
}
183182
```
184183

184+
In case you want to add a custom PHP namespace, you can also add the following option:
185+
186+
```go
187+
func (MessageWithPackageName) Annotations() []schema.Annotation {
188+
return []schema.Annotation{entproto.Message(
189+
entproto.PhpNamespace("My\\Company\\Todo"),
190+
)}
191+
}
192+
```
193+
185194
Per the protobuf style guide:
186195

187196
> Package name should be in lowercase, and should correspond to the directory hierarchy. e.g., if a file is in my/package/, then the package name should be my.package.
@@ -331,7 +340,7 @@ message User {
331340
Field type mappings:
332341

333342
| Ent Type | Proto Type | More considerations |
334-
|----------------|---------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
343+
| -------------- | ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
335344
| TypeBool | bool | |
336345
| TypeTime | google.protobuf.Timestamp | |
337346
| TypeJSON\[[]T] | repeated T | T must be one of: `string`, `int32`, `int64`, `uint32`, `uint64` |

entproto/adapter.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,26 @@ func (a *Adapter) parse() error {
121121

122122
if _, ok := protoPackages[protoPkg]; !ok {
123123
goPkg := a.goPackageName(protoPkg)
124+
fileOpts := &descriptorpb.FileOptions{
125+
GoPackage: &goPkg,
126+
}
127+
124128
protoPackages[protoPkg] = &descriptorpb.FileDescriptorProto{
125129
Name: relFileName(protoPkg),
126130
Package: &protoPkg,
127131
Syntax: strptr("proto3"),
128-
Options: &descriptorpb.FileOptions{
129-
GoPackage: &goPkg,
130-
},
132+
Options: fileOpts,
133+
}
134+
}
135+
136+
// Extract and set PhpNamespace if provided (even for existing packages)
137+
msgAnnot, err := extractMessageAnnotation(genType)
138+
if err == nil && msgAnnot.PhpNamespace != "" {
139+
fd := protoPackages[protoPkg]
140+
if fd.Options == nil {
141+
fd.Options = &descriptorpb.FileOptions{}
131142
}
143+
fd.Options.PhpNamespace = &msgAnnot.PhpNamespace
132144
}
133145
fd := protoPackages[protoPkg]
134146
fd.MessageType = append(fd.MessageType, messageDescriptor)

entproto/internal/entprototest/adapter_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,9 @@ func (suite *AdapterTestSuite) TestOptionals() {
290290
suite.Require().EqualValues(descriptorpb.FieldDescriptorProto_TYPE_MESSAGE, bytesField.GetType())
291291
suite.Require().EqualValues("BytesValue", uuidField.GetMessageType().GetName())
292292
}
293+
294+
func (suite *AdapterTestSuite) TestMessageWithPhpNamespace() {
295+
fd, err := suite.adapter.GetFileDescriptor("MessageWithPhpNamespace")
296+
suite.Require().NoError(err)
297+
suite.Equal("My\\Company\\Todo", fd.GetFileOptions().GetPhpNamespace())
298+
}

entproto/internal/entprototest/ent/client.go

Lines changed: 153 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

entproto/internal/entprototest/ent/ent.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

entproto/internal/entprototest/ent/hook/hook.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)