Skip to content

Commit e4c2a27

Browse files
19118605381911860538
andauthored
refactor(context): remove unused Context dependency in get method (#4304)
Co-authored-by: 1911860538 <[email protected]>
1 parent a4ac275 commit e4c2a27

File tree

2 files changed

+141
-9
lines changed

2 files changed

+141
-9
lines changed

context.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ func (c *Context) QueryMap(key string) (dicts map[string]string) {
573573
// whether at least one value exists for the given key.
574574
func (c *Context) GetQueryMap(key string) (map[string]string, bool) {
575575
c.initQueryCache()
576-
return c.get(c.queryCache, key)
576+
return getMapFromFormData(c.queryCache, key)
577577
}
578578

579579
// PostForm returns the specified key from a POST urlencoded form or multipart form
@@ -646,22 +646,23 @@ func (c *Context) PostFormMap(key string) (dicts map[string]string) {
646646
// whether at least one value exists for the given key.
647647
func (c *Context) GetPostFormMap(key string) (map[string]string, bool) {
648648
c.initFormCache()
649-
return c.get(c.formCache, key)
649+
return getMapFromFormData(c.formCache, key)
650650
}
651651

652-
// get is an internal method and returns a map which satisfies conditions.
653-
func (c *Context) get(m map[string][]string, key string) (map[string]string, bool) {
654-
dicts := make(map[string]string)
655-
exist := false
652+
// getMapFromFormData return a map which satisfies conditions.
653+
// It parses from data with bracket notation like "key[subkey]=value" into a map.
654+
func getMapFromFormData(m map[string][]string, key string) (map[string]string, bool) {
655+
d := make(map[string]string)
656+
found := false
656657
for k, v := range m {
657658
if i := strings.IndexByte(k, '['); i >= 1 && k[0:i] == key {
658659
if j := strings.IndexByte(k[i+1:], ']'); j >= 1 {
659-
exist = true
660-
dicts[k[i+1:][:j]] = v[0]
660+
found = true
661+
d[k[i+1:][:j]] = v[0]
661662
}
662663
}
663664
}
664-
return dicts, exist
665+
return d, found
665666
}
666667

667668
// FormFile returns the first file for the provided form key.

context_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,3 +3350,134 @@ func TestContextSetCookieData(t *testing.T) {
33503350
assert.Contains(t, setCookie, "SameSite=None")
33513351
})
33523352
}
3353+
3354+
func TestGetMapFromFormData(t *testing.T) {
3355+
testCases := []struct {
3356+
name string
3357+
data map[string][]string
3358+
key string
3359+
expected map[string]string
3360+
found bool
3361+
}{
3362+
{
3363+
name: "Basic bracket notation",
3364+
data: map[string][]string{
3365+
"ids[a]": {"hi"},
3366+
"ids[b]": {"3.14"},
3367+
},
3368+
key: "ids",
3369+
expected: map[string]string{
3370+
"a": "hi",
3371+
"b": "3.14",
3372+
},
3373+
found: true,
3374+
},
3375+
{
3376+
name: "Mixed data with bracket notation",
3377+
data: map[string][]string{
3378+
"ids[a]": {"hi"},
3379+
"ids[b]": {"3.14"},
3380+
"names[a]": {"mike"},
3381+
"names[b]": {"maria"},
3382+
"other[key]": {"value"},
3383+
"simple": {"data"},
3384+
},
3385+
key: "ids",
3386+
expected: map[string]string{
3387+
"a": "hi",
3388+
"b": "3.14",
3389+
},
3390+
found: true,
3391+
},
3392+
{
3393+
name: "Names key",
3394+
data: map[string][]string{
3395+
"ids[a]": {"hi"},
3396+
"ids[b]": {"3.14"},
3397+
"names[a]": {"mike"},
3398+
"names[b]": {"maria"},
3399+
"other[key]": {"value"},
3400+
},
3401+
key: "names",
3402+
expected: map[string]string{
3403+
"a": "mike",
3404+
"b": "maria",
3405+
},
3406+
found: true,
3407+
},
3408+
{
3409+
name: "Key not found",
3410+
data: map[string][]string{
3411+
"ids[a]": {"hi"},
3412+
"names[b]": {"maria"},
3413+
},
3414+
key: "notfound",
3415+
expected: map[string]string{},
3416+
found: false,
3417+
},
3418+
{
3419+
name: "Empty data",
3420+
data: map[string][]string{},
3421+
key: "ids",
3422+
expected: map[string]string{},
3423+
found: false,
3424+
},
3425+
{
3426+
name: "Malformed bracket notation",
3427+
data: map[string][]string{
3428+
"ids[a": {"hi"}, // Missing closing bracket
3429+
"ids]b": {"3.14"}, // Missing opening bracket
3430+
"idsab": {"value"}, // No brackets
3431+
},
3432+
key: "ids",
3433+
expected: map[string]string{},
3434+
found: false,
3435+
},
3436+
{
3437+
name: "Nested bracket notation",
3438+
data: map[string][]string{
3439+
"ids[a][b]": {"nested"},
3440+
"ids[c]": {"simple"},
3441+
},
3442+
key: "ids",
3443+
expected: map[string]string{
3444+
"a": "nested",
3445+
"c": "simple",
3446+
},
3447+
found: true,
3448+
},
3449+
{
3450+
name: "Simple key without brackets",
3451+
data: map[string][]string{
3452+
"simple": {"data"},
3453+
"ids[a]": {"hi"},
3454+
},
3455+
key: "simple",
3456+
expected: map[string]string{},
3457+
found: false,
3458+
},
3459+
{
3460+
name: "Mixed simple and bracket keys",
3461+
data: map[string][]string{
3462+
"simple": {"data"},
3463+
"ids[a]": {"hi"},
3464+
"ids[b]": {"3.14"},
3465+
"other": {"value"},
3466+
},
3467+
key: "ids",
3468+
expected: map[string]string{
3469+
"a": "hi",
3470+
"b": "3.14",
3471+
},
3472+
found: true,
3473+
},
3474+
}
3475+
3476+
for _, tc := range testCases {
3477+
t.Run(tc.name, func(t *testing.T) {
3478+
result, found := getMapFromFormData(tc.data, tc.key)
3479+
assert.Equal(t, tc.expected, result, "result mismatch")
3480+
assert.Equal(t, tc.found, found, "found mismatch")
3481+
})
3482+
}
3483+
}

0 commit comments

Comments
 (0)