mirror of https://github.com/status-im/NimYAML.git
Allow backticked field names in macros
This commit is contained in:
parent
24d68ad11d
commit
71b075abf6
|
@ -1423,6 +1423,14 @@ macro getFieldIndex(t: typedesc, field: untyped): untyped =
|
||||||
result = newNimNode(nnkInt16Lit)
|
result = newNimNode(nnkInt16Lit)
|
||||||
result.intVal = fieldIndex
|
result.intVal = fieldIndex
|
||||||
|
|
||||||
|
proc fieldIdent(field: NimNode): NimNode {.compileTime.} =
|
||||||
|
case field.kind
|
||||||
|
of nnkIdent: result = field
|
||||||
|
of nnkAccQuoted: result = field[0]
|
||||||
|
else:
|
||||||
|
raise newException(Exception, "invalid node type (expected ident):" &
|
||||||
|
$field.kind)
|
||||||
|
|
||||||
macro markAsTransient*(t: typedesc, field: untyped): typed =
|
macro markAsTransient*(t: typedesc, field: untyped): typed =
|
||||||
## Mark an object field as *transient*, meaning that this object field will
|
## Mark an object field as *transient*, meaning that this object field will
|
||||||
## not be serialized when an object instance is dumped as YAML, and also that
|
## not be serialized when an object instance is dumped as YAML, and also that
|
||||||
|
@ -1439,7 +1447,9 @@ macro markAsTransient*(t: typedesc, field: untyped): typed =
|
||||||
## markAsTransient(MyObject, c)
|
## markAsTransient(MyObject, c)
|
||||||
##
|
##
|
||||||
## This does not work if the object has been marked as implicit.
|
## This does not work if the object has been marked as implicit.
|
||||||
let nextBitvectorIndex = transientVectors.len
|
let
|
||||||
|
nextBitvectorIndex = transientVectors.len
|
||||||
|
fieldName = fieldIdent(field)
|
||||||
result = quote do:
|
result = quote do:
|
||||||
when compiles(`implicitVariantObjectMarker`(`t`)):
|
when compiles(`implicitVariantObjectMarker`(`t`)):
|
||||||
{.fatal: "Cannot mark fields of implicit variant objects as transient." .}
|
{.fatal: "Cannot mark fields of implicit variant objects as transient." .}
|
||||||
|
@ -1450,7 +1460,7 @@ macro markAsTransient*(t: typedesc, field: untyped): typed =
|
||||||
static: transientVectors.add({})
|
static: transientVectors.add({})
|
||||||
static:
|
static:
|
||||||
transientVectors[`transientBitvectorProc`(`t`)].incl(
|
transientVectors[`transientBitvectorProc`(`t`)].incl(
|
||||||
getFieldIndex(`t`, `field`))
|
getFieldIndex(`t`, `fieldName`))
|
||||||
|
|
||||||
macro setDefaultValue*(t: typedesc, field: untyped, value: typed): typed =
|
macro setDefaultValue*(t: typedesc, field: untyped, value: typed): typed =
|
||||||
## Set the default value of an object field. Fields with default values may
|
## Set the default value of an object field. Fields with default values may
|
||||||
|
@ -1469,6 +1479,7 @@ macro setDefaultValue*(t: typedesc, field: untyped, value: typed): typed =
|
||||||
let
|
let
|
||||||
dSym = genSym(nskVar, ":default")
|
dSym = genSym(nskVar, ":default")
|
||||||
nextBitvectorIndex = defaultVectors.len
|
nextBitvectorIndex = defaultVectors.len
|
||||||
|
fieldName = fieldIdent(field)
|
||||||
result = quote do:
|
result = quote do:
|
||||||
when compiles(`transientBitvectorProc`(`t`)):
|
when compiles(`transientBitvectorProc`(`t`)):
|
||||||
{.fatal: "Cannot set default value of transient field".}
|
{.fatal: "Cannot set default value of transient field".}
|
||||||
|
@ -1482,8 +1493,8 @@ macro setDefaultValue*(t: typedesc, field: untyped, value: typed): typed =
|
||||||
{.compileTime.} = `nextBitvectorIndex`
|
{.compileTime.} = `nextBitvectorIndex`
|
||||||
static: defaultVectors.add({})
|
static: defaultVectors.add({})
|
||||||
static:
|
static:
|
||||||
`defaultValueGetter`(`t`).`field` = `value`
|
`defaultValueGetter`(`t`).`fieldName` = `value`
|
||||||
defaultVectors[`defaultBitvectorProc`(`t`)].incl(getFieldIndex(`t`, `field`))
|
defaultVectors[`defaultBitvectorProc`(`t`)].incl(getFieldIndex(`t`, `fieldName`))
|
||||||
|
|
||||||
macro ignoreInputKey*(t: typedesc, name: string{lit}): typed =
|
macro ignoreInputKey*(t: typedesc, name: string{lit}): typed =
|
||||||
## Tell NimYAML that when loading an object of type ``t``, any mapping key
|
## Tell NimYAML that when loading an object of type ``t``, any mapping key
|
||||||
|
|
Loading…
Reference in New Issue