mholt / json-to-go

Translates JSON into a Go type in your browser instantly (original)
https://mholt.github.io/json-to-go/
MIT License
4.48k stars 473 forks source link

optionally emits "example" annotation now #102

Closed brock-weaver-roostify closed 2 years ago

brock-weaver-roostify commented 2 years ago

added test cases as well

brock-weaver-roostify commented 2 years ago

added strict type check, my bad

mholt commented 2 years ago

What is this, exactly? What is the motivation, where is the issue discussed first, etc?

brock-weaver-roostify commented 2 years ago

This was an enhancement I needed for my purposes -- I had an example json payload and I wanted the values in that example to be emitted in the generated model definition. The "example" annotation alongside the "json" annotation is picked up by https://github.com/swaggo/gin-swagger when generating Swagger documentation.

e.g. I had this:

{
  "firstName":"Alice"
}

and I wanted this go model to be emitted:

type AutoGenerated struct {
  FirstName `json:"firstName" example:"Alice"`
} 

gin-swagger uses the "example" annotation there and will include it in the resulting swagger file it generates when running "swag init".

mholt commented 2 years ago

Oh I see, #101. (Somehow missed that.)

Seems to be a simple enough change. Thanks for the contribution.

brock-weaver-roostify commented 2 years ago

Oh sorry -- I should have tagged #101 on that. My bad. And thank you for such a useful tool!