Revert "Support when statements in records that reuse the same field name in different branches"

This reverts commit 4e2ffe3f6d.
This commit is contained in:
Zahary Karadjov 2021-02-18 23:07:52 +02:00
parent 293a987c51
commit 32fce5df8d
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
2 changed files with 13 additions and 37 deletions

View File

@ -101,7 +101,6 @@ macro enumAllSerializedFieldsImpl(T: type, body: untyped): untyped =
let
fieldType = field.typ
FieldTypeSym = getTypeInst(fieldType)
fieldIdent = field.name
realFieldName = newLit($fieldIdent.skipPragma)
serializedFieldName = field.readPragma("serializedFieldName")
@ -133,15 +132,14 @@ macro enumAllSerializedFieldsImpl(T: type, body: untyped): untyped =
result.add quote do:
block:
when compiles(type(`field`)):
`fieldNameDefs`
type FieldType {.inject, used.} = type(`field`)
when FieldType is `FieldTypeSym`:
`fieldNameDefs`
template fieldCaseDiscriminator: auto {.used.} = `discriminator`
template fieldCaseBranches: auto {.used.} = `branches`
template fieldCaseDiscriminator: auto {.used.} = `discriminator`
template fieldCaseBranches: auto {.used.} = `branches`
`body`
`body`
i += 1

View File

@ -4,41 +4,19 @@ import
../serialization/object_serialization,
../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":
test "custom fields order":
check collectFields(Simple) == @["Meter distance", "int x", "string y"]
test "tuples handling":
setup:
var fieldsList = newSeq[string]()
test "custom fields order":
enumAllSerializedFields(Simple):
fieldsList.add(name(FieldType) & " " & fieldName & fieldCaseDiscriminator)
check fieldsList == @["Meter distance", "int x", "string y"]
test "tuples handling":
enumAllSerializedFields(HoldsTuples):
fieldsList.add(fieldName & ": " & $isTuple(FieldType))
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"]