Reference object whose members to set.
String name of the thing's member to set.
String contents of the value to set the member to; string even if the member is of a different type.
true if a member was found and set, false if nothing was done.
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.
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 struct Foo 5 { 6 string bar; 7 int baz; 8 float* f; 9 string[string] aa; 10 11 @Separator("|") 12 @Separator(" ") 13 { 14 string[] arr; 15 string[] matey; 16 } 17 18 @Separator(";;") 19 { 20 string[] parrots; 21 string[] withSpaces; 22 } 23 24 @Separator(`\o/`) 25 { 26 string[] blurgh; 27 } 28 29 static if (__VERSION__ >= 2087L) 30 { 31 @(`\o/`) 32 { 33 int[] blargh; 34 } 35 } 36 } 37 38 Foo foo; 39 bool success; 40 41 success = foo.setMemberByName("bar", "asdf fdsa adf"); 42 assert(success); 43 assert((foo.bar == "asdf fdsa adf"), foo.bar); 44 45 success = foo.setMemberByName("baz", "42"); 46 assert(success); 47 assert((foo.baz == 42), foo.baz.to!string); 48 49 success = foo.setMemberByName("aa", `["abc":"def", "ghi":"jkl"]`); 50 assert(success); 51 assert((foo.aa == [ "abc":"def", "ghi":"jkl" ]), foo.aa.to!string); 52 53 success = foo.setMemberByName("arr", "herp|derp|dirp|darp"); 54 assert(success); 55 assert((foo.arr == [ "herp", "derp", "dirp", "darp"]), foo.arr.to!string); 56 57 success = foo.setMemberByName("arr", "herp derp dirp|darp"); 58 assert(success); 59 assert((foo.arr == [ "herp", "derp", "dirp", "darp"]), foo.arr.to!string); 60 61 success = foo.setMemberByName("matey", "this,should,not,be,separated"); 62 assert(success); 63 assert((foo.matey == [ "this,should,not,be,separated" ]), foo.matey.to!string); 64 65 success = foo.setMemberByName("parrots", "squaawk;;parrot sounds;;repeating"); 66 assert(success); 67 assert((foo.parrots == [ "squaawk", "parrot sounds", "repeating"]), 68 foo.parrots.to!string); 69 70 success = foo.setMemberByName("withSpaces", ` squoonk ;;" spaced ";;" "`); 71 assert(success); 72 assert((foo.withSpaces == [ "squoonk", ` spaced `, " "]), 73 foo.withSpaces.to!string); 74 75 success = foo.setMemberByName("invalid", "oekwpo"); 76 assert(!success); 77 78 /*success = foo.setMemberByName("", "true"); 79 assert(!success);*/ 80 81 success = foo.setMemberByName("matey", "hirr steff\\ stuff staff\\|stirf hooo"); 82 assert(success); 83 assert((foo.matey == [ "hirr", "steff stuff", "staff|stirf", "hooo" ]), foo.matey.to!string); 84 85 success = foo.setMemberByName("matey", "hirr steff\\\\ stuff staff\\\\|stirf hooo"); 86 assert(success); 87 assert((foo.matey == [ "hirr", "steff\\", "stuff", "staff\\", "stirf", "hooo" ]), foo.matey.to!string); 88 89 success = foo.setMemberByName("matey", "asdf\\ fdsa\\\\ hirr steff"); 90 assert(success); 91 assert((foo.matey == [ "asdf fdsa\\", "hirr", "steff" ]), foo.matey.to!string); 92 93 success = foo.setMemberByName("blurgh", "asdf\\\\o/fdsa\\\\\\o/hirr\\o/\\o/\\o/\\o/\\o/\\o/\\o/\\o/steff"); 94 assert(success); 95 assert((foo.blurgh == [ "asdf\\o/fdsa\\", "hirr", "steff" ]), foo.blurgh.to!string); 96 97 static if (__VERSION__ >= 2087L) 98 { 99 success = foo.setMemberByName("blargh", `1\o/2\o/3\o/4\o/5`); 100 assert(success); 101 assert((foo.blargh == [ 1, 2, 3, 4, 5 ]), foo.blargh.to!string); 102 } 103 104 class C 105 { 106 string abc; 107 int def; 108 } 109 110 C c = new C; 111 112 success = c.setMemberByName("abc", "this is abc"); 113 assert(success); 114 assert((c.abc == "this is abc"), c.abc); 115 116 success = c.setMemberByName("def", "42"); 117 assert(success); 118 assert((c.def == 42), c.def.to!string); 119 120 import lu.conv : Enum; 121 122 enum E { abc, def, ghi } 123 124 struct S 125 { 126 E e = E.ghi; 127 } 128 129 S s; 130 131 assert(s.e == E.ghi); 132 success = s.setMemberByName("e", "def"); 133 assert(success); 134 assert((s.e == E.def), Enum!E.toString(s.e)); 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 success = parent.setMemberByName("child", "flerp"); 158 assert(success); 159 assert((parent.child.thing == "flerp"), parent.child.thing); 160 161 class ClassWithOpAssign 162 { 163 string thing = "init"; 164 165 void opAssign(const string thing) //@safe pure nothrow @nogc 166 { 167 this.thing = thing; 168 } 169 } 170 171 class ClassWithAssignableMember 172 { 173 ClassWithOpAssign child; 174 175 this() 176 { 177 child = new ClassWithOpAssign; 178 } 179 } 180 181 ClassWithAssignableMember parent2 = new ClassWithAssignableMember; 182 success = parent2.setMemberByName("child", "flerp"); 183 assert(success); 184 assert((parent2.child.thing == "flerp"), parent2.child.thing);
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.