I'm a big fan of code generation for tedious, boilerplate stuff. I stumbled across go-swagger the other day and it looks pretty nice. The code it generates is as clean as generated code can be, and there are enough hooks to let you inject functionality where you need (e.g. go middleware).
I'm looking at using swagger-codegen for generating a client side API with TypeScript, getting me to a full cycle of:
- develop API model in swagger
- generate go models + plumbing
- generate TypeScript models + plumbing
- type check everything!
评论:
Jamo008:
vburenin:Link to go-swagger
kpurdon:Link to the go-swagger picture https://twitter.com/asteroix/status/723728506648059904 and the other https://twitter.com/asteroix/status/726160120766320641
3264128256:as clean as generated code can be
Ya ... never clean enough for me to go with generated code. I always find myself doing 10x more work to get generated code to work the way I want (and to test it properly).
I tried go-swagger for awhile (though was going the other way comments>spec) and ended up bailing on it in favor of writing my own code and specs. However we (work) are using swagger-codegen to generate typescript for the FE that we additionally use to validate that the spec/BE are in sync. Pretty neat to have a FE-SPEC-BE validation tree that we can use. Keeps us all in sync!
You may be interested in https://github.com/swagger-api/swagger-codegen/pull/2763 which a co-worker recently got merged into codegen.
chub79:You might also want to give goa a try.
casualjim:I haven't looked at goa nor go-swagger but, how do they generate meaningful tests from a swagger spec?
chub79:go-swagger doesn't at this point, but nothing is stopping you from adding it. The reason it doesn't is that I wanted to make the model generation support all of the swagger first before I got into doing more features. I felt that stability was more important than features. That being said it has pretty much stabilized now, so it could be added by a motivated party.
Okay. I was curious because I'm facing that very question :)