VariantConfig

Handles the processing of config files.

Members

Aliases

as
alias as = coerce

Simpler alias that conveys the meaning more appropriately.

asBool
alias asBool = coerce!bool

Gets the value and converts it to a bool.

asByte
alias asByte = coerce!byte

Gets the value and converts it to a byte.

asDouble
alias asDouble = coerce!double

Gets the value and converts it to a double.

asFloat
alias asFloat = coerce!float

Gets the value and converts it to a float.

asInt
alias asInt = coerce!int

Gets the value and converts it to a int.

asLong
alias asLong = coerce!long

Gets the value and converts it to a long.

asReal
alias asReal = coerce!real

Gets the value and converts it to a real.

asShort
alias asShort = coerce!short

Gets the value and converts it to a short.

asString
alias asString = coerce!string

Gets the value and converts it to a string.

Functions

coerce
T coerce(string key, T defaultValue)

Converts the value of key to type of T. Works the same as std.variant's coerce.

contains
bool contains(string key)

Determines if the key is found in the config file. The key can be either its name of in the format of groupName.keyName or just the key name.

contains
bool contains(string group, string key)

Determines if the key is found in the config file.

containsGroup
bool containsGroup(string group)

Determines if the given group exists.

get
Variant get(string key, T defaultValue)

Retrieves the value T associated with key where T is the designated type to be converted to.

get
Variant get(string group, string key, T defaultValue)

Retrieves the value T associated with key where T is the designated type to be converted to.

getGroup
auto getGroup(string group)

Retrieves key/values associated with the group portion of a config file/string.

getGroupValue
Variant getGroupValue(string group, string key, T defaultValue)

Gets the value associated with the group and key.

getGroups
auto getGroups()

Retrieves an array containing key/values of all groups in the configfile omitting groupless key/values.

loadFile
bool loadFile(string fileName)

Loads a config fileName(app.config by default) to be processed.

loadString
bool loadString(string text)

Similar to loadFile but loads and processes the passed string instead.

opIndex
Variant opIndex(string key)

Allows config values to be accessed as you would with an associative array.

opIndexAssign
void opIndexAssign(T value, string key)

Allows config values to be assigned as you would with an associative array.

remove
bool remove(string key)

Removes a key/value from config file. The key can be either its name of in the format of groupName.keyName or just the keyName.

remove
bool remove(string group, string key)

Removes a key/value from config file. The key can be either its name of in the format of group.keyor just the key.

removeGroup
bool removeGroup(string group)

Removes a group from the config file.

save
void save()

Saves config values to the config file used when loading(loadFile).

save
void save(string fileName)

Saves config values to the config file.

set
void set(string key, T value)

Sets a config value.

set
void set(string group, string key, T value)

Sets a config value.

Examples

1 string text = "
2 	aBool=true
3 	decimal = 3443.443
4 	number=12071
5 	#Here is a comment
6 	sentence=This is a really long sentence to test for a really long value string!
7 	time=12:04
8 	[section]
9 	groupSection=is really cool if this works!
10 	japan=true
11 	babymetal=the one
12 	[another]
13 	#And another comment!
14 	world=hello
15 	japan=false
16 ";
17 
18 VariantConfig config;
19 
20 immutable bool loaded = config.loadString(text);
21 assert(loaded, "Failed to load string!");
22 
23 assert(config.containsGroup("section"));
24 config.removeGroup("section");
25 assert(config.containsGroup("section") == false);
26 
27 assert(config.get("aBool", true).coerce!bool == true);
28 assert(config.asBool("aBool")); // Syntactic sugar
29 assert(config["aBool"].coerce!bool == true); // Also works but rather awkward
30 assert(config.coerce!bool("aBool") == true); // Also works and more natural
31 
32 assert(config.contains("time"));
33 
34 immutable auto number = config["number"];
35 
36 assert(number == 12_071);
37 assert(config["decimal"] == 3443.443);
38 
39 assert(config.contains("another.world"));
40 assert(config["another.world"] == "hello");
41 
42 config["another.japan"] = true;
43 assert(config["another.japan"] == true);
44 
45 config.remove("another.world");
46 
47 assert(config.contains("another.world") == false);
48 assert(config.contains("anothers", "world") == false);
49 
50 assert(config.contains("number"));
51 config.remove("number");
52 assert(config.contains("number") == false);
53 
54 // Tests for nonexistent keys
55 assert(config.asString("nonexistent", "Value doesn't exist!") == "Value doesn't exist!");
56 config["nonexistent"] = "The value now exists!!!";
57 assert(config.asString("nonexistent", "The value now exists!!!") == "The value now exists!!!");
58 
59 auto group = config.getGroup("another");
60 
61 foreach(value; group)
62 {
63 	assert(value.key == "japan");
64 	assert(value.value == true);
65 }
66 
67 config.set("aBool", false);
68 assert(config["aBool"] == false);
69 debug config.save();
70 
71 config["aBool"] = true;
72 assert(config["aBool"] == true);
73 assert(config["aBool"].toString == "true");
74 
75 assert(config.get!int("numberGroup", "numberValue", 1234) == 1234);
76 
77 immutable string customFileName = "custom-config-format.dat";
78 debug config.save(customFileName);
79 
80 VariantConfig configLoadTest;
81 
82 bool isLoadedTest = configLoadTest.loadFile("doesnt-exist.dat");
83 assert(isLoadedTest == false);
84 
85 isLoadedTest = configLoadTest.loadFile(customFileName);
86 assert(isLoadedTest == true);
87 
88 string noEqualSign = "
89 	equal=sign
90 	time=12:04
91 	This is a really long sentence to test for a really long value string!
92 ";
93 
94 immutable bool equalSignValue = config.loadString(noEqualSign);
95 assert(equalSignValue == false);
96 
97 auto groups = config.getGroups();
98 
99 writeln("Listing groups: ");
100 writeln;
101 
102 foreach(currGroup; groups)
103 {
104 	writeln(currGroup);
105 }
106 
107 string invalidGroup = "
108 	[first]
109 	equal=sign
110 	time=12:04
111 	[second
112 	another=key value is here
113 ";
114 
115 immutable bool invalidGroupValue = config.loadString(invalidGroup);
116 assert(invalidGroupValue == false);
117 
118 string emptyString;
119 
120 immutable bool emptyLoad = config.loadString(emptyString);
121 assert(emptyLoad == false);

Meta