RejectedSoftware Forums

Sign up

Cannot return web interface from interface method with parameter.

I have a bunch of routes that look like this:

item/:id
item/:id/edit
item/:id/delete

etc.

and for each one, I have to put in a path UDA to designate how to map
the id to the _id parameter.

I realized that actually, I can delegate this to a different object!

...Or so I thought, but I get this assertion:

Instances may only be returned from parameter-less functions

What I had is something like this:

@path(":id")
ItemHandler get(int _id)
{

itemHandler.id = _id;
return itemHandler;

}

Why does this limitation exist? I'm not even certain it's necessary,
even in the general case where the parameters come from the form data.

It would make writing all my handling code more modular and less
verbose. Is this something that should be supported, even if it's only
for route parameters?

-Steve

Re: Cannot return web interface from interface method with parameter.

On Wed, 5 Jun 2019 17:51:34 -0400, Steven Schveighoffer wrote:

I have a bunch of routes that look like this:

item/:id
item/:id/edit
item/:id/delete

etc.

and for each one, I have to put in a path UDA to designate how to map
the id to the _id parameter.

I realized that actually, I can delegate this to a different object!

...Or so I thought, but I get this assertion:

Instances may only be returned from parameter-less functions

What I had is something like this:

@path(":id")
ItemHandler get(int _id)
{

itemHandler.id = _id;
return itemHandler;

}

Why does this limitation exist? I'm not even certain it's necessary,
even in the general case where the parameters come from the form data.

It would make writing all my handling code more modular and less
verbose. Is this something that should be supported, even if it's only
for route parameters?

-Steve

I think what you need is Collection!I.

The example above would look something like this:

interface Item {
    // defines the chain of fixed parameters that identify a single item
    struct CollectionIndices {
        int _id;
    }

    void get(int _id);
    void edit(int _id, ...);
    void delete(int _id, ...);
}

interface API {
    @property Collection!Item item();
}

Apart from unifying handling of the "path" parameters, it also enables a nicer, index based, access syntax from D: api.item[id].edit(...)

Collection!I can also be nested freely by extending the entries in the CollectionIndices struct (see the example in the API docs).

Re: Cannot return web interface from interface method with parameter.

On 6/6/19 4:40 AM, Sönke Ludwig wrote:

I think what you need is Collection!I.

The example above would look something like this:

interface Item {
     // defines the chain of fixed parameters that identify a single item
     struct CollectionIndices {
         int _id;
     }

     void get(int _id);
     void edit(int _id, ...);
     void delete(int _id, ...);
}

interface API {
     @property Collection!Item item();
}

Except this isn't REST. What I wanted to do was handle the first part of
the route, then pass the id into the second part. Basically, abstract
the subroute into its own piece. This can be useful for multiple
reasons. It appears that this isn't possible basically due to the static
assertion. It seems like an artificial limitation, and I'm looking to
learn why it isn't artificial if there is a good reason. It might be
something I would be able to add to vibe.d, but only if there isn't a
reason it would be impossible.

-Steve