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

Add support for "example" annotation used by Swaggo #101

Closed brock-weaver-roostify closed 2 years ago

brock-weaver-roostify commented 2 years ago

Would be nice to have the "example" annotation optionally emitted, which is used by swaggo.

e.g.: This:

{
  "name":"Frank"
}

would emit:

type AutoGenerated struct {
    Name string `json:"name" example:"Frank"`
}

In case there are multiple values, grabbing the most convenient one would make sense. e.g.:

{
  "people": [
    {
      "name":"Frank"
    },
    {
      "name":"Dennis"
    },
    {
      "name":"Dee"
    },
    {
      "name":"Charley"
    },
    {
      "name":"Mac"
    },
  ]
}

would emit:

type AutoGenerated struct {
    People []struct {
        Name string `json:"name" example:"Frank"`
    } `json:"people"`
}

I will have a PR for this shortly.