mirror of
https://github.com/kataras/iris.git
synced 2025-12-21 03:47:04 +00:00
add a table on the README for the param types, macro funcs and do it yourself section
Former-commit-id: eca9418779371c014d3bf3bca88430055841da4f
This commit is contained in:
@@ -159,17 +159,17 @@ func main() {
|
||||
ctx.Writef("age selected: %d", age)
|
||||
})
|
||||
|
||||
// Another example using a custom regexp and any custom logic.
|
||||
// Another example using a custom regexp or any custom logic.
|
||||
|
||||
// Register your custom argument-less macro function to the :string param type.
|
||||
latLonExpr := "^-?[0-9]{1,3}(?:\\.[0-9]{1,10})?$"
|
||||
latLonRegex, err := regexp.Compile(latLonExpr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
app.Macros().String.RegisterFunc("coordinate", func() func(paramName string) (ok bool) {
|
||||
// MatchString is a type of func(string) bool, so we can return that as it's.
|
||||
return latLonRegex.MatchString
|
||||
})
|
||||
// MatchString is a type of func(string) bool, so we use it as it is.
|
||||
app.Macros().String.RegisterFunc("coordinate", latLonRegex.MatchString)
|
||||
|
||||
app.Get("/coordinates/{lat:string coordinate() else 502}/{lon:string coordinate() else 502}", func(ctx iris.Context) {
|
||||
ctx.Writef("Lat: %s | Lon: %s", ctx.Params().Get("lat"), ctx.Params().Get("lon"))
|
||||
@@ -177,6 +177,42 @@ func main() {
|
||||
|
||||
//
|
||||
|
||||
// Another one is by using a custom body.
|
||||
app.Macros().String.RegisterFunc("range", func(minLength, maxLength int) func(string) bool {
|
||||
return func(paramValue string) bool {
|
||||
return len(paramValue) >= minLength && len(paramValue) <= maxLength
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/limitchar/{name:string range(1,200)}", func(ctx iris.Context) {
|
||||
name := ctx.Params().Get("name")
|
||||
ctx.Writef(`Hello %s | the name should be between 1 and 200 characters length
|
||||
otherwise this handler will not be executed`, name)
|
||||
})
|
||||
|
||||
//
|
||||
|
||||
// Register your custom macro function which accepts a slice of strings `[...,...]`.
|
||||
app.Macros().String.RegisterFunc("has", func(validNames []string) func(string) bool {
|
||||
return func(paramValue string) bool {
|
||||
for _, validName := range validNames {
|
||||
if validName == paramValue {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/static_validation/{name:string has([kataras,gerasimos,maropoulos]}", func(ctx iris.Context) {
|
||||
name := ctx.Params().Get("name")
|
||||
ctx.Writef(`Hello %s | the name should be "kataras" or "gerasimos" or "maropoulos"
|
||||
otherwise this handler will not be executed`, name)
|
||||
})
|
||||
|
||||
//
|
||||
|
||||
// http://localhost:8080/game/a-zA-Z/level/42
|
||||
// remember, alphabetical is lowercase or uppercase letters only.
|
||||
app.Get("/game/{name:alphabetical}/level/{level:number}", func(ctx iris.Context) {
|
||||
|
||||
Reference in New Issue
Block a user