refactor(context): remove unused Context dependency in get method (#4304)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Name · committed by GitHub · parent a4ac275 · Browse files · 2 files·+141 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: 1911860538 <alxps1911@gmail.com>
Diff stats
2 files+141 added−9 removed
- other:+141 added−9 removed
Provenance
- Commit
e4c2a27- Tree
31954c7- Author
Name <1911860538@qq.com>- Committer
GitHub <noreply@github.com>- Parent
a4ac275
2 files changed+141 added−9 removed
modified:context_test.go+131 added
3348 unmodified lines3349 setCookie := c.Writer.Header().Get("Set-Cookie")3350 assert.Contains(t, setCookie, "SameSite=None")3351 })3352 }3353+3354+func TestGetMapFromFormData(t *testing.T) {3355+ testCases := []struct {3356+ name string3357+ data map[string][]string3358+ key string3359+ expected map[string]string3360+ found bool3361+ }{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 bracket3429+ "ids]b": {"3.14"}, // Missing opening bracket3430+ "idsab": {"value"}, // No brackets3431+ },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+}modified:context.go+10 added-9 removed
571 unmodified lines572 // GetQueryMap returns a map for a given query key, plus a boolean value573 // whether at least one value exists for the given key.574 func (c *Context) GetQueryMap(key string) (map[string]string, bool) {575 c.initQueryCache()576- return c.get(c.queryCache, key)576+ return getMapFromFormData(c.queryCache, key)577 }578 579 // PostForm returns the specified key from a POST urlencoded form or multipart form580 // when it exists, otherwise it returns an empty string `("")`.64 unmodified lines645 // GetPostFormMap returns a map for a given form key, plus a boolean value646 // whether at least one value exists for the given key.647 func (c *Context) GetPostFormMap(key string) (map[string]string, bool) {648 c.initFormCache()649- return c.get(c.formCache, key)649+ return getMapFromFormData(c.formCache, key)650 }651 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 := false652+// 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 := false657 for k, v := range m {658 if i := strings.IndexByte(k, '['); i >= 1 && k[0:i] == key {659 if j := strings.IndexByte(k[i+1:], ']'); j >= 1 {659- exist = true660- dicts[k[i+1:][:j]] = v[0]660+ found = true661+ d[k[i+1:][:j]] = v[0]662 }663 }664 }664- return dicts, exist665+ return d, found666 }667 668 // FormFile returns the first file for the provided form key.669 func (c *Context) FormFile(name string) (*multipart.FileHeader, error) {725 unmodified lines