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