parent
772ed5eff2
commit
33bfe5b12c
2 changed files with 18 additions and 2 deletions
|
|
@ -14,7 +14,7 @@ from typetraits import OrdinalEnum, HoleyEnum
|
||||||
|
|
||||||
macro genEnumCaseStmt*(typ: typedesc, argSym: typed, default: typed,
|
macro genEnumCaseStmt*(typ: typedesc, argSym: typed, default: typed,
|
||||||
userMin, userMax: static[int], normalizer: static[proc(s :string): string]): untyped =
|
userMin, userMax: static[int], normalizer: static[proc(s :string): string]): untyped =
|
||||||
# generates a case stmt, which assigns the correct enum field given
|
# Generates a case stmt, which assigns the correct enum field given
|
||||||
# a normalized string comparison to the `argSym` input.
|
# a normalized string comparison to the `argSym` input.
|
||||||
# string normalization is done using passed normalizer.
|
# string normalization is done using passed normalizer.
|
||||||
# NOTE: for an enum with fields Foo, Bar, ... we cannot generate
|
# NOTE: for an enum with fields Foo, Bar, ... we cannot generate
|
||||||
|
|
@ -49,7 +49,12 @@ macro genEnumCaseStmt*(typ: typedesc, argSym: typed, default: typed,
|
||||||
of nnkIntLit:
|
of nnkIntLit:
|
||||||
fStr = f[0].strVal
|
fStr = f[0].strVal
|
||||||
fNum = f[1].intVal
|
fNum = f[1].intVal
|
||||||
else: error("Invalid tuple syntax!", f[1])
|
else:
|
||||||
|
let fAst = f[0].getImpl
|
||||||
|
if fAst.kind == nnkStrLit:
|
||||||
|
fStr = fAst.strVal
|
||||||
|
else:
|
||||||
|
error("Invalid tuple syntax!", f[1])
|
||||||
else: error("Invalid node for enum type `" & $f.kind & "`!", f)
|
else: error("Invalid node for enum type `" & $f.kind & "`!", f)
|
||||||
# add field if string not already added
|
# add field if string not already added
|
||||||
if fNum >= userMin and fNum <= userMax:
|
if fNum >= userMin and fNum <= userMax:
|
||||||
|
|
|
||||||
|
|
@ -583,6 +583,17 @@ template main() =
|
||||||
let g = parseEnum[Foo]("Bar", A)
|
let g = parseEnum[Foo]("Bar", A)
|
||||||
doAssert g == A
|
doAssert g == A
|
||||||
|
|
||||||
|
block: # bug #19463
|
||||||
|
const CAMPAIGN_TABLE = "wikientries_campaign"
|
||||||
|
const CHARACTER_TABLE = "wikientries_character"
|
||||||
|
|
||||||
|
type Tables = enum
|
||||||
|
a = CAMPAIGN_TABLE,
|
||||||
|
b = CHARACTER_TABLE,
|
||||||
|
|
||||||
|
let myA = CAMPAIGN_TABLE
|
||||||
|
doAssert $parseEnum[Tables](myA) == "wikientries_campaign"
|
||||||
|
|
||||||
block: # check enum defined in block
|
block: # check enum defined in block
|
||||||
type
|
type
|
||||||
Bar = enum
|
Bar = enum
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue