As Ent and Protobuf schemas are not identical, we must supply some annotations on our schema to help
entproto figure out exactly how to generate Protobuf definitions (called "Messages" in protobuf terminology).
The first thing we need to do is to add an
entproto.Message() annotation. This is our opt-in to Protobuf schema generation, we don't necessarily want to generate proto messages or gRPC service definitions from all of our schema entities, and this annotation gives us that control. To add it, append to
Next, we need to annotate each field and assign it a field number. Recall that when defining a protobuf message type, each field must be assigned a unique number. To do that, we add an
entproto.Field annotation on each field. Update the
Notice that we did not start our field numbers from 1, this is because
ent implicitly creates the
ID field for the entity, and that field is automatically assigned the number 1. We can now generate our protobuf message type definitions. To do that, we will add to
go:generate directive that invokes the
entproto command-line tool. It should now look like this:
Let's re-generate our code:
Observe that a new directory was created which will contain all protobuf related generated code:
ent/proto. It now contains:
Two files were created. Let's look at their contents:
Nice! A new
.proto file containing a message type definition that maps to our
User schema was created!
generate.go file was created with an invocation to
protoc, the protobuf code generator instructing it how to generate Go code from our
.proto file. For this command to work, we must first install
protoc as well as 3 protobuf plugins:
protoc-gen-go (which generates Go Protobuf structs),
protoc-gen-go-grpc (which generates Go gRPC service interfaces and clients), and
protoc-gen-entgrpc (which generates an implementation of the service interface). If you do not have these installed, please follow these directions:
protoc-gen-entgrpc, run:go get -u entgo.io/contrib/entproto/cmd/protoc-gen-entgrpc
After installing these dependencies, we can re-run code-generation:
Observe that a new file named
ent/proto/entpb/entpb.pb.go was created which contains the generated Go structs for our entities.
Let's write a test that uses it to make sure everything is wired correctly. Create a new file named
pb_test.go and write:
To run it:
Hooray! The test passes. We have successfully generated working Go Protobuf structs from our Ent schema. Next, let's see how to automatically generate a working CRUD gRPC server from our schema.