Skip to content

Commit

Permalink
Add a getter for all defined Sections in parsecfg (#15450)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardek66 authored Mar 28, 2021
1 parent eb3ed44 commit 207bcab
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## Standard library additions and changes

- Added `sections` iterator in `parsecfg`.

- Make custom op in macros.quote work for all statements.

- On Windows the SSL library now checks for valid certificates.
Expand Down
6 changes: 6 additions & 0 deletions lib/pure/parsecfg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ runnableExamples:

import strutils, lexbase, streams, tables
import std/private/decode_helpers
import std/private/since

include "system/inclrtl"

Expand Down Expand Up @@ -649,3 +650,8 @@ proc delSectionKey*(dict: var Config, section, key: string) =
dict.del(section)
else:
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
23 changes: 9 additions & 14 deletions tests/stdlib/tparsecfg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ discard """
targets: "c js"
"""

import parsecfg, streams
import parsecfg, streams, sequtils

when not defined(js):
from stdtest/specialpaths import buildDir
Expand Down Expand Up @@ -39,19 +39,14 @@ var ss = newStringStream()
dict1.writeConfig(ss)

## Reading a configuration file.
var dict2 = loadConfig(newStringStream(ss.data))
var charset = dict2.getSectionValue("", "charset")
var threads = dict2.getSectionValue("Package", "--threads")
var pname = dict2.getSectionValue("Package", "name")
var name = dict2.getSectionValue("Author", "name")
var qq = dict2.getSectionValue("Author", "qq")
var email = dict2.getSectionValue("Author", "email")
doAssert charset == "utf-8"
doAssert threads == "on"
doAssert pname == "hello"
doAssert name == "lihf8515"
doAssert qq == "10214028"
doAssert email == "lihaifeng@wxm.com"
let dict2 = loadConfig(newStringStream(ss.data))
doAssert dict2.getSectionValue("", "charset") == "utf-8"
doAssert dict2.getSectionValue("Package", "--threads") == "on"
doAssert dict2.getSectionValue("Package", "name") == "hello"
doAssert dict2.getSectionValue("Author", "name") == "lihf8515"
doAssert dict2.getSectionValue("Author", "qq") == "10214028"
doAssert dict2.getSectionValue("Author", "email") == "lihaifeng@wxm.com"
doAssert toSeq(dict2.sections) == @["", "Package", "Author"]

## Modifying a configuration file.
var dict3 = loadConfig(newStringStream(ss.data))
Expand Down

0 comments on commit 207bcab

Please sign in to comment.