Skip to content

Commit 0dc585a

Browse files
authored
Allow passing params to file decode (#132)
* Allow passing params to file decode * wip * wip
1 parent bede84f commit 0dc585a

3 files changed

Lines changed: 60 additions & 12 deletions

File tree

confutils.nim

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -979,29 +979,67 @@ macro configurationRtti(RecordType: type): untyped =
979979
result = newTree(nnkPar, newLitFixed cmdInfo, fieldSetters)
980980

981981
when hasSerialization:
982-
proc addConfigFile*(secondarySources: auto,
983-
Format: type,
984-
path: InputFile) {.raises: [ConfigurationError].} =
982+
template addConfigFileImpl(
983+
secondarySources: auto,
984+
Format: type SerializationFormat,
985+
path: InputFile,
986+
params: varargs[untyped]
987+
): untyped =
985988
try:
986-
secondarySources.data.add loadFile(Format, string path,
987-
type(secondarySources.data[0]))
989+
secondarySources.data.add loadFile(
990+
Format, string path, typeof(secondarySources.data[0]), params
991+
)
988992
except SerializationError as err:
989993
raise newException(ConfigurationError, err.formatMsg(string path), err)
990994
except IOError as err:
991995
raise newException(ConfigurationError,
992996
"Failed to read config file at '" & string(path) & "': " & err.msg)
993997

994-
proc addConfigFileContent*(secondarySources: auto,
995-
Format: type,
996-
content: string) {.raises: [ConfigurationError].} =
998+
template addConfigFileWithParams*(
999+
secondarySources: auto,
1000+
Format: type SerializationFormat,
1001+
path: InputFile,
1002+
params: varargs[untyped]
1003+
): untyped =
1004+
addConfigFileImpl(secondarySources, Format, path, params)
1005+
1006+
proc addConfigFile*(
1007+
secondarySources: auto,
1008+
Format: type SerializationFormat,
1009+
path: InputFile
1010+
) {.raises: [ConfigurationError].} =
1011+
addConfigFileImpl(secondarySources, Format, path)
1012+
1013+
template addConfigFileContentImpl(
1014+
secondarySources: auto,
1015+
Format: type SerializationFormat,
1016+
content: string,
1017+
params: varargs[untyped]
1018+
): untyped =
9971019
try:
998-
secondarySources.data.add decode(Format, content,
999-
type(secondarySources.data[0]))
1020+
secondarySources.data.add decode(
1021+
Format, content, type(secondarySources.data[0], params)
1022+
)
10001023
except SerializationError as err:
10011024
raise newException(ConfigurationError, err.formatMsg("<content>"), err)
10021025
except IOError:
10031026
raiseAssert "This should not be possible"
10041027

1028+
template addConfigFileContentWithParams*(
1029+
secondarySources: auto,
1030+
Format: type SerializationFormat,
1031+
content: string,
1032+
params: varargs[untyped]
1033+
): untyped =
1034+
addConfigFileContentImpl(secondarySources, Format, content, params)
1035+
1036+
proc addConfigFileContent*(
1037+
secondarySources: auto,
1038+
Format: type SerializationFormat,
1039+
content: string
1040+
) {.raises: [ConfigurationError].} =
1041+
addConfigFileContentImpl(secondarySources, Format, content)
1042+
10051043
func constructEnvKey*(prefix: string, key: string): string {.raises: [].} =
10061044
## Generates env. variable names from keys and prefix following the
10071045
## IEEE Open Group env. variable spec: https://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html

tests/config_files/nested_cmd.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
outer-arg = "toml outer-arg"
2-
outerCmd1 = { outer-arg1 = "toml outer-arg1", innerCmd1 = { inner-arg1 = "toml inner-arg1" }, innerCmd2 = { inner-arg2 = "toml inner-arg2" } }
2+
outerCmd1 = {
3+
outer-arg1 = "toml outer-arg1",
4+
innerCmd1 = {
5+
inner-arg1 = "toml inner-arg1"
6+
},
7+
innerCmd2 = {
8+
inner-arg2 = "toml inner-arg2"
9+
}
10+
}

tests/test_nested_cmd.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ template loadFile(T, file): untyped =
1515
proc (
1616
config: T, sources: ref SecondarySources
1717
) {.raises: [ConfigurationError].} =
18-
sources.addConfigFile(Toml, InputFile(configFilePath / file))
18+
sources.addConfigFileWithParams(
19+
Toml, InputFile(configFilePath / file), flags = {TomlInlineTableNewline}
20+
)
1921

2022
type
2123
OuterCmd = enum

0 commit comments

Comments
 (0)