Skip to content

Commit 0230b4e

Browse files
committed
update convert docs
1 parent 2f44dc1 commit 0230b4e

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

docs/api/ctx.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -2206,21 +2206,23 @@ app.Get("/", func(c fiber.Ctx) error {
22062206

22072207
## Convert
22082208
Converts a string value to a specified type, handling errors and optional default values.
2209-
You can combine this function with other methods and functions.
2210-
2209+
This function simplifies the conversion process by encapsulating error handling and the management of default values, making your code cleaner and more consistent.
22112210
```go title="Signature"
22122211
func Convert[T any](value string, convertor func(string) (T, error), defaultValue ...T) (*T, error)
22132212
```
22142213

2215-
```go title="Example"
2216-
fiber.Convert("true", strconv.ParseBool) // true, nil
2217-
fiber.Convert("123", strconv.Atoi, 5) // 123, nil
2218-
```
2219-
22202214
```go title="Example"
22212215
// GET http://example.com/id/bb70ab33-d455-4a03-8d78-d3c1dacae9ff
22222216
app.Get("/id/:id", func(c fiber.Ctx) error {
22232217
fiber.Convert(c.Params("id"), uuid.Parse) // UUID(bb70ab33-d455-4a03-8d78-d3c1dacae9ff), nil
22242218
2219+
2220+
// GET http://example.com/search?id=65f6f54221fb90e6a6b76db7
2221+
app.Get("/search", func(c fiber.Ctx) error) {
2222+
fiber.Convert(c.Query("id"), mongo.ParseObjectID) // objectid(65f6f54221fb90e6a6b76db7), nil
2223+
fiber.Convert(c.Query("id"), uuid.Parse) // uuid.Nil, error(cannot parse given uuid)
2224+
fiber.Convert(c.Query("id"), uuid.Parse, mongo.NewObjectID) // new object id generated and return nil as error.
2225+
}
2226+
22252227
// ...
22262228
})

0 commit comments

Comments
 (0)