From 5fe233278a28b6b658b2cd95c8338cc1ff0aef6a Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Fri, 12 Feb 2021 17:12:23 +0200 Subject: [PATCH] add a new 'uuid' path parameter type e.g. /{id:uuid} --- HISTORY.md | 16 ++++++++++++++++ _examples/routing/dynamic-path/main.go | 11 +++++++++++ macro/macro_test.go | 18 ++++++++++++++++++ macro/macros.go | 13 +++++++++++++ 4 files changed, 58 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 8b78a32e6d..19236ce770 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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. diff --git a/_examples/routing/dynamic-path/main.go b/_examples/routing/dynamic-path/main.go index 8cc0ce733d..9adab3803e 100644 --- a/_examples/routing/dynamic-path/main.go +++ b/_examples/routing/dynamic-path/main.go @@ -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) { diff --git a/macro/macro_test.go b/macro/macro_test.go index 123ea69f5d..6ae452b585 100644 --- a/macro/macro_test.go +++ b/macro/macro_test.go @@ -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 { diff --git a/macro/macros.go b/macro/macros.go index f88aca069b..2fb3b555a3 100644 --- a/macro/macros.go +++ b/macro/macros.go @@ -5,6 +5,8 @@ import ( "strings" "github.com/kataras/iris/v12/macro/interpreter/ast" + + "github.com/google/uuid" ) var ( @@ -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. @@ -421,6 +433,7 @@ var ( Alphabetical, File, Path, + UUID, } )