v0.3.0-beta.1

This commit is contained in:
Alexander Zhirov 2023-03-30 09:58:28 +03:00
parent 1d89b62354
commit 1e45d1a503
6 changed files with 168 additions and 51 deletions

View File

@ -14,10 +14,12 @@ Singleton for reading the configuration file required for your program.
## What can do
1. Separation of parameter and value by separators `=` and `=>`
2. Commenting on lines using special characters `;`, `#`, `//`, `/*`
3. Support for sections for describing parameter blocks (sections are set by the name in `[]`)
4. Support for spaces and tabs for visual separation
- Reading multiple configuration files
- Separation of parameters by sections
- Access to parameters and sections using keys and indexes
- Commenting on lines
You will get more detailed information on the [wiki](https://git.zhirov.kz/dlang/readconf/wiki).
## Quick start
@ -25,7 +27,7 @@ The `settings.conf` file (see the [tests](tests/)):
![matches.png](img/matches.png)
Read `settings.conf` file:
Read `./tests/settings.conf` file:
```d
import readconf;
@ -33,7 +35,7 @@ import std.stdio;
void main()
{
rc.read("./settings.conf");
rc.read("./tests/settings.conf");
foreach (key, param; rc.sn.keys())
writefln("%s => %s", key, param);
@ -43,7 +45,7 @@ void main()
foreach (key, param; rc.sn("part2").keys())
writefln("%s => %s", key, param);
writeln(rc.sn("part2").key("value1"));
writeln(rc["part2"]["value1"]);
}
```
@ -63,6 +65,6 @@ value3 => good value!
this value will be in the new section
```
## Dub
## DUB
Add a dependency on `"readconf": "~>0.2.0"`
Add a dependency on `"readconf": "~>0.3.0"`

11
examples/comments.conf Normal file
View File

@ -0,0 +1,11 @@
This line will be a comment, since it does not match the basic template
parameter1 => value1 ; This will be a comment
parameter2 => value2 # This will be a comment
parameter3 => value3 // This will be a comment
parameter4 => value4 /* This will be a comment
parameter5 => value5;This will not be a comment
parameter6 => value6// This will also be a whole value
parameter7 => //value7 ;The value will not be read
parameter8 => "//value8" # Now the value is correctly
parameter9 => ';value9' // The value is correctly too
parameter10 => '"value10"' // Quotes inside

22
examples/sections.conf Normal file
View File

@ -0,0 +1,22 @@
parameter1 = value1
parameter_2 = value2
[first-section] ; Creating the first section
parameter1 = value3 ; A section may contain similar parameter names
parameter_2 = value4
[second-section] // Another section
parameter1 = value5
parameter_2 = value6
[] /* Addition of the main section
parameter3 = value7 /* A new parameter will be added
parameter1 = value8 /* And parameter1 will be redefined
[first-section] # Addition of the first section
parameter3 = value9
parameter4 = value10
[_section] # Creating the new section
parameter1 = value11
parameter2 = value12

16
examples/simple.conf Normal file
View File

@ -0,0 +1,16 @@
parameter1=value1
parameter2=>value2
parameter3=value3
parameter4=>value4
_parameter5 = value5
parameter6 => value6
parameter7 = value7
parameter8 => value8
parameter9 =value9
parameter-10 =>value10
parameter11 = value11
parameter12_ => value12

View File

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

View File

@ -3,48 +3,119 @@ import std.conv;
unittest
{
rc.read("./tests/settings.conf", "old");
rc.read("./tests/database.conf", "pgconf");
rc.read("./examples/simple.conf");
assert(rc.cf("old").sn.key("value1") == "text without quotes");
assert(rc["old"][""]["value2"] == "Yes!");
assert(rc.cf("old").sn.key("value3") == "value in apostrophes");
assert(rc.cf("old").sn.key("value4") == "1000");
assert(rc.cf("old").sn.key("value5") == "0.000");
assert(rc.cf("old").sn.key("value7") == "//path");
assert(rc.cf("old").sn.key("value8") == "\"Hey!\"");
assert(rc.cf("old").sn("part2").key("value1") == "this value will be in the new section");
assert(rc.cf("old").sn("part2").key("value3") == "good value!");
assert(rc.cf("old").sn("part3").key("value1") == "-2");
assert(rc.cf("old").sn("part3").key("value3") == "100");
auto configFile = rc.cf;
auto mainSection = configFile.sn;
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[]["host"] == "//myhost");
assert(pgconf2.sn.key("port").to!int == 5432);
assert(pgconf2.sn.key("name") == "mydatabase");
assert(pgconf2.sn.key("password") == "/&#BD&@MXLE");
assert(mainSection.key("parameter1") == "value1");
assert(mainSection["parameter2"] == "value2");
assert(mainSection.key("parameter3") == "value3");
assert(mainSection["parameter4"] == "value4");
assert(mainSection.key("_parameter5") == "value5");
assert(mainSection["parameter6"] == "value6");
assert(mainSection.key("parameter7") == "value7");
assert(mainSection["parameter8"] == "value8");
assert(mainSection.key("parameter9") == "value9");
assert(mainSection["parameter-10"] == "value10");
assert(mainSection.key("parameter11") == "value11");
assert(mainSection["parameter12_"] == "value12");
}
// void main()
// {
// import std.stdio;
// rc.read("./tests/settings.conf");
unittest
{
rc.read("./examples/sections.conf");
auto configFile = rc.cf;
// foreach (key, param; rc.sn.keys())
// writefln("%s => %s", key, param);
auto mainSection = configFile.sn;
auto firstSection = configFile.sn("first-section");
auto secondSection = configFile["second-section"];
auto section = configFile["_section"];
// writeln(rc.sn.key("value1"));
assert(mainSection.key("parameter1") == "value8");
assert(mainSection["parameter_2"] == "value2");
assert(mainSection["parameter3"] == "value7");
// foreach (key, param; rc.sn("part2").keys())
// writefln("%s => %s", key, param);
assert(firstSection["parameter1"] == "value3");
assert(firstSection["parameter_2"] == "value4");
assert(firstSection["parameter3"] == "value9");
assert(firstSection["parameter4"] == "value10");
// writeln(rc.sn("part2").key("value1"));
// }
assert(secondSection["parameter1"] == "value5");
assert(secondSection["parameter_2"] == "value6");
assert(section["parameter1"] == "value11");
assert(section["parameter2"] == "value12");
}
unittest
{
rc.read("./examples/simple.conf", "simple");
rc.read("./examples/sections.conf");
rc.read("./examples/comments.conf", "comments");
auto simpleConfig = rc.cf("simple");
auto sectionsConfig = rc["sections.conf"];
auto commentsConfig = rc["comments"];
auto simConMaiSec = simpleConfig.sn;
assert(simConMaiSec.key("parameter1") == "value1");
assert(simConMaiSec["parameter2"] == "value2");
assert(simConMaiSec.key("parameter3") == "value3");
assert(simConMaiSec["parameter4"] == "value4");
assert(simConMaiSec.key("_parameter5") == "value5");
assert(simConMaiSec["parameter6"] == "value6");
assert(simConMaiSec.key("parameter7") == "value7");
assert(simConMaiSec["parameter8"] == "value8");
assert(simConMaiSec.key("parameter9") == "value9");
assert(simConMaiSec["parameter-10"] == "value10");
assert(simConMaiSec.key("parameter11") == "value11");
assert(simConMaiSec["parameter12_"] == "value12");
auto secConMaiSec = sectionsConfig.sn;
auto secConFirSec = sectionsConfig.sn("first-section");
auto secConSecSec = sectionsConfig["second-section"];
auto secConSec = sectionsConfig["_section"];
assert(secConMaiSec.key("parameter1") == "value8");
assert(secConMaiSec["parameter_2"] == "value2");
assert(secConMaiSec["parameter3"] == "value7");
assert(secConFirSec["parameter1"] == "value3");
assert(secConFirSec["parameter_2"] == "value4");
assert(secConFirSec["parameter3"] == "value9");
assert(secConFirSec["parameter4"] == "value10");
assert(secConSecSec["parameter1"] == "value5");
assert(secConSecSec["parameter_2"] == "value6");
assert(secConSec["parameter1"] == "value11");
assert(secConSec["parameter2"] == "value12");
auto comConMaiSec = commentsConfig.sn;
assert(comConMaiSec["parameter1"] == "value1");
assert(comConMaiSec["parameter2"] == "value2");
assert(comConMaiSec["parameter3"] == "value3");
assert(comConMaiSec["parameter4"] == "value4");
assert(comConMaiSec["parameter5"] == "value5;This will not be a comment");
assert(comConMaiSec["parameter6"] == "value6// This will also be a whole value");
assert(comConMaiSec["parameter8"] == "//value8");
assert(comConMaiSec["parameter9"] == ";value9");
assert(comConMaiSec["parameter10"] == "\"value10\"");
}
unittest
{
rc.read("./tests/settings.conf");
assert(rc.cf.sn.key("value1") == "text without quotes");
assert(rc[][]["value2"] == "Yes!");
assert(rc.cf.sn.key("value3") == "value in apostrophes");
assert(rc[][]["value4"] == "1000");
assert(rc.cf.sn["value5"] == "0.000");
assert(rc[][].key("value7") == "//path");
assert(rc.cf.sn.key("value8") == "\"Hey!\"");
assert(rc[]["part2"]["value1"] == "this value will be in the new section");
assert(rc.cf.sn("part2").key("value3") == "good value!");
assert(rc[].sn("part3").key("value1") == "-2");
assert(rc.cf["part3"]["value3"] == "100");
}