Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic path regex #918

Closed
Dexus opened this issue Mar 4, 2018 · 3 comments
Closed

dynamic path regex #918

Dexus opened this issue Mar 4, 2018 · 3 comments

Comments

@Dexus
Copy link

Dexus commented Mar 4, 2018

Hello,

when I try to use regex like:

app.Get("/map/{lat:string regex(^[-]?[0-9]+(\\.[0-9]{1,5})) else 404}/{lon:string regex(^[-]?[0-9]+(\\.[0-9]{1,5})) else 404}", func(ctx iris.Context) {
...
})

I get can't get the route.
Also if i use something with $ at end of the regex it said it's an illegal token.

@Zeno-Code
Copy link
Contributor

Zeno-Code commented Mar 4, 2018

if you need else in route
use app.Macros() Make filtering and restrictions
app.Macros().String.RegisterFunc("youpathfun", fn)
https://github.com/kataras/iris/tree/master/_examples/routing/dynamic-path

@kataras
Copy link
Owner

kataras commented Mar 5, 2018

Hello @Dexus,

First of all you use wrong string function for regexp, it's regexp no regex as shown at the examples and the README.md itself:P

Secondly, I think the expression of ^-?[0-9]{1,3}(?:\\.[0-9]{1,10})?$ would be more correct, but you know better.

Thirdly, the else 404 is not necessary, the router will fire 404 automatic if the parameters are not evaluated by the custom path evalutator(macro parameter func), use else for other status codes like else 500.

However do it like @Zeno-Code says, using macro functions is much more re-usable and more customized, regexp is a macro func of String type too! Just look here.

A full example for you

import "regexp"
// ...
// func main...

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
})

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"))
})

Request example: http://localhost:8080/coordinates/37.983810/23.727539


Happy coding!

@Dexus
Copy link
Author

Dexus commented Mar 5, 2018

@kataras Damn it! Thanks for the tip. Sometimes I can't see the forest because of the trees. 🙄

@Dexus Dexus closed this as completed Mar 5, 2018
github-actions bot pushed a commit to goproxies/github.com-kataras-iris that referenced this issue Jul 27, 2020
…ataras#918

Former-commit-id: 457c1a94dc8d93e614e9da70e1ec2482fe0c5765
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants