in a template, how can I get the parameter of a user defined attribute

Chris Bare chris at bareflix.com
Sat Nov 6 19:45:49 UTC 2021


In the example below, one of the attributes is 
@dbForeignKey!Position. pragma (msg, attr) gives me: 
dbForeignKey!(Position). I want to be able to extract the Postion 
as a type so I can call another template like: save!Position ();

Is this possible?

```d
import std.stdio;
import std.traits;
import std.format;

struct dbForeignKey(T)
{
}

struct Employee
{
	long id;
	string first_name;
	string last_name;
	@dbForeignKey!Position long default_position_id;
}

struct Position
{
	long id;
	string name;
}

void main()
{
	Employee e;

	structForm!Employee (e);

}

void structForm (T)(T data)
{
	string form = fullyQualifiedName!T;
		writeln ("form name = %s".format(form));
	string field_label;
	bool required;
		 foreach(field_name; FieldNameTuple!T)
		{
		writeln ("field name = %s".format(field_name));
			auto field_value = __traits(getMember, data, field_name);
		writeln ("field value = %s".format(field_value));
     	foreach (attr; __traits(getAttributes, __traits(getMember, 
data, field_name)))
		{
			pragma (msg, attr);
			writeln ("attr = %s".format (attr.stringof));
		}
		writeln ("-----------------------------------");
		}
}
```
The compiler prints this for the pragma is:
```
dbForeignKey!(Position)
```

the program's output is:
```
form name = test.Employee
field name = id
field value = 0
-----------------------------------
field name = first_name
field value =
-----------------------------------
field name = last_name
field value =
-----------------------------------
field name = default_position_id
field value = 0
attr = dbForeignKey!(Position)
-----------------------------------
```


More information about the Digitalmars-d-learn mailing list