diff --git a/app.go b/app.go index 47a01dd08a0..f48ff3de46d 100644 --- a/app.go +++ b/app.go @@ -144,6 +144,12 @@ type Config struct { // Default: false CaseSensitive bool `json:"case_sensitive"` + // When set to true, this will add a default value tag support to all parsers. + // This will allow you to set default values for query, path, and body parameters. + // + // Default: false + DefaultValueParser bool `json:"default_value_parser"` + // When set to true, this relinquishes the 0-allocation promise in certain // cases in order to access the handler values (e.g. request bodies) in an // immutable fashion so that these values are available even if you return diff --git a/ctx.go b/ctx.go index 55b81cf2ee7..31752132b2b 100644 --- a/ctx.go +++ b/ctx.go @@ -388,11 +388,24 @@ func (c *Ctx) BodyParser(out interface{}) error { } // Parse body accordingly - if strings.HasSuffix(ctype, "json") { + switch { + case strings.HasSuffix(ctype, "json"): + if c.app.config.DefaultValueParser { + utils.SetDefaultValues(out) + } return c.app.config.JSONDecoder(c.Body(), out) - } - if strings.HasPrefix(ctype, MIMEApplicationForm) { - data := make(map[string][]string) + + case strings.HasPrefix(ctype, MIMETextXML), strings.HasPrefix(ctype, MIMEApplicationXML): + if c.app.config.DefaultValueParser { + utils.SetDefaultValues(out) + } + if err := xml.Unmarshal(c.Body(), out); err != nil { + return fmt.Errorf("failed to unmarshal: %w", err) + } + return nil + + case strings.HasPrefix(ctype, MIMEApplicationForm): + formBody := make(map[string][]string) var err error c.fasthttp.PostArgs().VisitAll(func(key, val []byte) { @@ -410,28 +423,23 @@ func (c *Ctx) BodyParser(out interface{}) error { if c.app.config.EnableSplittingOnParsers && strings.Contains(v, ",") && equalFieldType(out, reflect.Slice, k, bodyTag) { values := strings.Split(v, ",") for i := 0; i < len(values); i++ { - data[k] = append(data[k], values[i]) + formBody[k] = append(formBody[k], values[i]) } } else { - data[k] = append(data[k], v) + formBody[k] = append(formBody[k], v) } }) + return c.parseToStruct(bodyTag, out, formBody) - return c.parseToStruct(bodyTag, out, data) - } - if strings.HasPrefix(ctype, MIMEMultipartForm) { - data, err := c.fasthttp.MultipartForm() + case strings.HasPrefix(ctype, MIMEMultipartForm): + multipartBody, err := c.fasthttp.MultipartForm() if err != nil { return err } - return c.parseToStruct(bodyTag, out, data.Value) - } - if strings.HasPrefix(ctype, MIMETextXML) || strings.HasPrefix(ctype, MIMEApplicationXML) { - if err := xml.Unmarshal(c.Body(), out); err != nil { - return fmt.Errorf("failed to unmarshal: %w", err) - } - return nil + return c.parseToStruct(bodyTag, out, multipartBody.Value) + } + // No suitable content type found return ErrUnprocessableEntity } @@ -1086,7 +1094,10 @@ func (c *Ctx) AllParams() map[string]string { func (c *Ctx) ParamsParser(out interface{}) error { params := make(map[string][]string, len(c.route.Params)) for _, param := range c.route.Params { - params[param] = append(params[param], c.Params(param)) + value := c.Params(param) + if value != "" { + params[param] = append(params[param], value) + } } return c.parseToStruct(paramsTag, out, params) } @@ -1342,7 +1353,10 @@ func (c *Ctx) ReqHeaderParser(out interface{}) error { return c.parseToStruct(reqHeaderTag, out, data) } -func (*Ctx) parseToStruct(aliasTag string, out interface{}, data map[string][]string) error { +func (c *Ctx) parseToStruct(aliasTag string, out interface{}, data map[string][]string) error { + if c.app.config.DefaultValueParser { + utils.SetDefaultValues(out) + } // Get decoder from pool schemaDecoder, ok := decoderPoolMap[aliasTag].Get().(*schema.Decoder) if !ok { diff --git a/ctx_test.go b/ctx_test.go index ea9dfa8485c..c153754a1ad 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -36,6 +36,10 @@ import ( "github.com/valyala/fasthttp" ) +const ( + cookieName = "Joseph" +) + // go test -run Test_Ctx_Accepts func Test_Ctx_Accepts(t *testing.T) { t.Parallel() @@ -987,6 +991,205 @@ func Test_Ctx_CookieParser(t *testing.T) { utils.AssertEqual(t, 0, len(empty.Courses)) } +func Test_Ctx_ParserWithDefaultValues(t *testing.T) { + t.Parallel() + // setup + app := New(Config{EnableSplittingOnParsers: true, DefaultValueParser: true}) + + type TestStruct struct { + Name string + Class int + NameWithDefault string `json:"name2" xml:"Name2" form:"name2" cookie:"name2" query:"name2" params:"name2" reqHeader:"name2" default:"doe"` + ClassWithDefault int `json:"class2" xml:"Class2" form:"class2" cookie:"class2" query:"class2" params:"class2" reqHeader:"class2" default:"10"` + } + + withValues := func(t *testing.T, actionFn func(c *Ctx, testStruct *TestStruct) error) { + t.Helper() + c := app.AcquireCtx(&fasthttp.RequestCtx{}) + defer app.ReleaseCtx(c) + testStruct := new(TestStruct) + + utils.AssertEqual(t, nil, actionFn(c, testStruct)) + utils.AssertEqual(t, "foo", testStruct.Name) + utils.AssertEqual(t, 111, testStruct.Class) + utils.AssertEqual(t, "bar", testStruct.NameWithDefault) + utils.AssertEqual(t, 222, testStruct.ClassWithDefault) + } + withoutValues := func(t *testing.T, actionFn func(c *Ctx, testStruct *TestStruct) error) { + t.Helper() + c := app.AcquireCtx(&fasthttp.RequestCtx{}) + defer app.ReleaseCtx(c) + testStruct := new(TestStruct) + + utils.AssertEqual(t, nil, actionFn(c, testStruct)) + utils.AssertEqual(t, "", testStruct.Name) + utils.AssertEqual(t, 0, testStruct.Class) + utils.AssertEqual(t, "doe", testStruct.NameWithDefault) + utils.AssertEqual(t, 10, testStruct.ClassWithDefault) + } + + t.Run("BodyParser:xml", func(t *testing.T) { + t.Parallel() + t.Run("withValues", func(t *testing.T) { + t.Parallel() + withValues(t, func(c *Ctx, testStruct *TestStruct) error { + c.Request().Header.SetContentType(MIMEApplicationXML) + c.Request().SetBody([]byte(`foo111bar222`)) + return c.BodyParser(testStruct) + }) + }) + t.Run("withoutValues", func(t *testing.T) { + t.Parallel() + withoutValues(t, func(c *Ctx, testStruct *TestStruct) error { + c.Request().Header.SetContentType(MIMEApplicationXML) + c.Request().SetBody([]byte(``)) + return c.BodyParser(testStruct) + }) + }) + }) + t.Run("BodyParser:form", func(t *testing.T) { + t.Parallel() + t.Run("withValues", func(t *testing.T) { + t.Parallel() + withValues(t, func(c *Ctx, testStruct *TestStruct) error { + c.Request().Header.SetContentType(MIMEApplicationForm) + c.Request().SetBody([]byte(`name=foo&class=111&name2=bar&class2=222`)) + return c.BodyParser(testStruct) + }) + }) + t.Run("withoutValues", func(t *testing.T) { + t.Parallel() + withoutValues(t, func(c *Ctx, testStruct *TestStruct) error { + c.Request().Header.SetContentType(MIMEApplicationForm) + c.Request().SetBody([]byte(``)) + + return c.BodyParser(testStruct) + }) + }) + }) + t.Run("BodyParser:json", func(t *testing.T) { + t.Parallel() + t.Run("withValues", func(t *testing.T) { + t.Parallel() + withValues(t, func(c *Ctx, testStruct *TestStruct) error { + c.Request().Header.SetContentType(MIMEApplicationJSON) + c.Request().SetBody([]byte(`{"name":"foo","class":111,"name2":"bar","class2":222}`)) + return c.BodyParser(testStruct) + }) + }) + t.Run("withoutValues", func(t *testing.T) { + t.Parallel() + withoutValues(t, func(c *Ctx, testStruct *TestStruct) error { + c.Request().Header.SetContentType(MIMEApplicationJSON) + c.Request().SetBody([]byte(`{}`)) + + return c.BodyParser(testStruct) + }) + }) + }) + t.Run("BodyParser:multiform", func(t *testing.T) { + t.Parallel() + t.Run("withValues", func(t *testing.T) { + t.Parallel() + withValues(t, func(c *Ctx, testStruct *TestStruct) error { + body := []byte("--b\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\nfoo\r\n--b\r\nContent-Disposition: form-data; name=\"class\"\r\n\r\n111\r\n--b\r\nContent-Disposition: form-data; name=\"name2\"\r\n\r\nbar\r\n--b\r\nContent-Disposition: form-data; name=\"class2\"\r\n\r\n222\r\n--b--") + c.Request().SetBody(body) + c.Request().Header.SetContentType(MIMEMultipartForm + `;boundary="b"`) + c.Request().Header.SetContentLength(len(body)) + return c.BodyParser(testStruct) + }) + }) + t.Run("withoutValues", func(t *testing.T) { + t.Parallel() + withoutValues(t, func(c *Ctx, testStruct *TestStruct) error { + body := []byte("--b\n\n--b--") + c.Request().SetBody(body) + c.Request().Header.SetContentType(MIMEMultipartForm + `;boundary="b"`) + c.Request().Header.SetContentLength(len(body)) + + return c.BodyParser(testStruct) + }) + }) + }) + t.Run("CookieParser", func(t *testing.T) { + t.Parallel() + t.Run("withValues", func(t *testing.T) { + t.Parallel() + withValues(t, func(c *Ctx, testStruct *TestStruct) error { + c.Request().Header.Set("Cookie", "name=foo;name2=bar;class=111;class2=222") + return c.CookieParser(testStruct) + }) + }) + t.Run("withoutValues", func(t *testing.T) { + t.Parallel() + withoutValues(t, func(c *Ctx, testStruct *TestStruct) error { + return c.CookieParser(testStruct) + }) + }) + }) + t.Run("QueryParser", func(t *testing.T) { + t.Parallel() + t.Run("withValues", func(t *testing.T) { + t.Parallel() + withValues(t, func(c *Ctx, testStruct *TestStruct) error { + c.Request().URI().SetQueryString("name=foo&name2=bar&class=111&class2=222") + return c.QueryParser(testStruct) + }) + }) + t.Run("withoutValues", func(t *testing.T) { + t.Parallel() + withoutValues(t, func(c *Ctx, testStruct *TestStruct) error { + return c.QueryParser(testStruct) + }) + }) + }) + t.Run("ParamsParser", func(t *testing.T) { + t.Parallel() + t.Run("withValues", func(t *testing.T) { + t.Parallel() + withValues(t, func(c *Ctx, testStruct *TestStruct) error { + c.route = &Route{Params: []string{"name", "name2", "class", "class2"}} + c.values = [30]string{"foo", "bar", "111", "222"} + return c.ParamsParser(testStruct) + }) + }) + t.Run("withoutValues", func(t *testing.T) { + t.Parallel() + // no params declared in route + withoutValues(t, func(c *Ctx, testStruct *TestStruct) error { + c.route = &Route{Params: []string{}} + c.values = [30]string{} + return c.ParamsParser(testStruct) + }) + // no values for route + withoutValues(t, func(c *Ctx, testStruct *TestStruct) error { + c.route = &Route{Params: []string{"name", "name2", "class", "class2"}} + c.values = [30]string{"", "", "", ""} + return c.ParamsParser(testStruct) + }) + }) + }) + t.Run("ReqHeaderParser", func(t *testing.T) { + t.Parallel() + t.Run("withValues", func(t *testing.T) { + t.Parallel() + withValues(t, func(c *Ctx, testStruct *TestStruct) error { + c.Request().Header.Add("name", "foo") + c.Request().Header.Add("name2", "bar") + c.Request().Header.Add("class", "111") + c.Request().Header.Add("class2", "222") + return c.ReqHeaderParser(testStruct) + }) + }) + t.Run("withoutValues", func(t *testing.T) { + t.Parallel() + withoutValues(t, func(c *Ctx, testStruct *TestStruct) error { + return c.ReqHeaderParser(testStruct) + }) + }) + }) +} + // go test -run Test_Ctx_CookieParserUsingTag -v func Test_Ctx_CookieParserUsingTag(t *testing.T) { t.Parallel() @@ -1002,8 +1205,8 @@ func Test_Ctx_CookieParserUsingTag(t *testing.T) { Grades []uint8 `cookie:"score"` } cookie1 := new(Cook) - cookie1.Name = "Joseph" - utils.AssertEqual(t, "Joseph", cookie1.Name) + cookie1.Name = cookieName + utils.AssertEqual(t, cookieName, cookie1.Name) c.Request().Header.Set("Cookie", "id=1") c.Request().Header.Set("Cookie", "name=Joey") @@ -1049,7 +1252,7 @@ func Test_Ctx_CookieParser_Schema(t *testing.T) { Result result `cookie:"result"` } res := &resStruct{ - Name: "Joseph", + Name: cookieName, Age: 10, Result: result{ Maths: 10, @@ -1083,7 +1286,7 @@ func Benchmark_Ctx_CookieParser(b *testing.B) { Grades []uint8 `cookie:"score"` } cookie1 := new(Cook) - cookie1.Name = "Joseph" + cookie1.Name = cookieName c.Request().Header.Set("Cookie", "id=1") c.Request().Header.Set("Cookie", "name=Joey") @@ -1100,6 +1303,33 @@ func Benchmark_Ctx_CookieParser(b *testing.B) { utils.AssertEqual(b, nil, err) } +func Benchmark_Ctx_BodyParserJsonWithDefaultValues(b *testing.B) { + b.ReportAllocs() + b.ResetTimer() + + app := New(Config{DefaultValueParser: true}) + c := app.AcquireCtx(&fasthttp.RequestCtx{}) + defer app.ReleaseCtx(c) + type TestStruct struct { + Name string + NameWithDefault string `json:"name2" default:"doe"` + NameWithDefaultNoValue string `json:"name3" default:"doe"` + } + c.Request().Header.SetContentType(MIMEApplicationJSON) + c.Request().SetBody([]byte(`{"name":"foo","name2":"bar"}`)) + + var err error + testStruct := new(TestStruct) + // Run the function b.N times + for i := 0; i < b.N; i++ { + err = c.BodyParser(testStruct) + } + utils.AssertEqual(b, nil, err) + utils.AssertEqual(b, "foo", testStruct.Name) + utils.AssertEqual(b, "bar", testStruct.NameWithDefault) + utils.AssertEqual(b, "doe", testStruct.NameWithDefaultNoValue) +} + // go test -run Test_Ctx_Cookies func Test_Ctx_Cookies(t *testing.T) { t.Parallel() diff --git a/docs/api/ctx.md b/docs/api/ctx.md index 2c3244926f0..fdc2fc64399 100644 --- a/docs/api/ctx.md +++ b/docs/api/ctx.md @@ -313,6 +313,10 @@ app.Post("/", func(c *fiber.Ctx) error { // curl -X POST "http://localhost:3000/?name=john&pass=doe" ``` +:::note +It supports the `DefaultValueParser` feature, which allows for the assignment of default values to struct fields when specific request parameters are missing. For more details, see the [default values documentation](./fiber.md#default-values). +::: + > _Returned value is only valid within the handler. Do not store any references. > Make copies or use the_ [_**`Immutable`**_](ctx.md) _setting instead._ [_Read more..._](../#zero-allocation) @@ -466,6 +470,13 @@ app.Get("/", func(c *fiber.Ctx) error { // curl.exe --cookie "name=Joseph; age=23; job=true" http://localhost:8000/ ``` +:::note +It supports the `DefaultValueParser` feature, which allows for the assignment of default values to struct fields when specific request parameters are missing. For more details, see the [default values documentation](./fiber.md#default-values). +::: + +> _Returned value is only valid within the handler. Do not store any references. +> Make copies or use the_ [_**`Immutable`**_](ctx.md) _setting instead._ [_Read more..._](../#zero-allocation) + ## Cookies Get cookie value by key, you could pass an optional default value that will be returned if the cookie key does not exist. @@ -1126,7 +1137,7 @@ This method is equivalent of using `atoi` with ctx.Params ## ParamsParser -This method is similar to BodyParser, but for path parameters. It is important to use the struct tag "params". For example, if you want to parse a path parameter with a field called Pass, you would use a struct field of params:"pass" +This method is similar to [BodyParser](ctx.md#bodyparser), but for path parameters. It is important to use the struct tag "params". For example, if you want to parse a path parameter with a field called Pass, you would use a struct field of params:"pass" ```go title="Signature" func (c *Ctx) ParamsParser(out interface{}) error @@ -1144,6 +1155,13 @@ app.Get("/user/:id", func(c *fiber.Ctx) error { ``` +:::note +It supports the `DefaultValueParser` feature, which allows for the assignment of default values to struct fields when specific request parameters are missing. For more details, see the [default values documentation](./fiber.md#default-values). +::: + +> _Returned value is only valid within the handler. Do not store any references. +> Make copies or use the_ [_**`Immutable`**_](ctx.md) _setting instead._ [_Read more..._](../#zero-allocation) + ## Path Contains the path part of the request URL. Optionally, you could override the path by passing a string. For internal redirects, you might want to call [RestartRouting](ctx.md#restartrouting) instead of [Next](ctx.md#next). @@ -1398,6 +1416,13 @@ app.Get("/", func(c *fiber.Ctx) error { // curl "http://localhost:3000/?name=john&pass=doe&products=shoe,hat" ``` +:::note +It supports the `DefaultValueParser` feature, which allows for the assignment of default values to struct fields when specific request parameters are missing. For more details, see the [default values documentation](./fiber.md#default-values). +::: + +> _Returned value is only valid within the handler. Do not store any references. +> Make copies or use the_ [_**`Immutable`**_](ctx.md) _setting instead._ [_Read more..._](../#zero-allocation) + ## Range A struct containing the type and a slice of ranges will be returned. @@ -1571,6 +1596,13 @@ app.Get("/", func(c *fiber.Ctx) error { // curl "http://localhost:3000/" -H "name: john" -H "pass: doe" -H "products: shoe,hat" ``` +:::note +It supports the `DefaultValueParser` feature, which allows for the assignment of default values to struct fields when specific request parameters are missing. For more details, see the [default values documentation](./fiber.md#default-values). +::: + +> _Returned value is only valid within the handler. Do not store any references. +> Make copies or use the_ [_**`Immutable`**_](ctx.md) _setting instead._ [_Read more..._](../#zero-allocation) + ## Response Response return the [\*fasthttp.Response](https://godoc.org/github.com/valyala/fasthttp#Response) pointer diff --git a/docs/api/fiber.md b/docs/api/fiber.md index 1f9a91b8bf6..d811d8ef110 100644 --- a/docs/api/fiber.md +++ b/docs/api/fiber.md @@ -37,50 +37,88 @@ app := fiber.New(fiber.Config{ // ... ``` -**Config fields** +### Config fields -| Property | Type | Description | Default | -| ---------------------------- | --------------------- |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| --------------------- | -| AppName | `string` | This allows to setup app name for the app | `""` | -| BodyLimit | `int` | Sets the maximum allowed size for a request body, if the size exceeds the configured limit, it sends `413 - Request Entity Too Large` response. | `4 * 1024 * 1024` | -| CaseSensitive | `bool` | When enabled, `/Foo` and `/foo` are different routes. When disabled, `/Foo`and `/foo` are treated the same. | `false` | +| Property | Type | Description | Default | +|------------------------------|-------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------| +| AppName | `string` | This allows to setup app name for the app | `""` | +| BodyLimit | `int` | Sets the maximum allowed size for a request body, if the size exceeds the configured limit, it sends `413 - Request Entity Too Large` response. | `4 * 1024 * 1024` | +| CaseSensitive | `bool` | When enabled, `/Foo` and `/foo` are different routes. When disabled, `/Foo`and `/foo` are treated the same. | `false` | | ColorScheme | [`Colors`](https://github.com/gofiber/fiber/blob/master/color.go) | You can define custom color scheme. They'll be used for startup message, route list and some middlewares. | [`DefaultColors`](https://github.com/gofiber/fiber/blob/master/color.go) | -| CompressedFileSuffix | `string` | Adds a suffix to the original file name and tries saving the resulting compressed file under the new file name. | `".fiber.gz"` | -| Concurrency | `int` | Maximum number of concurrent connections. | `256 * 1024` | -| DisableDefaultContentType | `bool` | When set to true, causes the default Content-Type header to be excluded from the Response. | `false` | -| DisableDefaultDate | `bool` | When set to true causes the default date header to be excluded from the response. | `false` | -| DisableHeaderNormalizing | `bool` | By default all header names are normalized: conteNT-tYPE -> Content-Type | `false` | -| DisableKeepalive | `bool` | Disable keep-alive connections, the server will close incoming connections after sending the first response to the client | `false` | -| DisablePreParseMultipartForm | `bool` | Will not pre parse Multipart Form data if set to true. This option is useful for servers that desire to treat multipart form data as a binary blob, or choose when to parse the data. | `false` | -| DisableStartupMessage | `bool` | When set to true, it will not print out debug information | `false` | -| ETag | `bool` | Enable or disable ETag header generation, since both weak and strong etags are generated using the same hashing method \(CRC-32\). Weak ETags are the default when enabled. | `false` | -| EnableIPValidation | `bool` | If set to true, `c.IP()` and `c.IPs()` will validate IP addresses before returning them. Also, `c.IP()` will return only the first valid IP rather than just the raw header value that may be a comma seperated string.

**WARNING:** There is a small performance cost to doing this validation. Keep disabled if speed is your only concern and your application is behind a trusted proxy that already validates this header. | `false` | -| EnablePrintRoutes | `bool` | EnablePrintRoutes enables print all routes with their method, path, name and handler.. | `false` | -| EnableSplittingOnParsers | `bool` | EnableSplittingOnParsers splits the query/body/header parameters by comma when it's true.

For example, you can use it to parse multiple values from a query parameter like this: `/api?foo=bar,baz == foo[]=bar&foo[]=baz` | `false` | -| EnableTrustedProxyCheck | `bool` | When set to true, fiber will check whether proxy is trusted, using TrustedProxies list.

By default `c.Protocol()` will get value from X-Forwarded-Proto, X-Forwarded-Protocol, X-Forwarded-Ssl or X-Url-Scheme header, `c.IP()` will get value from `ProxyHeader` header, `c.Hostname()` will get value from X-Forwarded-Host header.
If `EnableTrustedProxyCheck` is true, and `RemoteIP` is in the list of `TrustedProxies` `c.Protocol()`, `c.IP()`, and `c.Hostname()` will have the same behaviour when `EnableTrustedProxyCheck` disabled, if `RemoteIP` isn't in the list, `c.Protocol()` will return https in case when tls connection is handled by the app, or http otherwise, `c.IP()` will return RemoteIP() from fasthttp context, `c.Hostname()` will return `fasthttp.Request.URI().Host()` | `false` | -| ErrorHandler | `ErrorHandler` | ErrorHandler is executed when an error is returned from fiber.Handler. Mounted fiber error handlers are retained by the top-level app and applied on prefix associated requests. | `DefaultErrorHandler` | -| GETOnly | `bool` | Rejects all non-GET requests if set to true. This option is useful as anti-DoS protection for servers accepting only GET requests. The request size is limited by ReadBufferSize if GETOnly is set. | `false` | -| IdleTimeout | `time.Duration` | The maximum amount of time to wait for the next request when keep-alive is enabled. If IdleTimeout is zero, the value of ReadTimeout is used. | `nil` | -| Immutable | `bool` | When enabled, all values returned by context methods are immutable. By default, they are valid until you return from the handler; see issue [\#185](https://github.com/gofiber/fiber/issues/185). | `false` | -| JSONDecoder | `utils.JSONUnmarshal` | Allowing for flexibility in using another json library for decoding. | `json.Unmarshal` | -| JSONEncoder | `utils.JSONMarshal` | Allowing for flexibility in using another json library for encoding. | `json.Marshal` | -| Network | `string` | Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only)

**WARNING:** When prefork is set to true, only "tcp4" and "tcp6" can be chosen. | `NetworkTCP4` | -| PassLocalsToViews | `bool` | PassLocalsToViews Enables passing of the locals set on a fiber.Ctx to the template engine. See our **Template Middleware** for supported engines. | `false` | -| Prefork | `bool` | Enables use of the[`SO_REUSEPORT`](https://lwn.net/Articles/542629/)socket option. This will spawn multiple Go processes listening on the same port. learn more about [socket sharding](https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/). **NOTE: if enabled, the application will need to be ran through a shell because prefork mode sets environment variables. If you're using Docker, make sure the app is ran with `CMD ./app` or `CMD ["sh", "-c", "/app"]`. For more info, see** [**this**](https://github.com/gofiber/fiber/issues/1021#issuecomment-730537971) **issue comment.** | `false` | -| ProxyHeader | `string` | This will enable `c.IP()` to return the value of the given header key. By default `c.IP()`will return the Remote IP from the TCP connection, this property can be useful if you are behind a load balancer e.g. _X-Forwarded-\*_. | `""` | -| ReadBufferSize | `int` | per-connection buffer size for requests' reading. This also limits the maximum header size. Increase this buffer if your clients send multi-KB RequestURIs and/or multi-KB headers \(for example, BIG cookies\). | `4096` | -| ReadTimeout | `time.Duration` | The amount of time allowed to read the full request, including the body. The default timeout is unlimited. | `nil` | -| RequestMethods | `[]string` | RequestMethods provides customizibility for HTTP methods. You can add/remove methods as you wish. | `DefaultMethods` | -| ServerHeader | `string` | Enables the `Server` HTTP header with the given value. | `""` | -| StreamRequestBody | `bool` | StreamRequestBody enables request body streaming, and calls the handler sooner when given body is larger then the current limit. | `false` | -| StrictRouting | `bool` | When enabled, the router treats `/foo` and `/foo/` as different. Otherwise, the router treats `/foo` and `/foo/` as the same. | `false` | -| TrustedProxies | `[]string` | Contains the list of trusted proxy IP's. Look at `EnableTrustedProxyCheck` doc.

It can take IP or IP range addresses. If it gets IP range, it iterates all possible addresses. | `[]string*__*` | -| UnescapePath | `bool` | Converts all encoded characters in the route back before setting the path for the context, so that the routing can also work with URL encoded special characters | `false` | -| Views | `Views` | Views is the interface that wraps the Render function. See our **Template Middleware** for supported engines. | `nil` | -| ViewsLayout | `string` | Views Layout is the global layout for all template render until override on Render function. See our **Template Middleware** for supported engines. | `""` | -| WriteBufferSize | `int` | Per-connection buffer size for responses' writing. | `4096` | -| WriteTimeout | `time.Duration` | The maximum duration before timing out writes of the response. The default timeout is unlimited. | `nil` | -| XMLEncoder | `utils.XMLMarshal` | Allowing for flexibility in using another XML library for encoding. | `xml.Marshal` | +| CompressedFileSuffix | `string` | Adds a suffix to the original file name and tries saving the resulting compressed file under the new file name. | `".fiber.gz"` | +| Concurrency | `int` | Maximum number of concurrent connections. | `256 * 1024` | +| DefaultValueParser | `bool` | When set to true, this will enable support for default value tags across all parsers, allowing the specification of default values for inputs in query, body, route, request header, and cookie parsers. | `false` | +| DisableDefaultContentType | `bool` | When set to true, causes the default Content-Type header to be excluded from the Response. | `false` | +| DisableDefaultDate | `bool` | When set to true causes the default date header to be excluded from the response. | `false` | +| DisableHeaderNormalizing | `bool` | By default all header names are normalized: conteNT-tYPE -> Content-Type | `false` | +| DisableKeepalive | `bool` | Disable keep-alive connections, the server will close incoming connections after sending the first response to the client | `false` | +| DisablePreParseMultipartForm | `bool` | Will not pre parse Multipart Form data if set to true. This option is useful for servers that desire to treat multipart form data as a binary blob, or choose when to parse the data. | `false` | +| DisableStartupMessage | `bool` | When set to true, it will not print out debug information | `false` | +| EnableIPValidation | `bool` | If set to true, `c.IP()` and `c.IPs()` will validate IP addresses before returning them. Also, `c.IP()` will return only the first valid IP rather than just the raw header value that may be a comma seperated string.

**WARNING:** There is a small performance cost to doing this validation. Keep disabled if speed is your only concern and your application is behind a trusted proxy that already validates this header. | `false` | +| EnablePrintRoutes | `bool` | EnablePrintRoutes enables print all routes with their method, path, name and handler.. | `false` | +| EnableSplittingOnParsers | `bool` | EnableSplittingOnParsers splits the query/body/header parameters by comma when it's true.

For example, you can use it to parse multiple values from a query parameter like this: `/api?foo=bar,baz == foo[]=bar&foo[]=baz` | `false` | +| EnableTrustedProxyCheck | `bool` | When set to true, fiber will check whether proxy is trusted, using TrustedProxies list.

By default `c.Protocol()` will get value from X-Forwarded-Proto, X-Forwarded-Protocol, X-Forwarded-Ssl or X-Url-Scheme header, `c.IP()` will get value from `ProxyHeader` header, `c.Hostname()` will get value from X-Forwarded-Host header.
If `EnableTrustedProxyCheck` is true, and `RemoteIP` is in the list of `TrustedProxies` `c.Protocol()`, `c.IP()`, and `c.Hostname()` will have the same behaviour when `EnableTrustedProxyCheck` disabled, if `RemoteIP` isn't in the list, `c.Protocol()` will return https in case when tls connection is handled by the app, or http otherwise, `c.IP()` will return RemoteIP() from fasthttp context, `c.Hostname()` will return `fasthttp.Request.URI().Host()` | `false` | +| ErrorHandler | `ErrorHandler` | ErrorHandler is executed when an error is returned from fiber.Handler. Mounted fiber error handlers are retained by the top-level app and applied on prefix associated requests. | `DefaultErrorHandler` | +| ETag | `bool` | Enable or disable ETag header generation, since both weak and strong etags are generated using the same hashing method \(CRC-32\). Weak ETags are the default when enabled. | `false` | +| GETOnly | `bool` | Rejects all non-GET requests if set to true. This option is useful as anti-DoS protection for servers accepting only GET requests. The request size is limited by ReadBufferSize if GETOnly is set. | `false` | +| IdleTimeout | `time.Duration` | The maximum amount of time to wait for the next request when keep-alive is enabled. If IdleTimeout is zero, the value of ReadTimeout is used. | `nil` | +| Immutable | `bool` | When enabled, all values returned by context methods are immutable. By default, they are valid until you return from the handler; see issue [\#185](https://github.com/gofiber/fiber/issues/185). | `false` | +| JSONDecoder | `utils.JSONUnmarshal` | Allowing for flexibility in using another json library for decoding. | `json.Unmarshal` | +| JSONEncoder | `utils.JSONMarshal` | Allowing for flexibility in using another json library for encoding. | `json.Marshal` | +| Network | `string` | Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only)

**WARNING:** When prefork is set to true, only "tcp4" and "tcp6" can be chosen. | `NetworkTCP4` | +| PassLocalsToViews | `bool` | PassLocalsToViews Enables passing of the locals set on a fiber.Ctx to the template engine. See our **Template Middleware** for supported engines. | `false` | +| Prefork | `bool` | Enables use of the[`SO_REUSEPORT`](https://lwn.net/Articles/542629/)socket option. This will spawn multiple Go processes listening on the same port. learn more about [socket sharding](https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/). **NOTE: if enabled, the application will need to be ran through a shell because prefork mode sets environment variables. If you're using Docker, make sure the app is ran with `CMD ./app` or `CMD ["sh", "-c", "/app"]`. For more info, see** [**this**](https://github.com/gofiber/fiber/issues/1021#issuecomment-730537971) **issue comment.** | `false` | +| ProxyHeader | `string` | This will enable `c.IP()` to return the value of the given header key. By default `c.IP()`will return the Remote IP from the TCP connection, this property can be useful if you are behind a load balancer e.g. _X-Forwarded-\*_. | `""` | +| ReadBufferSize | `int` | per-connection buffer size for requests' reading. This also limits the maximum header size. Increase this buffer if your clients send multi-KB RequestURIs and/or multi-KB headers \(for example, BIG cookies\). | `4096` | +| ReadTimeout | `time.Duration` | The amount of time allowed to read the full request, including the body. The default timeout is unlimited. | `nil` | +| RequestMethods | `[]string` | RequestMethods provides customizibility for HTTP methods. You can add/remove methods as you wish. | `DefaultMethods` | +| ServerHeader | `string` | Enables the `Server` HTTP header with the given value. | `""` | +| StreamRequestBody | `bool` | StreamRequestBody enables request body streaming, and calls the handler sooner when given body is larger then the current limit. | `false` | +| StrictRouting | `bool` | When enabled, the router treats `/foo` and `/foo/` as different. Otherwise, the router treats `/foo` and `/foo/` as the same. | `false` | +| TrustedProxies | `[]string` | Contains the list of trusted proxy IP's. Look at `EnableTrustedProxyCheck` doc.

It can take IP or IP range addresses. If it gets IP range, it iterates all possible addresses. | `[]string*__*` | +| UnescapePath | `bool` | Converts all encoded characters in the route back before setting the path for the context, so that the routing can also work with URL encoded special characters | `false` | +| Views | `Views` | Views is the interface that wraps the Render function. See our **Template Middleware** for supported engines. | `nil` | +| ViewsLayout | `string` | Views Layout is the global layout for all template render until override on Render function. See our **Template Middleware** for supported engines. | `""` | +| WriteBufferSize | `int` | Per-connection buffer size for responses' writing. | `4096` | +| WriteTimeout | `time.Duration` | The maximum duration before timing out writes of the response. The default timeout is unlimited. | `nil` | +| XMLEncoder | `utils.XMLMarshal` | Allowing for flexibility in using another XML library for encoding. | `xml.Marshal` | + + +#### Default Values + +This [config](#config-fields) feature `DefaultValueParser` enables you to assign default values to struct fields for situations where certain parameters are missing in requests. The `default` struct tag can be utilized across various parsers, including [BodyParser](ctx.md#BodyParser), [CookieParser](ctx.md#CookieParser), [QueryParser](ctx.md#QueryParser), [ParamsParser](ctx.md#ParamsParser), and [ReqHeaderParser](ctx.md#ReqHeaderParser). This tag works in tandem with the respective parser tag to ensure default values are set when specific parameters are not provided. + +```go title="WithDefaultValues" +// Example demonstrating the use of default values in query parser +type PersonWithDefaults struct { + Name string `query:"name" default:"DefaultName"` + Pass string `query:"pass" default:"DefaultPass"` + Products []string `query:"products" default:"defaultProduct1,defaultProduct2"` +} + +app := fiber.New(fiber.Config{ + DefaultValueParser: true, +}) + +app.Get("/defaults", func(c *fiber.Ctx) error { + p := new(PersonWithDefaults) + + if err := c.QueryParser(p); err != nil { + return err + } + + log.Println(p.Name) // Will print "DefaultName" if name is not provided in the query + log.Println(p.Pass) // Will print "DefaultPass" if pass is not provided in the query + log.Println(p.Products) // Will print [defaultProduct1, defaultProduct2] if products is not provided in the query + + // ... +}) +// Run tests with the following curl command + +// curl "http://localhost:3000/defaults" +// This will use the default values since no query parameters are provided + +``` ## NewError diff --git a/utils/default.go b/utils/default.go new file mode 100644 index 00000000000..44721a915a8 --- /dev/null +++ b/utils/default.go @@ -0,0 +1,145 @@ +package utils + +import ( + "math" + "reflect" + "strconv" + "strings" + "sync" +) + +var ( + mu sync.RWMutex + structCache = make(map[reflect.Type][]reflect.StructField) +) + +const ( + tagName = "default" +) + +func tagHandlers(field reflect.Value, tagValue string) { + mu.Lock() + defer mu.Unlock() + + //nolint:exhaustive // We don't need to handle all types + switch field.Kind() { + case reflect.String: + if field.String() == "" { + field.SetString(tagValue) + } + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + if field.Int() == 0 { + if i, err := strconv.ParseInt(tagValue, 10, 64); err == nil { + field.SetInt(i) + } + } + case reflect.Float32, reflect.Float64: + if field.Float() == 0.0 { + if f, err := strconv.ParseFloat(tagValue, 64); err == nil { + field.SetFloat(f) + } + } + case reflect.Bool: + if !field.Bool() { + if b, err := strconv.ParseBool(tagValue); err == nil { + field.SetBool(b) + } + } + case reflect.Slice: + setDefaultForSlice(field, tagValue, field.Type().Elem()) + } +} + +func setDefaultForSlice(field reflect.Value, tagValue string, elemType reflect.Type) { + // TODO: produce deadlock because of mutex hirachy + //mu.Lock() + //defer mu.Unlock() + + items := strings.Split(tagValue, ",") + slice := reflect.MakeSlice(reflect.SliceOf(elemType), 0, len(items)) + for _, item := range items { + var val reflect.Value + //nolint:exhaustive // We don't need to handle all types + switch elemType.Kind() { + case reflect.Ptr: + elemKind := elemType.Elem().Kind() + //nolint:exhaustive // We don't need to handle all types + switch elemKind { + case reflect.String: + strVal := item + val = reflect.ValueOf(&strVal) + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + if intVal, err := strconv.ParseInt(item, 10, 64); err == nil { + intPtr := reflect.New(elemType.Elem()) + intPtr.Elem().SetInt(intVal) + val = intPtr + } + } + case reflect.String: + val = reflect.ValueOf(item) + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + if intVal, err := strconv.ParseInt(item, 10, 64); err == nil { + switch elemType.Kind() { + case reflect.Int: + if strconv.IntSize == 64 && (intVal >= int64(math.MinInt32) && intVal <= int64(math.MaxInt32)) { + val = reflect.ValueOf(int(intVal)) + } + case reflect.Int8: + if intVal >= int64(math.MinInt8) && intVal <= int64(math.MaxInt8) { + val = reflect.ValueOf(int8(intVal)) + } + case reflect.Int16: + if intVal >= int64(math.MinInt16) && intVal <= int64(math.MaxInt16) { + val = reflect.ValueOf(int16(intVal)) + } + case reflect.Int32: + if intVal >= int64(math.MinInt32) && intVal <= int64(math.MaxInt32) { + val = reflect.ValueOf(int32(intVal)) + } + case reflect.Int64: + val = reflect.ValueOf(intVal) + } + } + } + if val.IsValid() { + slice = reflect.Append(slice, val) + } + } + + field.Set(slice) +} + +func getFieldsWithDefaultTag(t reflect.Type) []reflect.StructField { + mu.RLock() + fields, ok := structCache[t] + mu.RUnlock() + if ok { + return fields + } + + var newFields []reflect.StructField + for i := 0; i < t.NumField(); i++ { + field := t.Field(i) + if _, ok := field.Tag.Lookup(tagName); ok { + newFields = append(newFields, field) + } + } + + mu.Lock() + structCache[t] = newFields + mu.Unlock() + + return newFields +} + +func SetDefaultValues(out interface{}) { + elem := reflect.ValueOf(out).Elem() + typ := elem.Type() + + fields := getFieldsWithDefaultTag(typ) + for _, fieldInfo := range fields { + field := elem.FieldByName(fieldInfo.Name) + tagValue := fieldInfo.Tag.Get(tagName) + tagHandlers(field, tagValue) + } +} diff --git a/utils/default_test.go b/utils/default_test.go new file mode 100644 index 00000000000..9fbc469ab29 --- /dev/null +++ b/utils/default_test.go @@ -0,0 +1,195 @@ +package utils + +import ( + "reflect" + "testing" +) + +type TestStruct struct { + Name string `default:"John"` + Age int `default:"25"` + Height float64 `default:"5.9"` + IsActive bool `default:"true"` + Friends []string `default:"Alice,Bob"` +} + +func TestSetDefaultValues(t *testing.T) { + tests := []struct { + name string + input *TestStruct + expected *TestStruct + }{ + { + name: "All fields empty", + input: &TestStruct{}, + expected: &TestStruct{Name: "John", Age: 25, Height: 5.9, IsActive: true, Friends: []string{"Alice", "Bob"}}, + }, + { + name: "Some fields set", + input: &TestStruct{Name: "Doe", Age: 0, Height: 0.0, IsActive: false, Friends: nil}, + expected: &TestStruct{Name: "Doe", Age: 25, Height: 5.9, IsActive: true, Friends: []string{"Alice", "Bob"}}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + SetDefaultValues(tt.input) + AssertEqual(t, tt.input, tt.expected, "SetDefaultValues failed") + }) + } +} + +type TestSecondLevelStruct struct { + Word string `default:"Bar"` + Number int `default:"42"` +} + +type TestFirstLevelStruct struct { + Word string `default:"Foo"` + Number int `default:"42"` + DeepStruct *TestSecondLevelStruct + DeepSlice []*TestSecondLevelStruct +} + +func TestDeepSetDefaultValues(t *testing.T) { + t.Parallel() + subject := &TestFirstLevelStruct{DeepStruct: &TestSecondLevelStruct{}, DeepSlice: []*TestSecondLevelStruct{&TestSecondLevelStruct{}, &TestSecondLevelStruct{}}} + SetDefaultValues(subject) + + AssertEqual( + t, + &TestFirstLevelStruct{ + Word: "Foo", + Number: 42, + DeepStruct: &TestSecondLevelStruct{ + Word: "Bar", + Number: 42, + }, + DeepSlice: []*TestSecondLevelStruct{ + &TestSecondLevelStruct{ + Word: "Bar", + Number: 42, + }, + &TestSecondLevelStruct{ + Word: "Bar", + Number: 42, + }, + }, + }, + subject, + "SetDefaultValues failed", + ) +} + +func TestTagHandlers(t *testing.T) { + t.Parallel() + tests := []struct { + name string + field reflect.Value + tagValue string + expected interface{} + }{ + { + name: "String field with default value", + field: reflect.ValueOf(new(string)).Elem(), + tagValue: "test", + expected: "test", + }, + { + name: "Int field with default value", + field: reflect.ValueOf(new(int)).Elem(), + tagValue: "42", + expected: 42, + }, + { + name: "Float64 field with default value", + field: reflect.ValueOf(new(float64)).Elem(), + tagValue: "3.14", + expected: 3.14, + }, + { + name: "Bool field with default value", + field: reflect.ValueOf(new(bool)).Elem(), + tagValue: "true", + expected: true, + }, + { + name: "Slice of strings with default value", + field: reflect.ValueOf(new([]string)).Elem(), + tagValue: "apple,1", + expected: []string{"apple", "1"}, + }, + { + name: "Slice of ints with default value", + field: reflect.ValueOf(new([]int)).Elem(), + tagValue: "1,2", + expected: []int{1, 2}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + tagHandlers(tt.field, tt.tagValue) + AssertEqual(t, tt.field.Interface(), tt.expected, "tagHandlers failed") + }) + } +} + +func TestSetDefaultForSlice(t *testing.T) { + t.Parallel() + tests := []struct { + name string + field reflect.Value + tagValue string + elemType reflect.Type + expected interface{} + }{ + { + name: "Slice of strings with default value", + field: reflect.ValueOf(new([]string)).Elem(), + tagValue: "apple,banana", + elemType: reflect.TypeOf(""), + expected: []string{"apple", "banana"}, + }, + { + name: "Slice of ints with default value", + field: reflect.ValueOf(new([]int)).Elem(), + tagValue: "1,2,3", + elemType: reflect.TypeOf(0), + expected: []int{1, 2, 3}, + }, + { + name: "Slice of string pointers with default value", + field: reflect.ValueOf(new([]*string)).Elem(), + tagValue: "apple,banana", + elemType: reflect.TypeOf(new(*string)).Elem(), + expected: []*string{str("apple"), str("banana")}, + }, + { + name: "Slice of int pointers with default value", + field: reflect.ValueOf(new([]*int)).Elem(), + tagValue: "1,2,3", + elemType: reflect.TypeOf(new(*int)).Elem(), + expected: []*int{ptr(1), ptr(2), ptr(3)}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + setDefaultForSlice(tt.field, tt.tagValue, tt.elemType) + AssertEqual(t, tt.field.Interface(), tt.expected, "setDefaultForSlice failed") + }) + } +} + +// ptr is a helper function to take the address of a string. +func ptr(s int) *int { + return &s +} + +func str(s string) *string { + return &s +}