feat: Use new go-based generator#101
Conversation
There was a problem hiding this comment.
Pull request overview
This PR replaces the dependency on openapitools/openapi-generator-cli with a custom Go-based code generator. The motivation is to eliminate a large dependency and overcome templating language limitations. The output is claimed to be identical, with the only difference being that generated files now use .gen.go filenames for easier identification in .gitattributes.
Changes:
- Implements a new Go-based OpenAPI code generator (~1000 lines) in
tools/codegen/ - Converts Handlebars templates to Go templates with updated syntax
- Generates identical output but with
.gen.gofile extensions - Removes dependency on Java-based OpenAPI generator tool
Reviewed changes
Copilot reviewed 37 out of 240 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| tools/codegen/*.go | New Go-based generator implementation with parser, preprocessor, and template functions |
| tools/codegen/go.mod | Go module dependencies for the new generator |
| tools/codegen/templates/*.tmpl | Converted templates from Handlebars to Go template syntax |
| templates/*.handlebars | Deleted old Handlebars templates |
| platform/*.gen.go | Regenerated model files with new .gen.go extension |
| platform/.openapi-generator/VERSION | Removed as no longer using openapi-generator-cli |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| tmpl, err := template. | ||
| New(""). | ||
| Funcs(templateFuncs{parser}.Funcs()). | ||
| ParseFS(templatesFS, "templates/*.tmpl") |
There was a problem hiding this comment.
Can you please add the directory to templates as a flag option to the generator itself?
Such that:
- //go:generate go run -C ../tools/codegen . --input ../../controlplane.yaml --output ../../controlplane --package controlplane
+ //go:generate go run -C ../tools/codegen . --input ../../controlplane.yaml --output ../../controlplane --package controlplane --templates ../tools/codegen/templatesI believe @ajpetersons and @petar-cvit would benefit from being able to supply custom templates in separate external projects.
There was a problem hiding this comment.
It would also allow us to replace openapi-generator for other languages too actually.
Hmmm. Now I wonder if we should put this in unikraft.com/x/tools...
There was a problem hiding this comment.
As discussed offline, this is the eventual aim, so that we can support other SDKs and other codegen from openapi.
However, if we're doing that, the exact functions / data become part of an internal API. I'm not entirely sure I'm happy with the current state being exposed.
We could try and merge this as-is, and then later, we can pull this into x, and make templates fully configurable.
3ac0359 to
fbf2d5c
Compare
Signed-off-by: Justin Chadwell <justin@unikraft.com>
Signed-off-by: Justin Chadwell <justin@unikraft.com>
Signed-off-by: Justin Chadwell <justin@unikraft.com>
fbf2d5c to
e0a7211
Compare
| @@ -0,0 +1,50 @@ | |||
| module unikraft.com/cloud/sdk/tools/codegen | |||
|
|
|||
| go 1.24.0 | |||
There was a problem hiding this comment.
Maybe bump to the latest version hehe >:D
craciunoiuc
left a comment
There was a problem hiding this comment.
I trust this PR with my life
Reviewed-by: Cezar Craciunoiu cezar.craciunoiu@unikraft.com
nderjung
left a comment
There was a problem hiding this comment.
Thanks!
Approved-by: Alexander Jung alex@unikraft.com
This removes our dependency on openapitools/openapi-generator-cli, which has a few issues:
To do this, we adapt all the templates into golang, and do all the parsing in Go. It's a tidy ~1000 lines of Go to do all the parsing. We could do further simplifications as well, we'd need something like getkin/kin-openapi#695 to remove our weird reordering work.
The output is identical. The only difference now is that the generated files get
.gen.gofilenames, so that they can easily be marked in.gitattributes.