2021-01-12 12:08

Suggestion: Namespace purely generated code vs user modifiable code

Awesome looking project, I am thinking about using this for a static website that will maybe update images / text and other information infrequently. Ponzu seems like a decent fit, thank you for putting this together :) However, as a generated code junkie, I have a humble suggestion:

What do you think about name-spacing generated files like book.go and author.go from your video walkthrough example as book_gen.go and author_gen.go? This will allow all boilerplate methods to live in one file which can be arbitrarily updated without worrying about nuking precious user code that has been written.

For example, imagine if func (b *Book) Push() []string, func (b *Book) Create(...) and other such interface methods lived in a default empty book.go file. This also means that users can be told to stay away from the _gen variants which typically keeps things nicely separated between what the platform provides and what the user wants to customize.


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • weixin_39622217 weixin_39622217 4月前

    In my mind, any code which is boilerplate (in your example everything until the Push() implementation), would live in book_gen.go which automatically tells all users that this was machine generated and is never intended for human editing. I usually have a big warning in the template that generates the code which reads : "Machine generated file, edits will be lost" or something to that effect.

    We would also have a separate book.go with just the package header and some very simple and helpful comments that will help users extend the relevant parts of their types :)

    With all that said, I do not fully yet grok all the subtleties of this project, and have not tried it out with the application I set out trying to build - just got excited when I saw what I assumed was boilerplate code :) Keep up the awesome work!

    点赞 评论 复制链接分享
  • weixin_39705850 weixin_39705850 4月前

    I use code generator too but almost always change the generated code.

    If we want to have separately generated code we need to have some yml or json it any other files from which the code will be generated and changing the yml file will result in updated code.

    点赞 评论 复制链接分享
  • weixin_39557199 weixin_39557199 4月前

    Closing this issue. It appears to be answered/resolved. Please reopen if not the case.

    点赞 评论 复制链接分享
  • weixin_39761696 weixin_39761696 4月前
    • thanks for the suggestion! You make a valid point about separating the generated code from purely user-written code. However, personally, I have not yet worked on a Ponzu project in which I didn't manually change the generated code at least once after generating it -- just an artifact of Ponzu's code gen being merely a nice "starting off" point, vs. a piece of code that is never touched after generate time.

    Additionally, there are guards in place to prevent the result of ponzu gen content ... rewriting any files on disk. If there is a file in your tree that conflicts with Ponzu's intended output file, the command will fail -- though the file naming policy is potentially safer.

    I do see how adding a naming convention could help ease any concerns though. I'll leave this open to get more feedback from others though.

    cc/ -cms/team

    点赞 评论 复制链接分享