Skip to content

Commit

Permalink
Fix whitespace in vibe.web.rest.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-ludwig committed Sep 12, 2014
1 parent 79895b8 commit 54ea846
Showing 1 changed file with 44 additions and 44 deletions.
88 changes: 44 additions & 44 deletions source/vibe/web/rest.d
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import std.algorithm : startsWith, endsWith;
If a method has its first parameter named 'id', it will be mapped to ':id/method' and
'id' is expected to be part of the URL instead of a JSON request. Parameters with default
values will be optional in the corresponding JSON request.
Any interface that you return from a getter will be made available with the
base url and its name appended.
Expand All @@ -64,9 +64,9 @@ import std.algorithm : startsWith, endsWith;
void registerRestInterface(TImpl)(URLRouter router, TImpl instance, string url_prefix,
MethodStyle style = MethodStyle.lowerUnderscored)
{
import vibe.internal.meta.traits : baseInterface;
import vibe.internal.meta.traits : baseInterface;
import std.traits : MemberFunctionsTuple, ParameterIdentifierTuple,
ParameterTypeTuple, ReturnType;
ParameterTypeTuple, ReturnType;

void addRoute(HTTPMethod httpVerb, string url, HTTPServerRequestDelegate handler, string[] params)
{
Expand Down Expand Up @@ -94,7 +94,7 @@ void registerRestInterface(TImpl)(URLRouter router, TImpl instance, string url_p
}
else {
static if (__traits(identifier, overload) == "index") {
pragma(msg, "Processing interface " ~ T.stringof ~
pragma(msg, "Processing interface " ~ T.stringof ~
": please use @path(\"/\") to define '/' path" ~
" instead of 'index' method. Special behavior will be removed" ~
" in the next release.");
Expand Down Expand Up @@ -201,14 +201,14 @@ unittest
// vibe.d takes care of all JSON encoding/decoding
// and actual API implementation can work directly
// with native types

class API : IMyAPI
{
private {
string m_greeting;
string[] m_users;
}

@property string greeting() { return m_greeting; }
@property void greeting(string text) { m_greeting = text; }

Expand Down Expand Up @@ -256,22 +256,22 @@ class RestInterfaceClient(I) : I

import vibe.inet.url : URL, PathEntry;
import vibe.http.client : HTTPClientRequest;

alias RequestFilter = void delegate(HTTPClientRequest req);

private {
URL m_baseURL;
MethodStyle m_methodStyle;
RequestFilter m_requestFilter;
}

/**
Creates a new REST implementation of I
Creates a new REST implementation of I
*/
this (string base_url, MethodStyle style = MethodStyle.lowerUnderscored)
{
import vibe.internal.meta.uda : findFirstUDA;

URL url;
enum uda = findFirstUDA!(RootPathAttribute, I);
static if (!uda.found) {
Expand Down Expand Up @@ -302,7 +302,7 @@ class RestInterfaceClient(I) : I

mixin (generateRestInterfaceSubInterfaceInstances!I());
}

/**
An optional request filter that allows to modify each request before it is made.
*/
Expand All @@ -316,19 +316,19 @@ class RestInterfaceClient(I) : I
m_requestFilter = v;
mixin (generateRestInterfaceSubInterfaceRequestFilter!I());
}

//pragma(msg, "subinterfaces:");
//pragma(msg, generateRestInterfaceSubInterfaces!(I)());
mixin (generateRestInterfaceSubInterfaces!I());

//pragma(msg, "restinterface:");
//pragma(msg, generateRestInterfaceMethods!(I)());
mixin (generateRestInterfaceMethods!I());

protected {
import vibe.data.json : Json;
import vibe.textfilter.urlencode;

Json request(string verb, string name, Json params, bool[string] param_is_json) const
{
import vibe.http.client : HTTPClientRequest, HTTPClientResponse,
Expand All @@ -341,7 +341,7 @@ class RestInterfaceClient(I) : I
URL url = m_baseURL;

if (name.length) url ~= Path(name);

if ((verb == "GET" || verb == "HEAD") && params.length > 0) {
auto query = appender!string();
bool first = true;
Expand All @@ -360,7 +360,7 @@ class RestInterfaceClient(I) : I

url.queryString = query.data();
}

Json ret;

auto reqdg = (scope HTTPClientRequest req) {
Expand All @@ -374,7 +374,7 @@ class RestInterfaceClient(I) : I
req.writeJsonBody(params);
}
};

auto resdg = (scope HTTPClientResponse res) {
ret = res.readJson();

Expand All @@ -391,7 +391,7 @@ class RestInterfaceClient(I) : I
};

requestHTTP(url, reqdg, resdg);

return ret;
}
}
Expand All @@ -409,7 +409,7 @@ unittest
@property string greeting();
// PUT /greeting
@property void greeting(string text);

// POST /new_user
void addNewUser(string name);
// GET /users
Expand Down Expand Up @@ -439,7 +439,7 @@ unittest
private HTTPServerRequestDelegate jsonMethodHandler(T, string method, alias Func)(T inst)
{
import std.traits : ParameterTypeTuple, ReturnType,
ParameterDefaultValueTuple, ParameterIdentifierTuple;
ParameterDefaultValueTuple, ParameterIdentifierTuple;
import std.string : format;
import std.algorithm : startsWith;
import std.exception : enforce;
Expand All @@ -453,11 +453,11 @@ private HTTPServerRequestDelegate jsonMethodHandler(T, string method, alias Func
alias RT = ReturnType!Func;
alias ParamDefaults = ParameterDefaultValueTuple!Func;
enum ParamNames = [ ParameterIdentifierTuple!Func ];

void handler(HTTPServerRequest req, HTTPServerResponse res)
{
PT params;

foreach (i, P; PT) {
static assert (
ParamNames[i].length,
Expand Down Expand Up @@ -489,7 +489,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 Down Expand Up @@ -536,7 +536,7 @@ private HTTPServerRequestDelegate jsonMethodHandler(T, string method, alias Func
}
}
}

try {
import vibe.internal.meta.funcattr;

Expand All @@ -560,7 +560,7 @@ private HTTPServerRequestDelegate jsonMethodHandler(T, string method, alias Func
);
}
}

return &handler;
}

Expand All @@ -574,7 +574,7 @@ private string generateRestInterfaceSubInterfaces(I)()
ReturnType, ParameterTypeTuple, fullyQualifiedName;
import std.algorithm : canFind;
import std.string : format;

string ret;
string[] tps; // list of already processed interface types

Expand Down Expand Up @@ -626,13 +626,13 @@ private string generateRestInterfaceSubInterfaceInstances(I)()
ReturnType, ParameterTypeTuple;
import std.string : format;
import std.algorithm : canFind;

string ret;
string[] tps; // list of of already processed interface types

foreach (method; __traits(allMembers, I)) {
foreach (overload; MemberFunctionsTuple!(I, method)) {

alias FT = FunctionTypeOf!overload;
alias PTT = ParameterTypeTuple!FT;
alias RT = ReturnType!FT;
Expand All @@ -646,9 +646,9 @@ private string generateRestInterfaceSubInterfaceInstances(I)()
if (!tps.canFind(RT.stringof)) {
tps ~= RT.stringof;
string implname = RT.stringof ~ "Impl";

enum meta = extractHTTPMethodAndName!overload();

ret ~= format(
q{
if (%s)
Expand Down Expand Up @@ -679,7 +679,7 @@ private string generateRestInterfaceSubInterfaceRequestFilter(I)()
ReturnType, ParameterTypeTuple;
import std.string : format;
import std.algorithm : canFind;

string ret;
string[] tps; // list of already processed interface types

Expand All @@ -699,7 +699,7 @@ private string generateRestInterfaceSubInterfaceRequestFilter(I)()
if (!tps.canFind(RT.stringof)) {
tps ~= RT.stringof;
string implname = RT.stringof ~ "Impl";

ret ~= format(
q{
m_%s.requestFilter = m_requestFilter;
Expand Down Expand Up @@ -729,7 +729,7 @@ private string generateRestInterfaceMethods(I)()
import vibe.internal.meta.codegen : cloneFunction;
import vibe.internal.meta.funcattr : IsAttributedParameter;
import vibe.http.server : httpMethodString;

string ret;

foreach (method; __traits(allMembers, I)) {
Expand All @@ -741,9 +741,9 @@ private string generateRestInterfaceMethods(I)()
alias ParamNames = ParameterIdentifierTuple!overload;

enum meta = extractHTTPMethodAndName!overload();

// NB: block formatting is coded in dependency order, not in 1-to-1 code flow order

static if (is(RT == interface)) {
ret ~= format(
q{
Expand All @@ -757,7 +757,7 @@ private string generateRestInterfaceMethods(I)()
} else {
string param_handling_str;
string url_prefix = `""`;

// Block 2
foreach (i, PT; PTT){
static assert (
Expand All @@ -768,7 +768,7 @@ private string generateRestInterfaceMethods(I)()
method
)
);

// legacy :id special case, left for backwards-compatibility reasons
static if (i == 0 && ParamNames[0] == "id") {
static if (is(PT == Json))
Expand All @@ -793,10 +793,10 @@ private string generateRestInterfaceMethods(I)()
);
}
}

// Block 3
string request_str;

static if (!meta.hadPathUDA) {
request_str = format(
q{
Expand Down Expand Up @@ -832,22 +832,22 @@ private string generateRestInterfaceMethods(I)()

request_str ~= ";\n";
}

request_str ~= format(
q{
auto jret__ = request("%s", url__ , jparams__, jparamsj__);
},
httpMethodString(meta.method)
);

static if (!is(ReturnType!overload == void)) {
request_str ~= q{
typeof(return) ret__;
deserializeJson(ret__, jret__);
return ret__;
};
}

// Block 1
ret ~= format(
q{
Expand All @@ -867,7 +867,7 @@ private string generateRestInterfaceMethods(I)()
}
}
}

return ret;
}

Expand Down Expand Up @@ -909,7 +909,7 @@ private string generateModuleImports(I)()
import std.algorithm : map;
import std.array : join;

auto modules = getRequiredImports!I();
auto modules = getRequiredImports!I();
return join(map!(a => "static import " ~ a ~ ";")(modules), "\n");
}

Expand Down

0 comments on commit 54ea846

Please sign in to comment.