Skip to content
Open
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
13 changes: 11 additions & 2 deletions entproto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,23 @@ func (User) Annotations() []schema.Annotation {
By default the proto package name for the generated files will be `entpb` but it can be specified using a functional option:

```go

func (MessageWithPackageName) Annotations() []schema.Annotation {
return []schema.Annotation{entproto.Message(
entproto.PackageName("io.entgo.apps.todo"),
)}
}
```

In case you want to add a custom PHP namespace, you can also add the following option:

```go
func (MessageWithPackageName) Annotations() []schema.Annotation {
return []schema.Annotation{entproto.Message(
entproto.PhpNamespace("My\\Company\\Todo"),
)}
}
```

Per the protobuf style guide:

> 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.
Expand Down Expand Up @@ -331,7 +340,7 @@ message User {
Field type mappings:

| Ent Type | Proto Type | More considerations |
|----------------|---------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| -------------- | ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| TypeBool | bool | |
| TypeTime | google.protobuf.Timestamp | |
| TypeJSON\[[]T] | repeated T | T must be one of: `string`, `int32`, `int64`, `uint32`, `uint64` |
Expand Down
22 changes: 21 additions & 1 deletion entproto/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,35 @@ func (a *Adapter) parse() error {

if _, ok := protoPackages[protoPkg]; !ok {
goPkg := a.goPackageName(protoPkg)

var phpNamespace *string

msgAnnot, err := extractMessageAnnotation(genType)
if err == nil && msgAnnot.PhpNamespace != "" {
phpNamespace = &msgAnnot.PhpNamespace
}

protoPackages[protoPkg] = &descriptorpb.FileDescriptorProto{
Name: relFileName(protoPkg),
Package: &protoPkg,
Syntax: strptr("proto3"),
Options: &descriptorpb.FileOptions{
GoPackage: &goPkg,
GoPackage: &goPkg,
PhpNamespace: phpNamespace,
},
}
}

// Extract and set PhpNamespace if provided (even for existing packages)
msgAnnot, err := extractMessageAnnotation(genType)
if err == nil && msgAnnot.PhpNamespace != "" {
fd := protoPackages[protoPkg]
if fd.Options == nil {
fd.Options = &descriptorpb.FileOptions{}
}
fd.Options.PhpNamespace = &msgAnnot.PhpNamespace
}

fd := protoPackages[protoPkg]
fd.MessageType = append(fd.MessageType, messageDescriptor)
a.schemaProtoFiles[genType.Name] = *fd.Name
Expand Down
6 changes: 6 additions & 0 deletions entproto/internal/entprototest/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,9 @@ func (suite *AdapterTestSuite) TestOptionals() {
suite.Require().EqualValues(descriptorpb.FieldDescriptorProto_TYPE_MESSAGE, bytesField.GetType())
suite.Require().EqualValues("BytesValue", uuidField.GetMessageType().GetName())
}

func (suite *AdapterTestSuite) TestMessageWithPhpNamespace() {
fd, err := suite.adapter.GetFileDescriptor("MessageWithPhpNamespace")
suite.Require().NoError(err)
suite.Equal("My\\Company\\Todo", fd.GetFileOptions().GetPhpNamespace())
}
163 changes: 153 additions & 10 deletions entproto/internal/entprototest/ent/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions entproto/internal/entprototest/ent/ent.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions entproto/internal/entprototest/ent/hook/hook.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading