Pondering about keeping model layer and protobuf messages in sync.

Daisuke Maki
Jul 28, 2017 · 2 min read

So you have a service that you would like to expose using gRPC. You need to write your .proto file and define message types and service/rpc types, etc, etc…

Where I get hung up is connecting this service layer with the model layer of your application.

So suppose you are writing a Go application that manages “Book” entries. It’s fairly straight forward to write a .proto file to do CRUD:

syntax = "proto3";package rpc;message Book {
string id = 1;
string title = 2;
string author = 3;
}
service BookSvc {
rpc Create(CreateBookReq) returns (CreateBookResp);
rpc Lookup(LookupBookReq) returns (LookupBookResp);
rpc Update(UpdateBookReq) returns (UpdateBookResp);
rpc Delete(DeleteBookReq) returns (DeleteBookResp);
}
// ... definition of requests/responses follow...

Then you run protoc --go_out=plugins=grpc:rpc *.proto or whatever, and voila, you just generated your server/client stubs for this service.

Now, that’s fine and dandy, but I’m sure you will have structures within your code that describes a Book , that is not necessarily tied to the protocol buffer layer. Maybe something like this:

package model // whatever, just putting this to make it explicit for
// this example.
type Book struct {
ID string `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
}

Initially writing the .proto definitions along with the above Go struct definition manually will feel just fine. But as you go on writing more services, rpcs, and more models, wouldn’t it become harder and harder to maintain the two in sync?

I can almost feel that for majority of the cases where a simple CRUD is sufficient, it may just suit to generate the basic .proto files from the Go struct definitions… But then again, I’m too used to parsing Go AST to generate something else, and I might just be blind to other, better ways.

So what’s the best way? Do you even keep these things in sync automatically? Or do you just do it by hand?

Written by

Go/perl hacker; author of peco; works @ HDE Inc; mastermind of builderscon; Proud father of three boys;

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade