Skip to content

Commit

Permalink
Simplify @path handling
Browse files Browse the repository at this point in the history
- Lay the ground to make @rootPathAttribute as the default by deprecating unattributed interfaces.
- Kill RootPathAttribute and use PathAttribute only instead: There was no real difference between @rootpath and @path, as they were mutually exclusive (path on method, rootPath on interface) and had the same behavior (e.g. they are always relative). This will obviously make user code less error-prone.
- Communicate the settings to the sub-interface.
  • Loading branch information
Geod24 committed Mar 25, 2015
1 parent 0118a2d commit 9fd5283
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
1 change: 1 addition & 0 deletions examples/rest/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ interface Example3API
int getMyID(int id);
}

@path("/")
interface Example3APINested
{
/* In this example it will be available under "GET /nested_module/number"
Expand Down
19 changes: 13 additions & 6 deletions source/vibe/web/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -322,19 +322,20 @@ PathAttribute path(string data)
UDA to define root URL prefix for annotated REST interface.
Empty path means deducing prefix from interface type name (see also rootPathFromName)
*/
RootPathAttribute rootPath(string path)
deprecated("rootPath is deprecated. Use path directly.")
PathAttribute rootPath(string path)
{
if (!__ctfe)
assert(false, onlyAsUda!__FUNCTION__);
return RootPathAttribute(path);
return PathAttribute(path);
}
///
unittest
{
import vibe.http.router;
import vibe.web.rest;

@rootPath("/oops")
@path("/oops")
interface IAPI
{
int getFoo();
Expand All @@ -355,10 +356,16 @@ unittest
assert(routes[0].pattern == "/oops/foo" && routes[0].method == HTTPMethod.GET);
}


/**
Convenience alias
*/
Convenience alias
This alias will ultimately be deprecated and made the default.
In Vibe.d 0.7.23 and before, unattributed interface had a path of "/".
In Vibe.d 0.7.24, this is deprecated, and it won't compile with 0.7.25.
In 0.7.26, it's allowed again, but the default is to have the name
infered from the interface name -the same behavior that rootPathFromName
currently provide-.
*/
@property RootPathAttribute rootPathFromName()
{
if (!__ctfe)
Expand Down
21 changes: 18 additions & 3 deletions source/vibe/web/rest.d
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,22 @@ void registerRestInterface(TImpl)(URLRouter router, TImpl instance, RestInterfac

string url_prefix = settings.baseURL.path.toString();

enum uda = findFirstUDA!(RootPathAttribute, I);
enum uda = findFirstUDA!(PathAttribute, I);
static if (uda.found) {
static if (uda.value.data == "") {
auto path = "/" ~ adjustMethodStyle(I.stringof, settings.methodStyle);
url_prefix = concatURL(url_prefix, path);
} else {
url_prefix = concatURL(url_prefix, uda.value.data);
}
} else {
pragma(msg, `Warning: Unattributed interface are deprecated and will error on the next release. Use @path("/") instead`);
// Warning introduced in 0.7.24. It will become an error on 0.7.25.
// On 0.7.26, it will be legal again, but with a different default (uncomment following code).
/*
auto path = "/" ~ adjustMethodStyle(I.stringof, settings.methodStyle);
url_prefix = concatURL(url_prefix, path);
*/
}

void addRoute(HTTPMethod httpVerb, string url, HTTPServerRequestDelegate handler, string[] params)
Expand Down Expand Up @@ -136,10 +144,12 @@ void registerRestInterface(TImpl)(URLRouter router, TImpl instance, RestInterfac
ParameterTypeTuple!overload.length == 0,
"Interfaces may only be returned from parameter-less functions!"
);
auto subSettings = settings.dup;
subSettings.baseURL = URL(concatURL(url_prefix, url, true));
registerRestInterface!RT(
router,
__traits(getMember, instance, method)(),
concatURL(url_prefix, url, true)
subSettings
);
} else {
// normal handler
Expand Down Expand Up @@ -294,13 +304,18 @@ class RestInterfaceClient(I) : I
}

URL url = settings.baseURL;
enum uda = findFirstUDA!(RootPathAttribute, I);
enum uda = findFirstUDA!(PathAttribute, I);
static if (uda.found) {
static if (uda.value.data == "") {
url.path = Path(concatURL(url.path.toString(), adjustMethodStyle(I.stringof, settings.methodStyle), true));
} else {
url.path = Path(concatURL(url.path.toString(), uda.value.data, true));
}
} else {
pragma(msg, `Warning: Unattributed interface are deprecated and will error on the next release. Use @path("/") instead`);
// Warning introduced in 0.7.24. It will become an error on 0.7.25.
// On 0.7.26, it will be legal again, but with a different default (uncomment following code).
//url.path = Path(concatURL(url.path.toString(), adjustMethodStyle(I.stringof, settings.methodStyle), true));
}

m_baseURL = url;
Expand Down

0 comments on commit 9fd5283

Please sign in to comment.