go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

Globally-defined string funcs for code generation from customized template

Open ez4bk opened this issue 5 months ago • 2 comments

Low flexibility when customizing template I'm using the goctl model command to generate model files for mongodb. Since go-zero currently do not support mongo-driver v2, I tried to customize the template file to accomodate my needs. Although {{.lowerType}} in defined when generating using model.tpl and model_custom.tpl, I really want to use untitled Type variable in model_types in my design.

Globally-defined string funcs I believe it will be better if the generation engine could offer some global funcs to manipulate strings for example {{lower | .Type}}, so that template customization can be more flexible.

Upgrade to mongo-driver v2 and better design An alternative is that go-zero could support mongo-driver v2 and optimize the design for model generation. The current design is a little complicated to read and understand. I'm using the gorm gen tool for SQL databases, which I consider generates very straight-forward-designed code.

ez4bk avatar Jun 13 '25 01:06 ez4bk

Thanks for your suggestion!

We'll upgrade to mongo-driver v2. Would you please give more details on how to make the model generation better?

kevwan avatar Jun 14 '25 14:06 kevwan

Thanks for your suggestion!

We'll upgrade to mongo-driver v2. Would you please give more details on how to make the model generation better?

Sure. The main concern for me is that I would like to have some globally defined functions to enbale something like {{lower | .var}}, so that more string maniluations to variables are possible.

In terms of the model generation, I think you can refer to GORM gentool. Not necessarily implement the GORM architect, but use the idea of spliting models and queries. From my point of view, it's good enough if you have the model_type and the model file. The model_custom file is really not that useful and adds the complexity of using it. Speaking of which, it's better if there's a official doc to give some example of using the generated code.

Looking forward to seeing the updates. Thank you!

ez4bk avatar Jun 17 '25 02:06 ez4bk