contrib icon indicating copy to clipboard operation
contrib copied to clipboard

entgrpc: support separate package

Open lesomnus opened this issue 9 months ago • 0 comments

These updates enable the output of generated code into another package where the pb codes (generated by protoc-gen-go and protoc-gen-go-grpc) exist.

I added the following two options:

  • package Import path of the package to be generated e.g. example.com/foo/bar
  • entity_package import path of the package generated by ent.

Assume pb package is example.com/foo.

Current implementation generates:

package foo

type UserService struct {
	client *ent.Client
	UnimplementedUserServiceServer
}

Note that UnimplementedUserServiceServer is generated by protoc-gen-go-grpc.

To separate the genreated code into example.com/foo/bar, add extra options to protoc:

WS_ROOT="/workspace/foo"
PROTO_ROOT="${WS_ROOT}/proto"
MODULE_NAME="example.com/foo"
protoc \
	-I="${PROTO_ROOT}" \
	--go_out="${WS_ROOT}" \
	--go_opt=module="${MODULE_NAME}" \
	--go-grpc_out="${WS_ROOT}" \
	--go-grpc_opt=module="${MODULE_NAME}" \
	--entgrpc_out="${WS_ROOT}/bar" \
	--entgrpc_opt=module="${MODULE_NAME}" \
	--entgrpc_opt=package="${MODULE_NAME}/bar" \
	--entgrpc_opt=schema_path="${WS_ROOT}/schema" \
	--entgrpc_opt=entity_package="${MODULE_NAME}/ent" \
	foo/user.proto

and following code is generated in the /workspace/foo/bar:

package bar

type UserService struct {
	client *ent.Client
	foo.UnimplementedUserServiceServer
}

lesomnus avatar May 10 '24 15:05 lesomnus