From 9fd528321c2cdda0fe2f0158a444aa1ba0f50666 Mon Sep 17 00:00:00 2001 From: Geod24 Date: Sat, 7 Mar 2015 02:29:44 +0100 Subject: [PATCH] Simplify @path handling - 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. --- examples/rest/source/app.d | 1 + source/vibe/web/common.d | 19 +++++++++++++------ source/vibe/web/rest.d | 21 ++++++++++++++++++--- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/examples/rest/source/app.d b/examples/rest/source/app.d index 938c6f7753..10c904b17b 100644 --- a/examples/rest/source/app.d +++ b/examples/rest/source/app.d @@ -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" diff --git a/source/vibe/web/common.d b/source/vibe/web/common.d index 16f66d3327..da8ec31ffa 100644 --- a/source/vibe/web/common.d +++ b/source/vibe/web/common.d @@ -322,11 +322,12 @@ 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 @@ -334,7 +335,7 @@ unittest import vibe.http.router; import vibe.web.rest; - @rootPath("/oops") + @path("/oops") interface IAPI { int getFoo(); @@ -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) diff --git a/source/vibe/web/rest.d b/source/vibe/web/rest.d index b4205f0e76..499c48db71 100644 --- a/source/vibe/web/rest.d +++ b/source/vibe/web/rest.d @@ -79,7 +79,7 @@ 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); @@ -87,6 +87,14 @@ void registerRestInterface(TImpl)(URLRouter router, TImpl instance, RestInterfac } 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) @@ -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 @@ -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;