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

New Feature: Handle different parameter types in the same path #1315

Closed
kataras opened this issue Jul 29, 2019 · 0 comments
Closed

New Feature: Handle different parameter types in the same path #1315

kataras opened this issue Jul 29, 2019 · 0 comments

Comments

@kataras
Copy link
Owner

kataras commented Jul 29, 2019

Hello Iris Community.

Yesterday I've got an e-mail about:

package main

import (
   "github.com/kataras/iris"
)

func main() {
   app := iris.New()
   app.Get("/{id:int}", func(ctx iris.Context) {
      ctx.WriteString("id")
   })
   app.Get("/{str:string}", func(ctx iris.Context) {
      ctx.WriteString("str")
   })
   app.Run(iris.Addr(":8080"))
}

And I answered that we can't handle this yet. However, the person who sent the e-mail gave me a fairly good reason to support this type of routing usage; iris developers in his company would love to use patterns like that, and it will be very helpful if Iris could support this as no other web framework can provide such a feature without performance cost on the rest of the routes.

I am glad to announce that I've just finished this feature and it will be pushed to the upcoming 11.2.3 version.

So something like this could work without any issues (order: top as fallback)

app.Get("/u/{username:string}", func(ctx iris.Context) {
	ctx.Writef("before username (string), current route name: %s\n", ctx.RouteName())
	ctx.Next()
}, func(ctx iris.Context) {
	ctx.Writef("username (string): %s", ctx.Params().Get("username"))
})

app.Get("/u/{id:int}", func(ctx iris.Context) {
	ctx.Writef("before id (int), current route name: %s\n", ctx.RouteName())
	ctx.Next()
}, func(ctx iris.Context) {
	ctx.Writef("id (int): %d", ctx.Params().GetIntDefault("id", 0))
})

app.Get("/u/{uid:uint}", func(ctx iris.Context) {
	ctx.Writef("before uid (uint), current route name: %s\n", ctx.RouteName())
	ctx.Next()
}, func(ctx iris.Context) {
	ctx.Writef("uid (uint): %d", ctx.Params().GetUintDefault("uid", 0))
})

app.Get("/u/{firstname:alphabetical}", func(ctx iris.Context) {
	ctx.Writef("before firstname (alphabetical), current route name: %s\n", ctx.RouteName())
	ctx.Next()
}, func(ctx iris.Context) {
	ctx.Writef("firstname (alphabetical): %s", ctx.Params().Get("firstname"))
})

/*
	/u/abcd maps to :alphabetical (if :alphabetical registered otherwise :string)
	/u/42 maps to :uint (if :uint registered otherwise :int)
	/u/-1 maps to :int (if :int registered otherwise :string)
	/u/abcd123 maps to :string
*/

Done with: #1316

@kataras kataras added this to the v11.2.3 milestone Jul 29, 2019
@kataras kataras closed this as completed Aug 3, 2019
github-actions bot pushed a commit to goproxies/github.com-kataras-iris that referenced this issue Jul 27, 2020
implements kataras#1315


Former-commit-id: 3e9276f2a95d6fc7c10fbf91186d041dcba72611
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

1 participant