asyncapi-codegen icon indicating copy to clipboard operation
asyncapi-codegen copied to clipboard

Clearly mark code that is designed for a Developer to change

Open gedw99 opened this issue 1 year ago • 6 comments

Hey @lerenn

2 things I want to ask for help with:

  1. Easily seeing what code i can expect to be stomped on by the code generator.

When I look at https://github.com/lerenn/asyncapi-codegen/tree/main/examples/helloworld/v3

All the going code has generators, and so you cant easily tell which code is your code and which is generated code.

A big and clear comment at the top of the files that are designed for a User to change that the generator will stomp on is the solution I think.

  1. Merge generated code with Developers own code.

https://github.com/lerenn/asyncapi-codegen?tab=readme-ov-file#with-async-api-generated-code explains the notion of User Code versus Generated code.

Also you can design in special comments into code files that are generated BUT also designed for Developers to add code to that same file , by using comment delineators. When the code generator is re-run alter, you can use there start and stop delineator to merge the generated code with the user code. An early way to do this is to:

a. First gen run is just normal, and generates any code files designed to have users code changes with the special comments. b. Next phase is to parse of all the code for files with these special comments, and copy each file in place from, for example, "file.go" to file". It's in the same directory, and so makes it easy. c. Then parse all the code again looking for a match of "file.go" and "file.go.tmp", and then do the merge based on the special comments. d. Delete "file.go.tmp"

The advantage of this approach is that the current generator is NOT affected. In stead we are just adding a 2nd phase. So this approach works fine with golang templates etc.

I used this technique for a very large Enterprise company that has a team for 500 developers. It was a total hit, and the developers liked it, because it solves an everyday problem and make it much easier to work with the code also. We were generating off a custom schema and we would generate the DB and Middle tier. The merging strategy did work well.

Please let me know if this is helpful ?

gedw99 avatar Aug 23 '24 19:08 gedw99

Hello @gedw99 ! Thanks for the tips and critics, it's really appreciated !

I'm currently on vacation so I won't be able to make the change now, but I'll take a look when I'll get back :)

lerenn avatar Aug 23 '24 21:08 lerenn