Some needed support for custom field serialization in Json

This commit is contained in:
Zahary Karadjov 2019-08-07 01:19:32 +03:00
parent 56ad4b18de
commit b42502b512
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
1 changed files with 12 additions and 3 deletions

View File

@ -2,7 +2,7 @@ import
stew/shims/macros, stew/objects
type
FieldTag[RecordType; fieldName: static string; FieldType] = distinct void
FieldTag*[RecordType; fieldName: static string; FieldType] = distinct void
let
# Identifiers affecting the public interface of the library:
@ -138,6 +138,13 @@ template readFieldIMPL[Reader](field: type FieldTag,
mixin readValue
reader.readValue(field.FieldType)
template writeFieldIMPL*[Writer](writer: var Writer,
fieldTag: type FieldTag,
fieldVal: auto,
holderObj: auto) =
mixin writeValue
writer.writeValue(fieldVal)
proc makeFieldReadersTable(RecordType, Reader: distinct type):
seq[FieldReader[RecordType, Reader]] =
mixin enumAllSerializedFields, readFieldIMPL
@ -281,8 +288,10 @@ proc genCustomSerializationForField(Format, field,
if writeBody != nil:
result.add quote do:
type Writer = WriterType(`Format`)
proc writeFieldIMPL*(F: type FieldTag[`RecordType`, `fieldName`, auto],
`writerVar`: var Writer) =
proc writeFieldIMPL*(`writerVar`: var Writer,
F: type FieldTag[`RecordType`, `fieldName`, auto],
`valueVar`: auto,
`holderVar`: `RecordType`) =
`writeBody`
proc genCustomSerializationForType(Format, typ: NimNode,