moul / protoc-gen-gotemplate

:open_file_folder: generic protocol generator based on golang's text/template (grpc/protobuf)
https://manfred.life/protoc-gen-gotemplate
MIT License
438 stars 70 forks source link

support codegen off the structs after the protocol buffer gen has occured #56

Open joeblew99 opened 7 years ago

joeblew99 commented 7 years ago

Currently the protobufs are used to code gen from, which is a awesome because that gets us our Types and the Transport and bindng sorted out.

But there are other things like DBs and Action Commands that can then also use code gen. We can codegen off the structs that the protobuf code gen made for us !

Its all based on this, which is nice and simple. https://github.com/moul/protoc-gen-gotemplate

example: Stellar uses it for a very large project. There are 3 points where they gen from shown here: https://github.com/stellar/horizon/search?utf8=%E2%9C%93&q=go+generate&type=

Here is one of the layers in the architecture where all Command Actions are generated from: https://github.com/stellar/horizon/blob/master/src/github.com/stellar/horizon/main.go From that generate command, it looks at all the action commands. Here is just one: https://github.com/stellar/horizon/blob/master/src/github.com/stellar/horizon/actions_account.go

Wondering if this help. I think it has some merit

moul commented 7 years ago

Hi @joeblew99 and thank you for your feedback,

Can you confirm that you are looking for a (or, the best) way to chain multiple generation tools, i.e.,

  1. protoc-gen-gotemplate generates code from protobuf files
  2. go-codegen uses the generated files from step 1. to generate new files ?

I'm pretty sure you already can chain protoc-gen-gotemplate with other code generation tools quite easily; would an example showing a chaining scenario be helpful?

joeblew99 commented 7 years ago

Hey @moul thanks for being interested in this. I have some ideas and am really playing around with how to code gen the storage layer. The Chat example uses NATS at the moment and to me its a great example of what can be code generated. Butting a store underneath it seems like the next local step.

yes please if you can make an example in the Chat demo with NATS.

I am emailing you a document outlining what i am building. Its quite a big 2 or 3 page document and easier to send to you with the images. I would like to get your feedback and also see if its something you would want added to this repo or i do it elsewhere. It extends from what you have.


I am going to try to extend the CHAT example to support catchup when the client is offline. NATS can do that now i think, but not certain. Its described here: https://github.com/nats-io/go-nats-streaming#subscription-start-ie-replay-options

joeblew99 commented 7 years ago

@moul thanks for adding this. Now i can get busy on codegen of the lower and upper layers.

Which leads me to this - Have you seen this ? https://github.com/favadi/protoc-go-inject-tag

I am working on generating a strong typed DAL to BoltDB, and so i need to add a ton of meta data to the protobuf to hint what the data access layer codegen needs to do.

what do ya think ? Is it redundant now that go generate works. Well i could put the meta data somewhere else other than the protobuf to drive it but i prefer the protobuf as its all in one place. Its also possible with a KV store like BoltDB because you have a one to one mapping of the domain objects to the DB to a high degree.