Type switching fix

This commit is contained in:
Fenny 2020-01-09 18:39:22 +01:00
parent 10ead9aa7b
commit d38cea2f89

View File

@ -136,18 +136,16 @@ func (ctx *Ctx) Cookies(args ...interface{}) string {
return ctx.Get("Cookie") return ctx.Get("Cookie")
} }
if len(args) == 1 { if len(args) == 1 {
str, strOk := args[0].(string) switch arg := args[0].(type) {
if strOk { case string:
return ctx.Get(str) return ctx.Get(arg)
} case func(string, string):
fnc, fncOk := args[0].(func(string, string))
if fncOk {
ctx.Fasthttp.Request.Header.VisitAllCookie(func(k, v []byte) { ctx.Fasthttp.Request.Header.VisitAllCookie(func(k, v []byte) {
fnc(b2s(k), b2s(v)) arg(b2s(k), b2s(v))
}) })
return "" default:
panic("Argument must be a string or func(string, string)")
} }
panic("Invalid parameters")
} }
if len(args) > 1 { if len(args) > 1 {
cook := &fasthttp.Cookie{} cook := &fasthttp.Cookie{}
@ -163,14 +161,14 @@ func (ctx *Ctx) Cookies(args ...interface{}) string {
} }
// ClearCookies : // ClearCookies :
func (ctx *Ctx) ClearCookies(args ...interface{}) { func (ctx *Ctx) ClearCookies(args ...string) {
if len(args) == 0 { if len(args) == 0 {
ctx.Fasthttp.Request.Header.VisitAllCookie(func(k, v []byte) { ctx.Fasthttp.Request.Header.VisitAllCookie(func(k, v []byte) {
ctx.Fasthttp.Response.Header.DelClientCookie(b2s(k)) ctx.Fasthttp.Response.Header.DelClientCookie(b2s(k))
}) })
} }
if len(args) == 1 { if len(args) == 1 {
ctx.Fasthttp.Response.Header.DelClientCookie(args[0].(string)) ctx.Fasthttp.Response.Header.DelClientCookie(args[0])
} }
} }
@ -310,9 +308,9 @@ func (ctx *Ctx) Is(ext string) bool {
} }
// Attachment : // Attachment :
func (ctx *Ctx) Attachment(args ...interface{}) { func (ctx *Ctx) Attachment(args ...string) {
if len(args) == 1 { if len(args) == 1 {
filename := filepath.Base(args[0].(string)) filename := filepath.Base(args[0])
ctx.Type(filepath.Ext(filename)) ctx.Type(filepath.Ext(filename))
ctx.Set("Content-Disposition", `attachment; filename="`+filename+`"`) ctx.Set("Content-Disposition", `attachment; filename="`+filename+`"`)
return return
@ -321,14 +319,14 @@ func (ctx *Ctx) Attachment(args ...interface{}) {
} }
// Download : // Download :
func (ctx *Ctx) Download(args ...interface{}) { func (ctx *Ctx) Download(args ...string) {
if len(args) == 0 { if len(args) == 0 {
panic("Missing filename") panic("Missing filename")
} }
file := args[0].(string) file := args[0]
filename := filepath.Base(file) filename := filepath.Base(file)
if len(args) > 1 { if len(args) > 1 {
filename = args[1].(string) filename = args[1]
} }
ctx.Set("Content-Disposition", "attachment; filename="+filename) ctx.Set("Content-Disposition", "attachment; filename="+filename)
ctx.SendFile(file) ctx.SendFile(file)