go-urlrouter icon indicating copy to clipboard operation
go-urlrouter copied to clipboard

Implement explicit API for creating a router and adding routes.

Open fcuny opened this issue 12 years ago • 4 comments

Add a NewRouter function to the router package to explicitly create a new router. The functions AddRoute and AddRoutes are added in order to explicitly add routes to the router. The AddRoute function also check that the route is unique during that phase, and report an error if it's not the case.

The tests and the example are updated to reflect that change.

fcuny avatar Apr 06 '13 23:04 fcuny

This is interesting, I noticed that most of the std lib packages try to not have constructor method. Instead they want to user to instantiate the object using the literal way. For this reason I didn't created constructor for this object. That been said, having a constructor now, allows for additional logic in the future without changing the API. So really, I'm 50/50 on this question. Is it a good practice to define constructor in Go or is it a good practice to avoid them ? I need to investigate that.

ant0ine avatar Apr 07 '13 02:04 ant0ine

As you said, having a constructor allows additional logic when creating the router. The way I see it, the router is not an ordinary object/type, but some kind of higher level object, and having a constructor to deal with (future) logic make sense (at least to me).

I would agree that it would make less sense for something like the Route.

And what about the AddRoute(s) functions? If the constructor is an issue I can rework the patch to remove it.

fcuny avatar Apr 07 '13 03:04 fcuny

This is not final, I'm still improving and simplifying the code added to support this feature.

fcuny avatar Apr 08 '13 00:04 fcuny

Wow, that's a lot of work :) Last week I've changed the implementation of go-json-rest to use one router per method: https://github.com/ant0ine/go-json-rest/commit/b3ca9f14bc3dfee2bd229f2e958a9eee7fc9f2e3

The idea is that on a REST framework you have to know the http method by definition. While on Go-UrlRouter that may be used for a different purpose, you may ask "find the route for this Path and ANY Method". Which mean that there is a special kind of placeholder to implement in the Trie. I decided to keep the things simple by not supporting the http Method in Go-UrlRouter. Lets discuss the Pros and Cons next week :) Thanks for all the work !

ant0ine avatar Apr 08 '13 00:04 ant0ine