grpc and golang struct tags

149 views
Skip to first unread message

Sankar

unread,
Nov 27, 2021, 10:53:06 AM11/27/21
to golang-nuts
Hi

I have a database that has a column `2xxCode`. This database is part of a legacy system and I cannot make changes to this, as there are multiple applications that depend on this, and some of the applications are unchangeable.

Now I want to create a new gRPC server in golang that has to interact with the data stored in this database.

Since golang variable names cannot begin with a number, I need create a `.proto` file which should generate a golang struct member like:

type mystruct {
    TwoxxCode `db:"2xxCode"`
}

from my .proto file. But I could not find out how to generate a golang struct from a .proto file, with adding custom tags to the struct. On searching, I found https://github.com/golang/protobuf/issues/52 and the second comment on the issue seems very strongly worded as if this would never happen in protobuf. Also, the bug seems open for a long time.

How have others solved this problem to add custom tags to proto generated golang structs ? Are there any best-known-methods for this ? There were a couple of scripts mentioned in the comments, but I am not sure what to pick. Any recommendations ?

Thanks.

Hein Meling

unread,
Nov 30, 2021, 12:40:21 AM11/30/21
to golang-nuts
I’ve used protopatch successfully to solve similar problems.

Cheers,
:) Hein

David Finkel

unread,
Nov 30, 2021, 11:56:30 AM11/30/21
to Sankar, golang-nuts
My general recommendation is "don't".
It's quite rare that DBs and RPC interfaces will stay a direct mapping. Usually one or the other will need to evolve over time, and a bit of separation is not a bad thing.

Usually, my recommendation is to create "model" structs representing what's actually in the DB, and then have translation functions to convert to/from the protobuf forms. (I also recommend having an interface around the DB implementation itself to make testing and migrations easier)

I'm in the process of moving one of the services I own off gogo protobuf (which is quasi-abandoned and looking for new ownership because it needs to be rewritten on top of the new go protobuf api)


Thanks.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/239e2f6a-09f7-4e89-a921-ec3fead637c7n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages