Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add method PostFormArray to app.RequestContext. #1172

Merged
merged 4 commits into from
Sep 2, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions pkg/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,17 @@ func (ctx *RequestContext) multipartFormValue(key string) (string, bool) {
return "", false
}

func (ctx *RequestContext) multipartFormValueArray(key string) ([]string, bool) {
mf, err := ctx.MultipartForm()
if err == nil && mf.Value != nil {
vv := mf.Value[key]
if len(vv) > 0 {
return vv, true
}
}
return nil, false
}

func (ctx *RequestContext) RequestBodyStream() io.Reader {
return ctx.Request.BodyStream()
}
Expand Down Expand Up @@ -1366,6 +1377,13 @@ func (ctx *RequestContext) PostForm(key string) string {
return value
}

// PostFormArray returns the specified key from a POST urlencoded form or multipart form
// when it exists, otherwise it returns an empty array `([])`.
func (ctx *RequestContext) PostFormArray(key string) []string {
values, _ := ctx.GetPostFormArray(key)
return values
}

// DefaultPostForm returns the specified key from a POST urlencoded form or multipart form
// when it exists, otherwise it returns the specified defaultValue string.
//
Expand Down Expand Up @@ -1393,6 +1411,32 @@ func (ctx *RequestContext) GetPostForm(key string) (string, bool) {
return ctx.multipartFormValue(key)
}

// GetPostFormArray is like PostFormArray(key). It returns the specified key from a POST urlencoded
// form or multipart form when it exists `([]string, true)` (even when the value is an empty string),
// otherwise it returns ([]string(nil), false).
//
// For example, during a PATCH request to update the item's tags:
//
// tags=tag1 tag=tag2 tag=tag3 --> (["tag1", "tag2", "tag3"], true) := GetPostFormArray("tags") // set tags to ["tag1", "tag2", "tag3"]
snowykami marked this conversation as resolved.
Show resolved Hide resolved
// tags= --> (nil, true) := GetPostFormArray("tags") // set tags to nil
// --> (nil, false) := GetPostFormArray("tags") // do nothing with tags
func (ctx *RequestContext) GetPostFormArray(key string) ([]string, bool) {
snowykami marked this conversation as resolved.
Show resolved Hide resolved
//if v, exists := ctx.PostArgs().PeekExists(key); exists {
snowykami marked this conversation as resolved.
Show resolved Hide resolved
// return strings.Split(v, ","), exists
//}
vs := ctx.PostArgs().PeekAll(key)
// [][]byte -> []string
values := make([]string, len(vs))
for i, v := range vs {
values[i] = bytesconv.B2s(v)
snowykami marked this conversation as resolved.
Show resolved Hide resolved
}
if len(values) == 0 {
return ctx.multipartFormValueArray(key)
} else {
return values, true
}
}

// bodyAllowedForStatus is a copy of http.bodyAllowedForStatus non-exported function.
func bodyAllowedForStatus(status int) bool {
switch {
Expand Down
Loading