Configuring Service Method Generation

By default, entproto will generate a number of service methods for an ent.Schema annotated with ent.Service(). Method generation can be customized by including the argument entproto.Methods() in the entproto.Service() annotation. entproto.Methods() accepts bit flags to determine what service methods should be generated. The flags include:

  1. // Generates a Create gRPC service method for the entproto.Service.
  2. entproto.MethodCreate
  3. // Generates a Get gRPC service method for the entproto.Service.
  4. entproto.MethodGet
  5. // Generates an Update gRPC service method for the entproto.Service.
  6. entproto.MethodUpdate
  7. // Generates a Delete gRPC service method for the entproto.Service.
  8. entproto.MethodDelete
  9. // Generates a List gRPC service method for the entproto.Service.
  10. entproto.MethodList
  11. // Generates a Batch Create gRPC service method for the entproto.Service.
  12. entproto.MethodBatchCreate
  13. // Generates all service methods for the entproto.Service.
  14. // This is the same behavior as not including entproto.Methods.
  15. entproto.MethodAll

To generate a service with multiple methods, bitwise OR the flags.

To see this in action, we can modify our ent schema. Let’s say we wanted to prevent our gRPC client from mutating entries. We can accomplish this by modifying ent/schema/user.go:

ent/schema/user.go

  1. func (User) Annotations() []schema.Annotation {
  2. return []schema.Annotation{
  3. entproto.Message(),
  4. entproto.Service(
  5. entproto.Methods(entproto.MethodCreate | entproto.MethodGet | entproto.MethodList | entproto.MethodBatchCreate),
  6. ),
  7. }
  8. }

Re-running go generate ./... will give us the following service definition in entpb.proto:

ent/proto/entpb/entpb.proto

  1. service UserService {
  2. rpc Create ( CreateUserRequest ) returns ( User );
  3. rpc Get ( GetUserRequest ) returns ( User );
  4. rpc List ( ListUserRequest ) returns ( ListUserResponse );
  5. rpc BatchCreate ( BatchCreateUsersRequest ) returns ( BatchCreateUsersResponse );
  6. }

Notice that the service no longer includes Update and Delete methods. Perfect!