Skip to content

Commit

Permalink
add a new 'uuid' path parameter type e.g. /{id:uuid}
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Feb 12, 2021
1 parent 590e999 commit 5fe2332
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
16 changes: 16 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ The codebase for Dependency Injection, Internationalization and localization and

## 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 `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.
Expand Down
11 changes: 11 additions & 0 deletions _examples/routing/dynamic-path/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ func main() {
// app.Macros().String.RegisterFunc("equal", func(argument string) func(paramValue string) bool {
// 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.
app.Get("/username/{name}", func(ctx iris.Context) {
Expand Down
18 changes: 18 additions & 0 deletions macro/macro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
fn := func(min uint64, slice []string) func(string) bool {
return func(paramValue string) bool {
Expand Down
13 changes: 13 additions & 0 deletions macro/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"

"github.com/kataras/iris/v12/macro/interpreter/ast"

"github.com/google/uuid"
)

var (
Expand Down Expand Up @@ -402,6 +404,16 @@ var (
// Should be living in the latest path segment of a route path.
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.
//
// Read https://github.com/kataras/iris/tree/master/_examples/routing/macros for more details.
Expand All @@ -421,6 +433,7 @@ var (
Alphabetical,
File,
Path,
UUID,
}
)

Expand Down

0 comments on commit 5fe2332

Please sign in to comment.