Add Format function

pull/6/head
Fenny 2020-01-22 12:08:14 +01:00
parent 31876e3109
commit d4a051dc85
3 changed files with 65 additions and 20 deletions

View File

@ -24,7 +24,7 @@ app.Get("/", func(c *fiber.Ctx) {
// => "application/json" // => "application/json"
// Accept: text/*, application/json // Accept: text/*, application/json
c.Accepts("image/png" c.Accepts("image/png")
c.Accepts("png") c.Accepts("png")
// => "" // => ""
}) })
@ -321,8 +321,29 @@ app.Get("/", func(c *fiber.Ctx) {
}) })
``` ```
#### !Format #### Format
!> Planned for v2.0.0 Performs content-negotiation on the Accept HTTP header. It uses [Accepts](#accepts) to select a proper format. If the header is not specified or there is no proper format, text/plain is used.
```go
// Function signature
c.Format(body string)
c.Format(body []byte)
// Example
app.Get("/", func(c *fiber.Ctx) {
// Accept: text/plain
c.Format("Hello, World!")
// => Hello, World!
// Accept: text/html
c.Format("Hello, World!")
// => <p>Hello, World!</p
// Accept: application/json
c.Format("Hello, World!")
// => "Hello, World!"
})
```
#### FormFile #### FormFile
MultipartForm files can be retrieved by name, the first file from the given key is returned. MultipartForm files can be retrieved by name, the first file from the given key is returned.
@ -444,7 +465,7 @@ app.Get("/", func(c *fiber.Ctx) {
``` ```
#### Json #### Json
Converts any interface to json using [Jsoniter](https://github.com/json-iterator/go), this functions also sets the content header to application/json. Converts any interface or string to json using [Jsoniter](https://github.com/json-iterator/go), this function also sets the content header to application/json.
```go ```go
// Function signature // Function signature
c.Json(v interface{}) error c.Json(v interface{}) error
@ -463,9 +484,13 @@ app.Get("/json", func(c *fiber.Ctx) {
} }
c.Json(data) c.Json(data)
// => "{"Name": "Grame", "Age": 20}" // => "{"Name": "Grame", "Age": 20}"
c.Json("Hello, World!")
// => "Hello, World!"
}) })
app.Listen(8080) app.Listen(8080)
``` ```
Or with error checking Or with error checking
```go ```go
app.Get("/json", func(c *fiber.Ctx) { app.Get("/json", func(c *fiber.Ctx) {

View File

@ -19,13 +19,13 @@ import (
// Append : https://gofiber.github.io/fiber/#/context?id=append // Append : https://gofiber.github.io/fiber/#/context?id=append
func (ctx *Ctx) Append(field string, values ...string) { func (ctx *Ctx) Append(field string, values ...string) {
newVal := ctx.Get(field) value := ctx.Get(field)
if len(values) > 0 { if len(values) > 0 {
for i := range values { for i := range values {
newVal = newVal + ", " + values[i] value = fmt.Sprintf("%s, %s", value, values[i])
} }
} }
ctx.Set(field, newVal) ctx.Set(field, value)
} }
// Attachment : https://gofiber.github.io/fiber/#/context?id=attachment // Attachment : https://gofiber.github.io/fiber/#/context?id=attachment
@ -41,16 +41,15 @@ func (ctx *Ctx) Attachment(name ...string) {
// ClearCookie : https://gofiber.github.io/fiber/#/context?id=clearcookie // ClearCookie : https://gofiber.github.io/fiber/#/context?id=clearcookie
func (ctx *Ctx) ClearCookie(name ...string) { func (ctx *Ctx) ClearCookie(name ...string) {
if len(name) == 0 { if len(name) > 0 {
ctx.Fasthttp.Request.Header.VisitAllCookie(func(k, v []byte) {
fmt.Println(b2s(k), b2s(v))
ctx.Fasthttp.Response.Header.DelClientCookie(b2s(k))
})
} else if len(name) > 0 {
for i := range name { for i := range name {
ctx.Fasthttp.Response.Header.DelClientCookie(name[i]) ctx.Fasthttp.Response.Header.DelClientCookie(name[i])
} }
return
} }
ctx.Fasthttp.Request.Header.VisitAllCookie(func(k, v []byte) {
ctx.Fasthttp.Response.Header.DelClientCookie(b2s(k))
})
} }
// Cookie : https://gofiber.github.io/fiber/#/context?id=cookie // Cookie : https://gofiber.github.io/fiber/#/context?id=cookie
@ -115,8 +114,28 @@ func (ctx *Ctx) End() {
} }
// Format : https://gofiber.github.io/fiber/#/context?id=format // Format : https://gofiber.github.io/fiber/#/context?id=format
func (ctx *Ctx) Format() { func (ctx *Ctx) Format(args ...interface{}) {
if len(args) == 0 {
panic("Missing string or []byte body")
}
var body string
switch b := args[0].(type) {
case string:
body = b
case []byte:
body = b2s(b)
default:
panic("Body must be a string or []byte")
}
accept := ctx.Accepts("html", "json")
switch accept {
case "html":
ctx.SendString("<p>" + body + "</p>")
case "json":
ctx.Json(body)
default:
ctx.SendString(body)
}
} }
// HeadersSent : https://gofiber.github.io/fiber/#/context?id=headerssent // HeadersSent : https://gofiber.github.io/fiber/#/context?id=headerssent
@ -126,12 +145,13 @@ func (ctx *Ctx) HeadersSent() {
// Json : https://gofiber.github.io/fiber/#/context?id=json // Json : https://gofiber.github.io/fiber/#/context?id=json
func (ctx *Ctx) Json(v interface{}) error { func (ctx *Ctx) Json(v interface{}) error {
raw, err := jsoniter.Marshal(&v)
raw, err := jsoniter.MarshalToString(&v)
if err != nil { if err != nil {
return err return err
} }
ctx.Set("Content-Type", "application/json") ctx.Set("Content-Type", "application/json")
ctx.Fasthttp.Response.SetBodyString(b2s(raw)) ctx.Fasthttp.Response.SetBodyString(raw)
return nil return nil
} }
@ -205,8 +225,8 @@ func (ctx *Ctx) Render() {
func (ctx *Ctx) Send(args ...interface{}) { func (ctx *Ctx) Send(args ...interface{}) {
// https://github.com/valyala/fasthttp/blob/master/http.go#L490 // https://github.com/valyala/fasthttp/blob/master/http.go#L490
if len(args) != 1 { if len(args) == 0 {
panic("To many arguments!") panic("Missing string or []byte body")
} }
switch body := args[0].(type) { switch body := args[0].(type) {
case string: case string:
@ -291,7 +311,7 @@ func (ctx *Ctx) Vary(field ...string) {
// Write : https://gofiber.github.io/fiber/#/context?id=write // Write : https://gofiber.github.io/fiber/#/context?id=write
func (ctx *Ctx) Write(args ...interface{}) { func (ctx *Ctx) Write(args ...interface{}) {
if len(args) == 0 { if len(args) == 0 {
panic("Missing body") panic("Missing string or []byte body")
} }
switch body := args[0].(type) { switch body := args[0].(type) {
case string: case string: