Skip to content

Commit

Permalink
vibe.http.rest : add funcattr support
Browse files Browse the repository at this point in the history
  • Loading branch information
Dicebot committed Oct 18, 2013
1 parent 361005b commit e6a98a9
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions source/vibe/http/rest.d
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class RestInterfaceClient(I) : I
//pragma(msg, generateModuleImports!(I)());
#line 1 "module imports"
mixin(generateModuleImports!I());
#line 243
#line 244

import vibe.inet.url : URL, PathEntry;
import vibe.http.client : HTTPClientRequest;
Expand Down Expand Up @@ -290,7 +290,7 @@ class RestInterfaceClient(I) : I

#line 1 "subinterface instances"
mixin (generateRestInterfaceSubInterfaceInstances!I());
#line 293
#line 294
}

/**
Expand All @@ -306,7 +306,7 @@ class RestInterfaceClient(I) : I
m_requestFilter = v;
#line 1 "request filter"
mixin (generateRestInterfaceSubInterfaceRequestFilter!I());
#line 309
#line 310
}

//pragma(msg, "subinterfaces:");
Expand All @@ -318,7 +318,7 @@ class RestInterfaceClient(I) : I
//pragma(msg, generateRestInterfaceMethods!(I)());
#line 1 "restinterface"
mixin (generateRestInterfaceMethods!I());
#line 337 "source/vibe/http/rest.d"
#line 322 "source/vibe/http/rest.d"

protected {
import vibe.data.json : Json;
Expand Down Expand Up @@ -593,6 +593,7 @@ private HTTPServerRequestDelegate jsonMethodHandler(T, string method, alias Func
import vibe.http.server : HTTPServerRequest, HTTPServerResponse;
import vibe.http.common : HTTPStatusException, HTTPStatus;
import vibe.utils.string : sanitizeUTF8;
import vibe.utils.meta.funcattr : IsAttributedParameter;

alias PT = ParameterTypeTuple!Func;
alias RT = ReturnType!Func;
Expand All @@ -613,6 +614,11 @@ private HTTPServerRequestDelegate jsonMethodHandler(T, string method, alias Func
)
);

// will be re-written by UDA function anyway
static if (IsAttributedParameter!(Func, ParamNames[i])) {
continue;
}

static if (i == 0 && ParamNames[i] == "id") {
// legacy special case for :id, backwards-compatibility
logDebug("id %s", req.params["id"]);
Expand All @@ -632,7 +638,7 @@ private HTTPServerRequestDelegate jsonMethodHandler(T, string method, alias Func
alias DefVal = ParamDefaults[i];
if (req.method == HTTPMethod.GET ) {
logDebug("query %s of %s" ,ParamNames[i], req.query);

static if (is (DefVal == void)) {
enforce(
ParamNames[i] in req.query,
Expand All @@ -644,6 +650,7 @@ private HTTPServerRequestDelegate jsonMethodHandler(T, string method, alias Func
continue;
}
}

params[i] = fromRestString!P(req.query[ParamNames[i]]);
} else {
logDebug("%s %s", method, ParamNames[i]);
Expand Down Expand Up @@ -679,11 +686,15 @@ private HTTPServerRequestDelegate jsonMethodHandler(T, string method, alias Func
}

try {
import vibe.utils.meta.funcattr;

auto handler = createAttributedFunction!Func(req, res);

static if (is(RT == void)) {
__traits(getMember, inst, method)(params);
handler(&__traits(getMember, inst, method), params);
res.writeJsonBody(Json.emptyObject);
} else {
auto ret = __traits(getMember, inst, method)(params);
auto ret = handler(&__traits(getMember, inst, method), params);
res.writeJsonBody(serializeToJson(ret));
}
} catch (HTTPStatusException e) {
Expand Down Expand Up @@ -863,6 +874,7 @@ private string generateRestInterfaceMethods(I)()
import std.array : split;

import vibe.utils.meta.codegen : cloneFunction;
import vibe.utils.meta.funcattr : IsAttributedParameter;
import vibe.http.server : httpMethodString;

string ret;
Expand Down Expand Up @@ -911,7 +923,10 @@ private string generateRestInterfaceMethods(I)()
else
url_prefix = q{urlEncode(toRestString(serializeToJson(id)))~"/"};
}
else static if (!ParamNames[i].startsWith("_")) {
else static if (
!ParamNames[i].startsWith("_") &&
!IsAttributedParameter!(overload, ParamNames[i])
) {
// underscore parameters are sourced from the HTTPServerRequest.params map or from url itself
param_handling_str ~= format(
q{
Expand Down

0 comments on commit e6a98a9

Please sign in to comment.