Skip to content

Conversation

@lesomnus
Copy link

@lesomnus lesomnus commented May 10, 2024

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
}

@a8m a8m requested a review from rotemtam May 28, 2024 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant