RejectedSoftware Forums

Sign up

REST ill-named arguments

interface IGitlabApi
{
    @path("projects/:p/merge_requests/:mr/notes")
    void postCommentForMergeRequest(uint _p, uint _mr, string body);
}

This would work fine if only body was not a reserved keyword. =)
Can I work it around somehow?
Thanx.

Re: REST ill-named arguments

Ok, for now I've worked it around in the following way:

class GitlabAPI : RestInterfaceClient!IGitlabAPI
{
    // Construction should go here...

    override Json request(string verb, string name, Json params, bool[string] param_is_json) const
    {
        foreach(k, v; param_is_json)
            if (k.endsWith('_'))
            {
                string newKey = k[0 .. $ - 1];
                param_is_json[newKey] = param_is_json[k];
                params[newKey] = params[k];
                params.remove(k);
            }
        return super.request(verb, name, params, param_is_json);
    }
}

The argument name should end in _, e.g. body_. Would be cool to find out any easier approach. =)

Re: REST ill-named arguments

Great!

But I prefer a little bit more generic solution:

class MyRestInterfaceClient(T) : RestInterfaceClient!T
{
    override Json request(...) const
    {
        ...     
    }
}

interface IGitlabApi
{
    @path("projects/:p/merge_requests/:mr/notes")
    void postCommentForMergeRequest(uint _p, uint _mr, string body_);
}

...

auto new api = MyRestInterfaceClient!IGitlabApi(host);

Re: REST ill-named arguments

On Wed, 03 Sep 2014 08:13:07 GMT, Yuriy wrote:

Ok, for now I've worked it around in the following way:

class GitlabAPI : RestInterfaceClient!IGitlabAPI
{
    // Construction should go here...

    override Json request(string verb, string name, Json params, bool[string] param_is_json) const
    {
        foreach(k, v; param_is_json)
            if (k.endsWith('_'))
            {
                string newKey = k[0 .. $ - 1];
                param_is_json[newKey] = param_is_json[k];
                params[newKey] = params[k];
                params.remove(k);
            }
        return super.request(verb, name, params, param_is_json);
    }
}

The argument name should end in _, e.g. body_. Would be cool to find out any easier approach. =)

This is now the default behavior as of f2ffc4b. RestInterfaceSettings.stripTrailingUnderscore = false; can be used to get the old behavior.