Skip to content

Commit

Permalink
Bug params header detector fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
verzth committed Mar 20, 2020
1 parent 9fa3034 commit 60d113f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
16 changes: 15 additions & 1 deletion demo/Demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,21 @@ func index(w http.ResponseWriter, r *http.Request) {
/*vn := req.GetMap("list")["obj"]
fmt.Println(vn.(map[string]interface{})["id"].([]interface{})[0])*/

fmt.Println(req.Get("test"))
if req.Filled("test"){
fmt.Println(req.Get("test"))
}else if req.Has("test"){
fmt.Println("Detected")
}else{
fmt.Println("Not detected")
}

if req.HeaderFilled("test"){
fmt.Println(req.Header("test"))
}else if req.HasHeader("test"){
fmt.Println("Detected")
}else{
fmt.Println("Not detected")
}

res.ReplySuccess("0000000", "SSSSSS", "Success", nil)
}
48 changes: 30 additions & 18 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/verzth/go-utils/utils"
"mime/multipart"
"net/http"
"reflect"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -205,7 +206,8 @@ func (r *Request) GetAll() map[string] interface{} {
}

func (r *Request) Get(key string) string {
if r.params[key] != nil && strings.ToLower(fmt.Sprintf("%v", r.params[key])) != "null" {
val := reflect.ValueOf(r.params[key])
if r.params[key] != nil || (val.Kind() == reflect.Slice && val.Len() > 0){
return fmt.Sprintf("%v", r.params[key])
}
return ""
Expand Down Expand Up @@ -450,44 +452,54 @@ func (r *Request) has(key string) bool {
return true
}

func (r *Request) Has(keys... string) bool {
found := true
func (r *Request) Has(keys... string) (found bool) {
found = true
for _, key := range keys {
found = found && r.has(key)
}
return found
return
}

func (r *Request) Filled(keys... string) (found bool) {
found = true
for _, key := range keys {
found = found && r.has(key)
val := reflect.ValueOf(r.params[key])
switch val.Kind() {
case reflect.String: found = found && strings.TrimSpace(r.Get(key)) != ""
case reflect.Slice: found = found && val.Len() > 0
}
}
return
}

func (r *Request) hasHeader(key string) bool {
if _, found := r.header[key]; !found {
if _, found := r.header[strings.Title(key)]; !found {
return false
}
return true
}

func (r *Request) HasHeader(keys... string) bool {
found := true
func (r *Request) HasHeader(keys... string) (found bool) {
found = true
for _, key := range keys {
found = found && r.hasHeader(key)
}
return found
return
}

func (r *Request) Filled(keys... string) bool {
found := true
func (r *Request) HeaderFilled(keys... string) (found bool) {
found = true
for _, key := range keys {
found = found && r.Has(key)
if _, ok := r.params[key].(string); ok {
found = found && strings.TrimSpace(r.params[key].(string)) != ""
}
found = found && r.hasHeader(key) && r.Header(key) != ""
}
return found
return
}

func (r *Request) HasFile(keys... string) bool {
found := true
func (r *Request) HasFile(keys... string) (found bool) {
found = true
for _, key := range keys {
found = found && r.files[key] != nil
}
return found
return
}

0 comments on commit 60d113f

Please sign in to comment.