setMemberByName

Given a struct/class object, sets one of its members by its string name to a specified value. Overload that takes the value as a string and tries to convert it into the target type.

It does not currently recurse into other struct/class members.

  1. auto setMemberByName(Thing thing, string memberToSet, string valueToSet)
    setMemberByName
    (
    Thing
    )
    (
    ref Thing thing
    ,
    const string memberToSet
    ,
    const string valueToSet
    )
    if (
    isAggregateType!Thing &&
    isMutable!Thing
    )
  2. auto setMemberByName(Thing thing, string memberToSet, Val valueToSet)

Parameters

thing Thing

Reference object whose members to set.

memberToSet string

String name of the thing's member to set.

valueToSet string

String contents of the value to set the member to; string even if the member is of a different type.

Return Value

Type: auto

true if a member was found and set, false if nothing was done.

Throws

ConvException if a string could not be converted into an array, if a passed string could not be converted into a bool, or if std.conv.to failed to convert a string into wanted type T. SetMemberException if an unexpected exception was thrown.

Examples

struct Foo
{
    string name;
    int number;
    bool alive;
}

Foo foo;

foo.setMemberByName("name", "James Bond");
foo.setMemberByName("number", "007");
foo.setMemberByName("alive", false);

assert(foo.name == "James Bond");
assert(foo.number == 7);
assert(!foo.alive);
1 import lu.uda : Separator;
2 import std.conv : to;
3 
4 {
5     struct Foo
6     {
7         string bar;
8         int baz;
9         float* f;
10         string[string] aa;
11 
12         @Separator("|")
13         @Separator(" ")
14         {
15             string[] arr;
16             string[] matey;
17         }
18 
19         @Separator(";;")
20         {
21             string[] parrots;
22             string[] withSpaces;
23         }
24 
25         @Separator(`\o/`)
26         {
27             string[] blurgh;
28         }
29 
30         @(`\o/`)
31         {
32             int[] blargh;
33         }
34     }
35 
36     Foo foo;
37     bool success;
38 
39     success = foo.setMemberByName("bar", "asdf fdsa adf");
40     assert(success);
41     assert((foo.bar == "asdf fdsa adf"), foo.bar);
42 
43     success = foo.setMemberByName("baz", "42");
44     assert(success);
45     assert((foo.baz == 42), foo.baz.to!string);
46 
47     success = foo.setMemberByName("aa", `["abc":"def", "ghi":"jkl"]`);
48     assert(success);
49     assert((foo.aa == [ "abc":"def", "ghi":"jkl" ]), foo.aa.to!string);
50 
51     success = foo.setMemberByName("arr", "herp|derp|dirp|darp");
52     assert(success);
53     assert((foo.arr == [ "herp", "derp", "dirp", "darp"]), foo.arr.to!string);
54 
55     success = foo.setMemberByName("arr", "herp derp dirp|darp");
56     assert(success);
57     assert((foo.arr == [ "herp", "derp", "dirp", "darp"]), foo.arr.to!string);
58 
59     success = foo.setMemberByName("matey", "this,should,not,be,separated");
60     assert(success);
61     assert((foo.matey == [ "this,should,not,be,separated" ]), foo.matey.to!string);
62 
63     success = foo.setMemberByName("parrots", "squaawk;;parrot sounds;;repeating");
64     assert(success);
65     assert((foo.parrots == [ "squaawk", "parrot sounds", "repeating"]),
66         foo.parrots.to!string);
67 
68     success = foo.setMemberByName("withSpaces", `         squoonk         ;;"  spaced  ";;" "`);
69     assert(success);
70     assert((foo.withSpaces == [ "squoonk", `  spaced  `, " "]),
71         foo.withSpaces.to!string);
72 
73     success = foo.setMemberByName("invalid", "oekwpo");
74     assert(!success);
75 
76     /*success = foo.setMemberByName("", "true");
77     assert(!success);*/
78 
79     success = foo.setMemberByName("matey", "hirr steff\\ stuff staff\\|stirf hooo");
80     assert(success);
81     assert((foo.matey == [ "hirr", "steff stuff", "staff|stirf", "hooo" ]), foo.matey.to!string);
82 
83     success = foo.setMemberByName("matey", "hirr steff\\\\ stuff staff\\\\|stirf hooo");
84     assert(success);
85     assert((foo.matey == [ "hirr", "steff\\", "stuff", "staff\\", "stirf", "hooo" ]), foo.matey.to!string);
86 
87     success = foo.setMemberByName("matey", "asdf\\ fdsa\\\\ hirr                                steff");
88     assert(success);
89     assert((foo.matey == [ "asdf fdsa\\", "hirr", "steff" ]), foo.matey.to!string);
90 
91     success = foo.setMemberByName("blurgh", "asdf\\\\o/fdsa\\\\\\o/hirr\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/steff");
92     assert(success);
93     assert((foo.blurgh == [ "asdf\\o/fdsa\\", "hirr", "steff" ]), foo.blurgh.to!string);
94 
95     success = foo.setMemberByName("blargh", `1\o/2\o/3\o/4\o/5`);
96     assert(success);
97     assert((foo.blargh == [ 1, 2, 3, 4, 5 ]), foo.blargh.to!string);
98 }
99 {
100     class C
101     {
102         string abc;
103         int def;
104     }
105 
106     C c = new C;
107     bool success;
108 
109     success = c.setMemberByName("abc", "this is abc");
110     assert(success);
111     assert((c.abc == "this is abc"), c.abc);
112 
113     success = c.setMemberByName("def", "42");
114     assert(success);
115     assert((c.def == 42), c.def.to!string);
116 }
117 {
118     import lu.conv : Enum;
119 
120     enum E { abc, def, ghi }
121 
122     struct S
123     {
124         E e = E.ghi;
125     }
126 
127     S s;
128     bool success;
129 
130     assert(s.e == E.ghi);
131     success = s.setMemberByName("e", "def");
132     assert(success);
133     assert((s.e == E.def), Enum!E.toString(s.e));
134 }
135 {
136     struct StructWithOpAssign
137     {
138         string thing = "init";
139 
140         void opAssign(const string thing)
141         {
142             this.thing = thing;
143         }
144     }
145 
146     StructWithOpAssign assignable;
147     assert((assignable.thing == "init"), assignable.thing);
148     assignable = "new thing";
149     assert((assignable.thing == "new thing"), assignable.thing);
150 
151     struct StructWithAssignableMember
152     {
153         StructWithOpAssign child;
154     }
155 
156     StructWithAssignableMember parent;
157     bool success;
158 
159     success = parent.setMemberByName("child", "flerp");
160     assert(success);
161     assert((parent.child.thing == "flerp"), parent.child.thing);
162 }
163 {
164     class ClassWithOpAssign
165     {
166         string thing = "init";
167 
168         void opAssign(const string thing) //@safe pure nothrow @nogc
169         {
170             this.thing = thing;
171         }
172     }
173 
174     class ClassWithAssignableMember
175     {
176         ClassWithOpAssign child;
177 
178         this()
179         {
180             child = new ClassWithOpAssign;
181         }
182     }
183 
184     ClassWithAssignableMember parent = new ClassWithAssignableMember;
185     bool success;
186 
187     success = parent.setMemberByName("child", "flerp");
188     assert(success);
189     assert((parent.child.thing == "flerp"), parent.child.thing);
190 }