v0.3.0-dev.4

This commit is contained in:
Alexander Zhirov 2023-03-27 18:32:29 +03:00
parent d9cf28a2a8
commit fb3550b8ec
2 changed files with 40 additions and 2 deletions

View File

@ -164,6 +164,20 @@ public:
* configName = config name (by default the name of the configuration file) * configName = config name (by default the name of the configuration file)
*/ */
alias cf = configFile; alias cf = configFile;
@property ConfigFile opIndex(string configName = "")
{
if (configName.length == 0)
{
if (configs.length == 1)
return configs[configs.byKey.front];
else
throw new Exception("More than one configuration file has been read. "
~ "It is necessary to specify the name of a specific");
}
return configName in configs ? configs[configName] : ConfigFile(configName);
}
} }
struct ConfigFile struct ConfigFile
@ -187,6 +201,8 @@ struct ConfigFile
throw new Exception("The configuration file does not exist"); throw new Exception("The configuration file does not exist");
if (section == mainSection && sections.length == 1) if (section == mainSection && sections.length == 1)
return sections[sections.byKey.front]; return sections[sections.byKey.front];
if (section.length == 0)
section = mainSection;
return section in sections ? sections[section] : ConfigSection(section); return section in sections ? sections[section] : ConfigSection(section);
} }
@ -206,6 +222,17 @@ struct ConfigFile
this.sections[sectionName].add(parameter); this.sections[sectionName].add(parameter);
} }
@property ConfigSection opIndex(string section = mainSection)
{
if (!this.exist)
throw new Exception("The configuration file does not exist");
if (section == mainSection && sections.length == 1)
return sections[sections.byKey.front];
if (section.length == 0)
section = mainSection;
return section in sections ? sections[section] : ConfigSection(section);
}
} }
struct ConfigSection struct ConfigSection
@ -230,6 +257,8 @@ struct ConfigSection
*/ */
ConfigParameter key(string key) ConfigParameter key(string key)
{ {
if (key.length == 0)
throw new Exception("The key cannot be empty");
if (this.empty) if (this.empty)
throw new Exception("The selected section has no parameters or does not exist"); throw new Exception("The selected section has no parameters or does not exist");
return key in this.parameters ? this.parameters[key] : ConfigParameter(key); return key in this.parameters ? this.parameters[key] : ConfigParameter(key);
@ -250,6 +279,15 @@ struct ConfigSection
Log.msg.warning("The parameter exists but will be overwritten"); Log.msg.warning("The parameter exists but will be overwritten");
this.parameters[parameter.property] = parameter; this.parameters[parameter.property] = parameter;
} }
ConfigParameter opIndex(string key)
{
if (key.length == 0)
throw new Exception("The key cannot be empty");
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(key);
}
} }
/** /**

View File

@ -7,7 +7,7 @@ unittest
rc.read("./tests/database.conf", "pgconf"); rc.read("./tests/database.conf", "pgconf");
assert(rc.cf("old").sn.key("value1") == "text without quotes"); assert(rc.cf("old").sn.key("value1") == "text without quotes");
assert(rc.cf("old").sn.key("value2") == "Yes!"); assert(rc["old"][""]["value2"] == "Yes!");
assert(rc.cf("old").sn.key("value3") == "value in apostrophes"); assert(rc.cf("old").sn.key("value3") == "value in apostrophes");
assert(rc.cf("old").sn.key("value4") == "1000"); assert(rc.cf("old").sn.key("value4") == "1000");
assert(rc.cf("old").sn.key("value5") == "0.000"); assert(rc.cf("old").sn.key("value5") == "0.000");
@ -27,7 +27,7 @@ unittest
auto pgconf2 = rc.cf("pgconf"); auto pgconf2 = rc.cf("pgconf");
assert(pgconf2.sn.key("host") == "//myhost"); assert(pgconf2[]["host"] == "//myhost");
assert(pgconf2.sn.key("port").to!int == 5432); assert(pgconf2.sn.key("port").to!int == 5432);
assert(pgconf2.sn.key("name") == "mydatabase"); assert(pgconf2.sn.key("name") == "mydatabase");
assert(pgconf2.sn.key("password") == "/&#BD&@MXLE"); assert(pgconf2.sn.key("password") == "/&#BD&@MXLE");