digitalmars.D.learn - How to get instance member value from getSymbolsByUDA
- Remi Thebault (35/35) Feb 26 2022 Hi all,
- max haughton (3/7) Feb 26 2022 https://dlang.org/phobos/std_traits.html#getUDAs
- Remi Thebault (4/13) Feb 26 2022 How do I use `getUDAs` in this context?
- max haughton (8/23) Feb 26 2022 Getting the UDAs from inside a symbol must be done via a
- Remi Thebault (5/11) Feb 26 2022 I don't need to get access to the UDA value, I need the value of
- max haughton (5/19) Feb 26 2022 I see, my apologies. FYI if possible you should avoid using
Hi all,
I'm trying to establish a REST API by using the type system (used
in both client and server code).
Considering the code
```d
struct Request
{
Method method;
string url;
int apiLevel;
}
Request(Method.GET, "/my-resource/%s", 1)
struct MyResourceGet
{
Param
string name;
// other members...
}
string requestUrl(ReqT)(ReqT req) if (isRequest!ReqT)
{
import std.format : format;
import std.traits : getSymbolsByUDA;
Request reqAttr = RequestAttr!ReqT;
alias paramSymbols = getSymbolsByUDA!(ReqT, Param);
// return format(reqAttr.url, ????);
}
unittest
{
MyResourceGet req;
req.name = "thisone";
assert(requestUrl(req) == "/my-resource/thisone");
}
```
In `requestUrl`, how do I actually get the value of `req.name`
from `paramsSymbols` and `req`?
Feb 26 2022
On Saturday, 26 February 2022 at 10:39:18 UTC, Remi Thebault wrote:Hi all, I'm trying to establish a REST API by using the type system (used in both client and server code). [...]https://dlang.org/phobos/std_traits.html#getUDAs
Feb 26 2022
On Saturday, 26 February 2022 at 11:26:54 UTC, max haughton wrote:On Saturday, 26 February 2022 at 10:39:18 UTC, Remi Thebault wrote:How do I use `getUDAs` in this context? I have `getUDAs!(req, Param).length == 0`. I think it only works on the struct attributes, not on the fields attributes.Hi all, I'm trying to establish a REST API by using the type system (used in both client and server code). [...]https://dlang.org/phobos/std_traits.html#getUDAs
Feb 26 2022
On Saturday, 26 February 2022 at 11:38:16 UTC, Remi Thebault wrote:On Saturday, 26 February 2022 at 11:26:54 UTC, max haughton wrote:Getting the UDAs from inside a symbol must be done via a recursive procedure in the same manner one would identify the aforementioned symbol i.e. you have to go through the fields looking for UDAs *then* use getUDAs. This is because UDAs cannot convey information without their context, so the trait doesn't look recursively.On Saturday, 26 February 2022 at 10:39:18 UTC, Remi Thebault wrote:How do I use `getUDAs` in this context? I have `getUDAs!(req, Param).length == 0`. I think it only works on the struct attributes, not on the fields attributes.Hi all, I'm trying to establish a REST API by using the type system (used in both client and server code). [...]https://dlang.org/phobos/std_traits.html#getUDAs
Feb 26 2022
On Saturday, 26 February 2022 at 12:01:14 UTC, max haughton wrote:Getting the UDAs from inside a symbol must be done via a recursive procedure in the same manner one would identify the aforementioned symbol i.e. you have to go through the fields looking for UDAs *then* use getUDAs. This is because UDAs cannot convey information without their context, so the trait doesn't look recursively.I don't need to get access to the UDA value, I need the value of the field decorated with UDA. Finally I can get access to the member using `__traits(getMember, req, paramSymbols[0].stringof)`.
Feb 26 2022
On Saturday, 26 February 2022 at 17:06:06 UTC, Remi Thebault wrote:On Saturday, 26 February 2022 at 12:01:14 UTC, max haughton wrote:I see, my apologies. FYI if possible you should avoid using stringof because it's basically only intended for debugging so isn't always consistent - this is what __traits(identifier) is forGetting the UDAs from inside a symbol must be done via a recursive procedure in the same manner one would identify the aforementioned symbol i.e. you have to go through the fields looking for UDAs *then* use getUDAs. This is because UDAs cannot convey information without their context, so the trait doesn't look recursively.I don't need to get access to the UDA value, I need the value of the field decorated with UDA. Finally I can get access to the member using `__traits(getMember, req, paramSymbols[0].stringof)`.
Feb 26 2022








max haughton <maxhaton gmail.com>