Automatic toObjectType for ref or ptr object in useDefault***In (#77)

This commit is contained in:
andri lim 2024-10-23 12:05:43 +07:00 committed by GitHub
parent 08055d72f3
commit 51367e0c79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 22 additions and 5 deletions

View File

@ -41,28 +41,46 @@ template createFlavor*(ModifiedFormat, FlavorName: untyped) =
template PreferredOutputType*(T: type FlavorName): type = PreferredOutputType(ModifiedFormat)
template mimeType*(T: type FlavorName): string = mimeType(ModifiedFormat)
template toObjectType(T: type): untyped =
typeof(T()[])
template toObjectTypeIfNecessary(T: type): untyped =
when T is ref|ptr:
toObjectType(T)
else:
T
# useDefault***In or useDefault***For only works for
# object|ref object|ptr object
template useDefaultSerializationIn*(T: untyped, Flavor: type) =
mixin Reader, Writer
template readValue*(r: var Reader(Flavor), value: var T) =
type TT = toObjectTypeIfNecessary(T)
template readValue*(r: var Reader(Flavor), value: var TT) =
mixin readRecordValue
readRecordValue(r, value)
template writeValue*(w: var Writer(Flavor), value: T) =
template writeValue*(w: var Writer(Flavor), value: TT) =
mixin writeRecordValue
writeRecordValue(w, value)
template useDefaultWriterIn*(T: untyped, Flavor: type) =
mixin Writer
template writeValue*(w: var Writer(Flavor), value: T) =
type TT = toObjectTypeIfNecessary(T)
template writeValue*(w: var Writer(Flavor), value: TT) =
mixin writeRecordValue
writeRecordValue(w, value)
template useDefaultReaderIn*(T: untyped, Flavor: type) =
mixin Reader
template readValue*(r: var Reader(Flavor), value: var T) =
type TT = toObjectTypeIfNecessary(T)
template readValue*(r: var Reader(Flavor), value: var TT) =
mixin readRecordValue
readRecordValue(r, value)
@ -83,4 +101,3 @@ macro useDefaultReaderFor*(Flavor: type, types: varargs[untyped])=
for T in types:
result.add newCall(bindSym "useDefaultReaderIn", T, Flavor)