Support when statements in records that reuse the same field name in different branches
This commit is contained in:
parent
217d78a1dc
commit
4e2ffe3f6d
|
@ -101,6 +101,7 @@ macro enumAllSerializedFieldsImpl(T: type, body: untyped): untyped =
|
||||||
|
|
||||||
let
|
let
|
||||||
fieldType = field.typ
|
fieldType = field.typ
|
||||||
|
FieldTypeSym = getTypeInst(fieldType)
|
||||||
fieldIdent = field.name
|
fieldIdent = field.name
|
||||||
realFieldName = newLit($fieldIdent.skipPragma)
|
realFieldName = newLit($fieldIdent.skipPragma)
|
||||||
serializedFieldName = field.readPragma("serializedFieldName")
|
serializedFieldName = field.readPragma("serializedFieldName")
|
||||||
|
@ -132,14 +133,15 @@ macro enumAllSerializedFieldsImpl(T: type, body: untyped): untyped =
|
||||||
result.add quote do:
|
result.add quote do:
|
||||||
block:
|
block:
|
||||||
when compiles(type(`field`)):
|
when compiles(type(`field`)):
|
||||||
`fieldNameDefs`
|
|
||||||
|
|
||||||
type FieldType {.inject, used.} = type(`field`)
|
type FieldType {.inject, used.} = type(`field`)
|
||||||
|
|
||||||
template fieldCaseDiscriminator: auto {.used.} = `discriminator`
|
when FieldType is `FieldTypeSym`:
|
||||||
template fieldCaseBranches: auto {.used.} = `branches`
|
`fieldNameDefs`
|
||||||
|
|
||||||
`body`
|
template fieldCaseDiscriminator: auto {.used.} = `discriminator`
|
||||||
|
template fieldCaseBranches: auto {.used.} = `branches`
|
||||||
|
|
||||||
|
`body`
|
||||||
|
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
|
|
|
@ -4,19 +4,41 @@ import
|
||||||
../serialization/object_serialization,
|
../serialization/object_serialization,
|
||||||
../serialization/testing/generic_suite
|
../serialization/testing/generic_suite
|
||||||
|
|
||||||
|
type
|
||||||
|
Untrusted = object
|
||||||
|
Trusted = object
|
||||||
|
|
||||||
|
Signature = object
|
||||||
|
p: int
|
||||||
|
k: float
|
||||||
|
|
||||||
|
TrustedSignature = object
|
||||||
|
data: string
|
||||||
|
|
||||||
|
SignatureHolder[TrustLevel] = object
|
||||||
|
when TrustLevel is Trusted:
|
||||||
|
sig: TrustedSignature
|
||||||
|
origin: string
|
||||||
|
else:
|
||||||
|
sig: Signature
|
||||||
|
|
||||||
|
func collectFields(T: type): seq[string] =
|
||||||
|
enumAllSerializedFields(T):
|
||||||
|
result.add(name(FieldType) & " " & fieldName & fieldCaseDiscriminator)
|
||||||
|
|
||||||
suite "object serialization":
|
suite "object serialization":
|
||||||
setup:
|
|
||||||
var fieldsList = newSeq[string]()
|
|
||||||
|
|
||||||
test "custom fields order":
|
test "custom fields order":
|
||||||
enumAllSerializedFields(Simple):
|
check collectFields(Simple) == @["Meter distance", "int x", "string y"]
|
||||||
fieldsList.add(name(FieldType) & " " & fieldName & fieldCaseDiscriminator)
|
|
||||||
|
|
||||||
check fieldsList == @["Meter distance", "int x", "string y"]
|
|
||||||
|
|
||||||
test "tuples handling":
|
test "tuples handling":
|
||||||
|
var fieldsList = newSeq[string]()
|
||||||
|
|
||||||
enumAllSerializedFields(HoldsTuples):
|
enumAllSerializedFields(HoldsTuples):
|
||||||
fieldsList.add(fieldName & ": " & $isTuple(FieldType))
|
fieldsList.add(fieldName & ": " & $isTuple(FieldType))
|
||||||
|
|
||||||
check fieldsList == @["t1: true", "t2: true", "t3: true"]
|
check fieldsList == @["t1: true", "t2: true", "t3: true"]
|
||||||
|
|
||||||
|
test "when statements":
|
||||||
|
check collectFields(SignatureHolder[Trusted]) == @["TrustedSignature sig", "string origin"]
|
||||||
|
check collectFields(SignatureHolder[Untrusted]) == @["Signature sig"]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue