gorilla / schema

Package gorilla/schema fills a struct with form values.
https://gorilla.github.io
BSD 3-Clause "New" or "Revised" License
1.39k stars 231 forks source link

[feature] Use json struct tags if wanted #184

Closed RealHarshThakur closed 2 years ago

RealHarshThakur commented 3 years ago

Is your feature request related to a problem? Please describe.

I have an API which already accepts JSON as content type, so all the structs have the json tags in them. To add support for form-encoded, I have to now add schema tags to each field same as json tag. …

Describe the solution you'd like

I was hoping json tags can be picked up as a fallback when schema tags aren't provided.

Describe alternatives you've considered

Are there alternatives you've tried, and/or workarounds in-place?

logansam commented 2 years ago

You can use the SetAliasTag method on the decoder to change the name of the struct tag

schemaDecoder = schema.NewDecoder() schemaDecoder.SetAliasTag("json")

RealHarshThakur commented 2 years ago

Thanks, that sounds amazing.