mirror of
https://github.com/kataras/iris.git
synced 2025-02-09 02:34:55 +01:00
add a new 'uuid' path parameter type e.g. /{id:uuid}
This commit is contained in:
parent
590e999688
commit
5fe233278a
16
HISTORY.md
16
HISTORY.md
|
@ -28,6 +28,22 @@ The codebase for Dependency Injection, Internationalization and localization and
|
||||||
|
|
||||||
## Fixes and Improvements
|
## Fixes and Improvements
|
||||||
|
|
||||||
|
- New `uuid` builtin path parameter type. Example:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// +------------------------+
|
||||||
|
// | {param:uuid} |
|
||||||
|
// +------------------------+
|
||||||
|
// UUIDv4 (and v1) path parameter validation.
|
||||||
|
|
||||||
|
// http://localhost:8080/user/bb4f33e4-dc08-40d8-9f2b-e8b2bb615c0e -> OK
|
||||||
|
// http://localhost:8080/user/dsadsa-invalid-uuid -> NOT FOUND
|
||||||
|
app.Get("/user/{id:uuid}", func(ctx iris.Context) {
|
||||||
|
id := ctx.Params().Get("id")
|
||||||
|
ctx.WriteString(id)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
- New `Configuration.KeepAlive` and `iris.WithKeepAlive(time.Duration) Configurator` added as helpers to start the server using a tcp listener featured with keep-alive.
|
- New `Configuration.KeepAlive` and `iris.WithKeepAlive(time.Duration) Configurator` added as helpers to start the server using a tcp listener featured with keep-alive.
|
||||||
|
|
||||||
- New `DirOptions.ShowHidden bool` is added by [@tuhao1020](https://github.com/tuhao1020) at [PR #1717](https://github.com/kataras/iris/pull/1717) to show or hide the hidden files when `ShowList` is set to true.
|
- New `DirOptions.ShowHidden bool` is added by [@tuhao1020](https://github.com/tuhao1020) at [PR #1717](https://github.com/kataras/iris/pull/1717) to show or hide the hidden files when `ShowList` is set to true.
|
||||||
|
|
|
@ -147,6 +147,17 @@ func main() {
|
||||||
// app.Macros().String.RegisterFunc("equal", func(argument string) func(paramValue string) bool {
|
// app.Macros().String.RegisterFunc("equal", func(argument string) func(paramValue string) bool {
|
||||||
// return func(paramValue string) bool { return argument == paramValue }
|
// return func(paramValue string) bool { return argument == paramValue }
|
||||||
// })
|
// })
|
||||||
|
// +------------------------+
|
||||||
|
// | {param:uuid} |
|
||||||
|
// +------------------------+
|
||||||
|
// UUIDv4 (and v1) path parameter validation.
|
||||||
|
|
||||||
|
// http://localhost:8080/user/bb4f33e4-dc08-40d8-9f2b-e8b2bb615c0e -> OK
|
||||||
|
// http://localhost:8080/user/dsadsa-invalid-uuid -> NOT FOUND
|
||||||
|
app.Get("/user/{id:uuid}", func(ctx iris.Context) {
|
||||||
|
id := ctx.Params().Get("id")
|
||||||
|
ctx.WriteString(id)
|
||||||
|
})
|
||||||
|
|
||||||
// you can use the "string" type which is valid for a single path parameter that can be anything.
|
// you can use the "string" type which is valid for a single path parameter that can be anything.
|
||||||
app.Get("/username/{name}", func(ctx iris.Context) {
|
app.Get("/username/{name}", func(ctx iris.Context) {
|
||||||
|
|
|
@ -419,6 +419,24 @@ func TestPathEvaluatorRaw(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUUIDEvaluatorRaw(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
pass bool
|
||||||
|
input string
|
||||||
|
}{
|
||||||
|
{true, "978ad967-5fad-4c82-af99-580097ace662"}, // v4
|
||||||
|
{true, "c7067f9c-6d43-11eb-9439-0242ac130002"}, // v1
|
||||||
|
{false, "astring"}, // 0
|
||||||
|
{false, "astringwith_numb3rS_and_symbol$"}, // 1
|
||||||
|
{false, "32321"}, // 2
|
||||||
|
{false, "main.css"}, // 3
|
||||||
|
{false, "/assets/main.css"}, // 4
|
||||||
|
}
|
||||||
|
for i, tt := range tests {
|
||||||
|
testEvaluatorRaw(t, UUID, tt.input, reflect.String, tt.pass, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestConvertBuilderFunc(t *testing.T) {
|
func TestConvertBuilderFunc(t *testing.T) {
|
||||||
fn := func(min uint64, slice []string) func(string) bool {
|
fn := func(min uint64, slice []string) func(string) bool {
|
||||||
return func(paramValue string) bool {
|
return func(paramValue string) bool {
|
||||||
|
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/kataras/iris/v12/macro/interpreter/ast"
|
"github.com/kataras/iris/v12/macro/interpreter/ast"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -402,6 +404,16 @@ var (
|
||||||
// Should be living in the latest path segment of a route path.
|
// Should be living in the latest path segment of a route path.
|
||||||
Path = NewMacro("path", "", false, true, nil)
|
Path = NewMacro("path", "", false, true, nil)
|
||||||
|
|
||||||
|
// UUID string type for validating a uuidv4 (and v1) path parameter
|
||||||
|
// Read more at: https://tools.ietf.org/html/rfc4122
|
||||||
|
UUID = NewMacro("uuid", "uuidv4", false, false, func(paramValue string) (interface{}, bool) {
|
||||||
|
_, err := uuid.Parse(paramValue) // this is x10+ times faster than regexp.
|
||||||
|
if err != nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
return paramValue, true
|
||||||
|
})
|
||||||
// Defaults contains the defaults macro and parameters types for the router.
|
// Defaults contains the defaults macro and parameters types for the router.
|
||||||
//
|
//
|
||||||
// Read https://github.com/kataras/iris/tree/master/_examples/routing/macros for more details.
|
// Read https://github.com/kataras/iris/tree/master/_examples/routing/macros for more details.
|
||||||
|
@ -421,6 +433,7 @@ var (
|
||||||
Alphabetical,
|
Alphabetical,
|
||||||
File,
|
File,
|
||||||
Path,
|
Path,
|
||||||
|
UUID,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user