Skip to content
Myr

feat(binding): support custom struct tag (#2720)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat(binding): support custom struct tag Add function `binding.MapFormWithTag` (#2719) * doc: add 'bind form-data with custom struct tag' Add 'Bind form-data request with custom struct and custom tag' section (#2719) * test(binding): add test for MapFromWithTag

authored by voidman · committed by GitHub · parent e3ee01d · Browse files · 3 files+68 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+68 added−0 removed

  • other:+68 added−0 removed

3 files+68 added−0 removed

modified:binding/form_mapping_test.go+9 added
143 unmodified lines
144 assert.NoError(t, err)145 assert.Equal(t, int(6), s.F)146}147148func TestMapFormWithTag(t *testing.T) {149 var s struct {150 F int `externalTag:"field"`151 }152 err := MapFormWithTag(&s, map[string][]string{"field": {"6"}}, "externalTag")153 assert.NoError(t, err)154 assert.Equal(t, int(6), s.F)155}156157func TestMappingTime(t *testing.T) {158 var s struct {159 Time time.Time160 LocalTime time.Time `time_format:"2006-01-02"`
130 unmodified lines
modified:binding/form_mapping.go+4 added
32 unmodified lines
33func mapForm(ptr interface{}, form map[string][]string) error {34 return mapFormByTag(ptr, form, "form")35}3637func MapFormWithTag(ptr interface{}, form map[string][]string, tag string) error {38 return mapFormByTag(ptr, form, tag)39}4041var emptyField = reflect.StructField{}4243func mapFormByTag(ptr interface{}, form map[string][]string, tag string) error {44 // Check if ptr is a map
360 unmodified lines
modified:README.md+55 added
2019 unmodified lines
2020`ProtoBuf`. For other formats, `Query`, `Form`, `FormPost`, `FormMultipart`,2021can be called by `c.ShouldBind()` multiple times without any damage to2022performance (See [#1341](https://github.com/gin-gonic/gin/pull/1341)).20232024### Bind form-data request with custom struct and custom tag20252026```go2027const (2028 customerTag = "url"2029 defaultMemory = 32 << 202030)20312032type customerBinding struct {}20332034func (customerBinding) Name() string {2035 return "form"2036}20372038func (customerBinding) Bind(req *http.Request, obj interface{}) error {2039 if err := req.ParseForm(); err != nil {2040 return err2041 }2042 if err := req.ParseMultipartForm(defaultMemory); err != nil {2043 if err != http.ErrNotMultipart {2044 return err2045 }2046 }2047 if err := binding.MapFormWithTag(obj, req.Form, customerTag); err != nil {2048 return err2049 }2050 return validate(obj)2051}20522053func validate(obj interface{}) error {2054 if binding.Validator == nil {2055 return nil2056 }2057 return binding.Validator.ValidateStruct(obj)2058}20592060// Now we can do this!!!2061// FormA is a external type that we can't modify it's tag2062type FormA struct {2063 FieldA string `url:"field_a"`2064}20652066func ListHandler(s *Service) func(ctx *gin.Context) {2067 return func(ctx *gin.Context) {2068 var urlBinding = customerBinding{}2069 var opt FormA2070 err := ctx.MustBindWith(&opt, urlBinding)2071 if err != nil {2072 ...2073 }2074 ...2075 }2076}2077```20782079### http2 server push20802081http.Pusher is supported only **go1.8+**. See the [golang blog](https://blog.golang.org/h2push) for detail information.2082
207 unmodified lines