Add a getter for all defined Sections in parsecfg (#15450)

This commit is contained in:
Ardek Romak 2021-03-28 19:57:40 +03:00 • committed by GitHub
commit 207bcabdf2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 14 deletions

View file

@ -4,6 +4,8 @@
## Standard library additions and changes ## Standard library additions and changes
- Added `sections` iterator in `parsecfg`.
- Make custom op in macros.quote work for all statements. - Make custom op in macros.quote work for all statements.
- On Windows the SSL library now checks for valid certificates. - On Windows the SSL library now checks for valid certificates.

View file

@ -174,6 +174,7 @@ runnableExamples:
import strutils, lexbase, streams, tables import strutils, lexbase, streams, tables
import std/private/decode_helpers import std/private/decode_helpers
import std/private/since
include "system/inclrtl" include "system/inclrtl"
@ -649,3 +650,8 @@ proc delSectionKey*(dict: var Config, section, key: string) =
dict.del(section) dict.del(section)
else: else:
dict[section].del(key) dict[section].del(key)
iterator sections*(dict: Config): lent string {.since: (1, 5).} =
## Iterates through the sections in the `dict`.
for section in dict.keys:
yield section

View file

@ -2,7 +2,7 @@ discard """
targets: "c js" targets: "c js"
""" """
import parsecfg, streams import parsecfg, streams, sequtils
when not defined(js): when not defined(js):
from stdtest/specialpaths import buildDir from stdtest/specialpaths import buildDir
@ -39,19 +39,14 @@ var ss = newStringStream()
dict1.writeConfig(ss) dict1.writeConfig(ss)
## Reading a configuration file. ## Reading a configuration file.
var dict2 = loadConfig(newStringStream(ss.data)) let dict2 = loadConfig(newStringStream(ss.data))
var charset = dict2.getSectionValue("", "charset") doAssert dict2.getSectionValue("", "charset") == "utf-8"
var threads = dict2.getSectionValue("Package", "--threads") doAssert dict2.getSectionValue("Package", "--threads") == "on"
var pname = dict2.getSectionValue("Package", "name") doAssert dict2.getSectionValue("Package", "name") == "hello"
var name = dict2.getSectionValue("Author", "name") doAssert dict2.getSectionValue("Author", "name") == "lihf8515"
var qq = dict2.getSectionValue("Author", "qq") doAssert dict2.getSectionValue("Author", "qq") == "10214028"
var email = dict2.getSectionValue("Author", "email") doAssert dict2.getSectionValue("Author", "email") == "lihaifeng@wxm.com"
doAssert charset == "utf-8" doAssert toSeq(dict2.sections) == @["", "Package", "Author"]
doAssert threads == "on"
doAssert pname == "hello"
doAssert name == "lihf8515"
doAssert qq == "10214028"
doAssert email == "lihaifeng@wxm.com"
## Modifying a configuration file. ## Modifying a configuration file.
var dict3 = loadConfig(newStringStream(ss.data)) var dict3 = loadConfig(newStringStream(ss.data))