Thread overview
in a template, how can I get the parameter of a user defined attribute
Nov 06, 2021
Chris Bare
Nov 06, 2021
Adam Ruppe
Nov 06, 2021
Chris Bare
November 06, 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?

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)
-----------------------------------
November 06, 2021

On Saturday, 6 November 2021 at 19:45:49 UTC, Chris Bare wrote:

>

dbForeignKey!(Position)

static if(is(T == dbForeignKey!Arg, Arg)) {
// use Arg here
}

November 06, 2021

On Saturday, 6 November 2021 at 19:51:09 UTC, Adam Ruppe wrote:

>

On Saturday, 6 November 2021 at 19:45:49 UTC, Chris Bare wrote:

>

dbForeignKey!(Position)

static if(is(T == dbForeignKey!Arg, Arg)) {
// use Arg here
}

Thanks, that did it.