v0.3.0-dev.2

This commit is contained in:
Alexander Zhirov 2023-03-27 15:37:37 +03:00
parent de3690c94e
commit 6952bab705
3 changed files with 43 additions and 14 deletions

View File

@ -169,6 +169,11 @@ struct ConfigFile
private string name = "[]"; private string name = "[]";
private ConfigSection[string] sections; private ConfigSection[string] sections;
@property bool exist()
{
return this.sections.length > 0;
}
/** /**
* Get the section * Get the section
* Params: * Params:
@ -176,6 +181,10 @@ struct ConfigFile
*/ */
@property ConfigSection sectionName(string section = "[]") @property ConfigSection sectionName(string section = "[]")
{ {
if (!this.exist)
throw new Exception("The configuration file does not exist");
if (sections.length == 1)
return sections[sections.byKey.front];
return section in sections ? sections[section] : ConfigSection(); return section in sections ? sections[section] : ConfigSection();
} }
@ -219,6 +228,8 @@ struct ConfigSection
*/ */
ConfigParameter key(string key) ConfigParameter key(string key)
{ {
if (this.empty)
throw new Exception("The selected section has no parameters or does not exist");
return key in this.parameters ? this.parameters[key] : ConfigParameter(); return key in this.parameters ? this.parameters[key] : ConfigParameter();
} }

5
tests/database.conf Normal file
View File

@ -0,0 +1,5 @@
[postgres]
host => "//myhost"
port => 5432
name => mydatabase
password => "/&#BD&@MXLE"

View File

@ -1,23 +1,36 @@
import readconf; import readconf;
import std.conv;
unittest unittest
{ {
rc.read("./tests/settings.conf"); rc.read("./tests/settings.conf", "old");
rc.read("./tests/settings.conf", "new"); rc.read("./tests/database.conf", "pgconf");
assert(rc.cf("settings.conf").sn.key("value1") == "text without quotes"); assert(rc.cf("old").sn.key("value1") == "text without quotes");
assert(rc.cf("settings.conf").sn.key("value2") == "Yes!"); assert(rc.cf("old").sn.key("value2") == "Yes!");
assert(rc.cf("settings.conf").sn.key("value3") == "value in apostrophes"); assert(rc.cf("old").sn.key("value3") == "value in apostrophes");
assert(rc.cf("settings.conf").sn.key("value4") == "1000"); assert(rc.cf("old").sn.key("value4") == "1000");
assert(rc.cf("settings.conf").sn.key("value5") == "0.000"); assert(rc.cf("old").sn.key("value5") == "0.000");
assert(rc.cf("settings.conf").sn.key("value7") == "//path"); assert(rc.cf("old").sn.key("value7") == "//path");
assert(rc.cf("settings.conf").sn.key("value8") == "\"Hey!\""); assert(rc.cf("old").sn.key("value8") == "\"Hey!\"");
assert(rc.cf("settings.conf").sn("part2").key("value1") == "this value will be in the new section"); assert(rc.cf("old").sn("part2").key("value1") == "this value will be in the new section");
assert(rc.cf("settings.conf").sn("part2").key("value3") == "good value!"); assert(rc.cf("old").sn("part2").key("value3") == "good value!");
assert(rc.cf("settings.conf").sn("part3").key("value1") == "-2"); assert(rc.cf("old").sn("part3").key("value1") == "-2");
assert(rc.cf("settings.conf").sn("part3").key("value3") == "100"); assert(rc.cf("old").sn("part3").key("value3") == "100");
assert(rc.cf("new").sn("part3").key("value3") == "100"); auto pgconf = rc.cf("pgconf").sn("postgres");
assert(pgconf.key("host") == "//myhost");
assert(pgconf.key("port").to!int == 5432);
assert(pgconf.key("name") == "mydatabase");
assert(pgconf.key("password") == "/&#BD&@MXLE");
auto pgconf2 = rc.cf("pgconf");
assert(pgconf2.sn.key("host") == "//myhost");
assert(pgconf2.sn.key("port").to!int == 5432);
assert(pgconf2.sn.key("name") == "mydatabase");
assert(pgconf2.sn.key("password") == "/&#BD&@MXLE");
} }
// void main() // void main()