mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-23 11:33:37 +08:00
Merge bef375202fdeb3060e613a02d298a854a042fc02 into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646
This commit is contained in:
commit
bed758ccb0
@ -11,6 +11,7 @@ import (
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
@ -451,6 +452,74 @@ func TestBindingQueryStringMap(t *testing.T) {
|
||||
assert.Equal(t, "world", obj["hello"])
|
||||
}
|
||||
|
||||
func TestBindingMapPointer(t *testing.T) {
|
||||
const data = "foo=first&foo=last&empty="
|
||||
for _, b := range []struct {
|
||||
binding Binding
|
||||
path string
|
||||
body string
|
||||
}{
|
||||
{Query, "/?" + data, ""},
|
||||
{Form, "/", data},
|
||||
{FormPost, "/", data},
|
||||
} {
|
||||
t.Run(b.binding.Name(), func(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
obj any
|
||||
want any
|
||||
}{
|
||||
{
|
||||
"nil string map",
|
||||
new(map[string]string),
|
||||
&map[string]string{"foo": "last", "empty": ""},
|
||||
},
|
||||
{
|
||||
"initialized string map",
|
||||
&map[string]string{"foo": "stale", "keep": "value"},
|
||||
&map[string]string{"foo": "last", "empty": "", "keep": "value"},
|
||||
},
|
||||
{
|
||||
"nil string slice map",
|
||||
new(map[string][]string),
|
||||
&map[string][]string{"foo": {"first", "last"}, "empty": {""}},
|
||||
},
|
||||
{
|
||||
"initialized string slice map",
|
||||
&map[string][]string{"foo": {"stale"}, "keep": {"value"}},
|
||||
&map[string][]string{"foo": {"first", "last"}, "empty": {""}, "keep": {"value"}},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := requestWithBody(http.MethodPost, b.path, b.body)
|
||||
req.Header.Set("Content-Type", MIMEPOSTForm)
|
||||
require.NoError(t, req.ParseForm())
|
||||
source := req.Form
|
||||
var err error
|
||||
require.NotPanics(t, func() {
|
||||
err = b.binding.Bind(req, tt.obj)
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.want, tt.obj)
|
||||
assert.Equal(t, url.Values{"foo": {"first", "last"}, "empty": {""}}, source)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBindingUriMapPointer(t *testing.T) {
|
||||
form := map[string][]string{"foo": {"first", "last"}, "empty": {""}}
|
||||
var stringMap map[string]string
|
||||
require.NoError(t, Uri.BindUri(form, &stringMap))
|
||||
assert.Equal(t, map[string]string{"foo": "last", "empty": ""}, stringMap)
|
||||
|
||||
var sliceMap map[string][]string
|
||||
require.NoError(t, Uri.BindUri(form, &sliceMap))
|
||||
assert.Equal(t, form, sliceMap)
|
||||
}
|
||||
|
||||
func TestBindingXML(t *testing.T) {
|
||||
testBodyBinding(t,
|
||||
XML, "xml",
|
||||
|
||||
@ -53,6 +53,14 @@ func mapFormByTag(ptr any, form map[string][]string, tag string) error {
|
||||
}
|
||||
if ptrVal.Kind() == reflect.Map &&
|
||||
ptrVal.Type().Key().Kind() == reflect.String {
|
||||
if ptrVal.IsNil() && ptrVal.CanSet() && len(form) > 0 {
|
||||
m := reflect.MakeMap(ptrVal.Type())
|
||||
if err := setFormMap(m.Interface(), form); err != nil {
|
||||
return err
|
||||
}
|
||||
ptrVal.Set(m)
|
||||
return nil
|
||||
}
|
||||
if pointed != nil {
|
||||
ptr = pointed
|
||||
}
|
||||
|
||||
@ -182,6 +182,63 @@ func TestMapFormWithTag(t *testing.T) {
|
||||
assert.Equal(t, 6, s.F)
|
||||
}
|
||||
|
||||
func TestMapFormWithTagMapPointer(t *testing.T) {
|
||||
type namedMap map[string]string
|
||||
type namedSliceMap map[string][]string
|
||||
form := map[string][]string{"foo": {"first", "last"}, "empty": {""}}
|
||||
tests := []struct {
|
||||
name string
|
||||
obj any
|
||||
form map[string][]string
|
||||
want any
|
||||
err error
|
||||
}{
|
||||
{"absent string map", new(map[string]string), nil, new(map[string]string), nil},
|
||||
{"absent string slice map", new(map[string][]string), nil, new(map[string][]string), nil},
|
||||
{"empty string map", new(map[string]string), map[string][]string{}, new(map[string]string), nil},
|
||||
{"empty string slice map", new(map[string][]string), map[string][]string{}, new(map[string][]string), nil},
|
||||
{"nil string map", new(map[string]string), form, &map[string]string{"foo": "last", "empty": ""}, nil},
|
||||
{
|
||||
"nil string slice map", new(map[string][]string), form,
|
||||
&map[string][]string{"foo": {"first", "last"}, "empty": {""}}, nil,
|
||||
},
|
||||
{"unsupported value", new(map[string]int), form, new(map[string]int), ErrConvertToMapString},
|
||||
{"unsupported slice value", new(map[string][]int), form, new(map[string][]int), ErrConvertMapStringSlice},
|
||||
{"unsupported named map", new(namedMap), form, new(namedMap), ErrConvertToMapString},
|
||||
{"unsupported named slice map", new(namedSliceMap), form, new(namedSliceMap), ErrConvertMapStringSlice},
|
||||
{
|
||||
"string map by value",
|
||||
map[string]string{"foo": "stale", "keep": "value"},
|
||||
form,
|
||||
map[string]string{"foo": "last", "empty": "", "keep": "value"},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"string slice map by value",
|
||||
map[string][]string{"foo": {"stale"}, "keep": {"value"}},
|
||||
form,
|
||||
map[string][]string{"foo": {"first", "last"}, "empty": {""}, "keep": {"value"}},
|
||||
nil,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var err error
|
||||
require.NotPanics(t, func() {
|
||||
err = MapFormWithTag(tt.obj, tt.form, "externalTag")
|
||||
})
|
||||
if tt.err != nil {
|
||||
require.ErrorIs(t, err, tt.err)
|
||||
assert.Same(t, tt.err, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
assert.Equal(t, tt.want, tt.obj)
|
||||
assert.Equal(t, map[string][]string{"foo": {"first", "last"}, "empty": {""}}, form)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMappingTime(t *testing.T) {
|
||||
var s struct {
|
||||
Time time.Time
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user