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 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")
@ -133,15 +132,14 @@ 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`)
when FieldType is `FieldTypeSym`: template fieldCaseDiscriminator: auto {.used.} = `discriminator`
`fieldNameDefs` template fieldCaseBranches: auto {.used.} = `branches`
template fieldCaseDiscriminator: auto {.used.} = `discriminator` `body`
template fieldCaseBranches: auto {.used.} = `branches`
`body`
i += 1 i += 1

View File

@ -4,41 +4,19 @@ 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":
test "custom fields order": setup:
check collectFields(Simple) == @["Meter distance", "int x", "string y"]
test "tuples handling":
var fieldsList = newSeq[string]() 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): 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"]