Skip to content

Commit 1b03edd

Browse files
committed
Don't generate field in SecondarySources types if field has 'ignore' pragma
1 parent a32d811 commit 1b03edd

1 file changed

Lines changed: 36 additions & 28 deletions

File tree

confutils/config_file.nim

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,21 @@ type
3838
isCommandOrArgument: bool
3939
isCaseBranch: bool
4040
isDiscriminator: bool
41+
isIgnore: bool
4142

42-
GeneratedFieldInfo = tuple
43+
GeneratedFieldInfo = object
44+
isIgnore: bool
4345
isCommandOrArgument: bool
4446
path: seq[string]
4547

4648
OriginalToGeneratedFields = OrderedTable[string, GeneratedFieldInfo]
4749

50+
SectionParam = object
51+
isCommandOrArgument: bool
52+
isIgnore: bool
53+
defaultValue: string
54+
namePragma: string
55+
4856
{.push gcsafe, raises: [].}
4957

5058
func isOption(n: NimNode): bool =
@@ -69,26 +77,25 @@ proc generateOptionalField(fieldName: NimNode, fieldType: NimNode): NimNode =
6977
let right = if isOption(fieldType): fieldType else: makeOption(fieldType)
7078
newIdentDefs(fieldName, right)
7179

72-
proc traverseIdent(ident: NimNode, typ: NimNode, isDiscriminator: bool,
73-
isCommandOrArgument = false, defaultValue = "",
74-
namePragma = ""): ConfFileSection =
80+
proc traverseIdent(ident: NimNode, typ: NimNode,
81+
isDiscriminator: bool, param = SectionParam()): ConfFileSection =
7582
ident.expectKind nnkIdent
76-
ConfFileSection(fieldName: $ident, namePragma: namePragma, typ: typ,
77-
defaultValue: defaultValue, isCommandOrArgument: isCommandOrArgument,
78-
isDiscriminator: isDiscriminator)
83+
ConfFileSection(fieldName: $ident,
84+
namePragma: param.namePragma, typ: typ,
85+
defaultValue: param.defaultValue,
86+
isCommandOrArgument: param.isCommandOrArgument,
87+
isDiscriminator: isDiscriminator,
88+
isIgnore: param.isIgnore)
7989

8090
proc traversePostfix(postfix: NimNode, typ: NimNode, isDiscriminator: bool,
81-
isCommandOrArgument = false, defaultValue = "",
82-
namePragma = ""): ConfFileSection =
91+
param = SectionParam()): ConfFileSection =
8392
postfix.expectKind nnkPostfix
8493

8594
case postfix[1].kind
8695
of nnkIdent:
87-
traverseIdent(postfix[1], typ, isDiscriminator, isCommandOrArgument,
88-
defaultValue, namePragma)
96+
traverseIdent(postfix[1], typ, isDiscriminator, param)
8997
of nnkAccQuoted:
90-
traverseIdent(postfix[1][0], typ, isDiscriminator, isCommandOrArgument,
91-
defaultValue, namePragma)
98+
traverseIdent(postfix[1][0], typ, isDiscriminator, param)
9299
else:
93100
raiseAssert "[Postfix] Unsupported child node:\n" & postfix[1].treeRepr
94101

@@ -98,8 +105,7 @@ proc shortEnumName(n: NimNode): NimNode =
98105
else:
99106
n
100107

101-
proc traversePragma(pragma: NimNode):
102-
tuple[isCommandOrArgument: bool, defaultValue, namePragma: string] =
108+
proc traversePragma(pragma: NimNode): SectionParam =
103109
pragma.expectKind nnkPragma
104110
var child: NimNode
105111

@@ -120,6 +126,8 @@ proc traversePragma(pragma: NimNode):
120126
let sym = $child
121127
if sym == "command" or sym == "argument":
122128
result.isCommandOrArgument = true
129+
elif sym == "ignore":
130+
result.isIgnore = true
123131
of nnkExprColonExpr:
124132
let pragma = $child[0]
125133
if pragma == "defaultValue":
@@ -132,19 +140,15 @@ proc traversePragma(pragma: NimNode):
132140
proc traversePragmaExpr(pragmaExpr: NimNode, typ: NimNode,
133141
isDiscriminator: bool): ConfFileSection =
134142
pragmaExpr.expectKind nnkPragmaExpr
135-
let (isCommandOrArgument, defaultValue, namePragma) =
136-
traversePragma(pragmaExpr[1])
143+
let param = traversePragma(pragmaExpr[1])
137144

138145
case pragmaExpr[0].kind
139146
of nnkIdent:
140-
traverseIdent(pragmaExpr[0], typ, isDiscriminator, isCommandOrArgument,
141-
defaultValue, namePragma)
147+
traverseIdent(pragmaExpr[0], typ, isDiscriminator, param)
142148
of nnkAccQuoted:
143-
traverseIdent(pragmaExpr[0][0], typ, isDiscriminator, isCommandOrArgument,
144-
defaultValue, namePragma)
149+
traverseIdent(pragmaExpr[0][0], typ, isDiscriminator, param)
145150
of nnkPostfix:
146-
traversePostfix(pragmaExpr[0], typ, isDiscriminator, isCommandOrArgument,
147-
defaultValue, namePragma)
151+
traversePostfix(pragmaExpr[0], typ, isDiscriminator, param)
148152
else:
149153
raiseAssert "[PragmaExpr] Unsupported expression:\n" & pragmaExpr.treeRepr
150154

@@ -239,7 +243,7 @@ proc generateTypes(root: ConfFileSection): seq[NimNode] =
239243
result.add getAst(objectDecl(genSym(nskType, root.fieldName)))[0]
240244
var recList = newNimNode(nnkRecList)
241245
for child in root.children:
242-
if child.isCommandOrArgument:
246+
if child.isCommandOrArgument or child.isIgnore:
243247
continue
244248
if child.isCaseBranch:
245249
if child.children.len > 0:
@@ -255,7 +259,11 @@ proc generateSettersPaths(node: ConfFileSection,
255259
pathsCache: var seq[string]) =
256260
pathsCache.add node.getRenamedName
257261
if node.children.len == 0:
258-
result[node.fieldName] = (node.isCommandOrArgument, pathsCache)
262+
result[node.fieldName] = GeneratedFieldInfo(
263+
isIgnore: node.isIgnore,
264+
isCommandOrArgument: node.isCommandOrArgument,
265+
path: pathsCache,
266+
)
259267
else:
260268
for child in node.children:
261269
generateSettersPaths(child, result, pathsCache)
@@ -279,16 +287,16 @@ proc generateSetters(confType, CF: NimNode, fieldsPaths: OriginalToGeneratedFiel
279287
numSetters = 0
280288

281289
let c = "c".ident
282-
for field, (isCommandOrArgument, path) in fieldsPaths:
283-
if isCommandOrArgument:
290+
for field, param in fieldsPaths:
291+
if param.isCommandOrArgument or param.isIgnore:
284292
assignments.add quote do:
285293
result.setters[`numSetters`] = defaultConfigFileSetter
286294
inc numSetters
287295
continue
288296

289297
var fieldPath = c
290298
var condition: NimNode
291-
for fld in path:
299+
for fld in param.path:
292300
fieldPath = newDotExpr(fieldPath, fld.ident)
293301
let fieldChecker = newDotExpr(fieldPath, "isSome".ident)
294302
if condition == nil:

0 commit comments

Comments
 (0)