www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to get instance member value from getSymbolsByUDA

reply Remi Thebault <remi.thebault gmail.com> writes:
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
parent reply max haughton <maxhaton gmail.com> writes:
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
parent reply Remi Thebault <remi.thebault gmail.com> writes:
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:
 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
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.
Feb 26 2022
parent reply max haughton <maxhaton gmail.com> writes:
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:
 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
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.
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.
Feb 26 2022
parent reply Remi Thebault <remi.thebault gmail.com> writes:
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
parent max haughton <maxhaton gmail.com> writes:
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:
 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)`.
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 for
Feb 26 2022