2018-12-17 23:00:00 +00:00
|
|
|
import
|
2019-07-08 07:55:32 +00:00
|
|
|
stew/shims/macros, stew/objects
|
2018-11-11 11:40:19 +00:00
|
|
|
|
2019-07-30 22:52:17 +00:00
|
|
|
type
|
2019-08-06 22:19:32 +00:00
|
|
|
FieldTag*[RecordType; fieldName: static string; FieldType] = distinct void
|
2019-07-30 22:52:17 +00:00
|
|
|
|
2019-08-02 12:30:30 +00:00
|
|
|
let
|
|
|
|
# Identifiers affecting the public interface of the library:
|
|
|
|
valueVar {.compileTime.} = ident "value"
|
|
|
|
readerVar {.compileTime.} = ident "reader"
|
|
|
|
writerVar {.compileTime.} = ident "writer"
|
|
|
|
holderVar {.compileTime.} = ident "holder"
|
|
|
|
fieldNameVar {.compileTime.} = ident "fieldName"
|
|
|
|
FieldTypeSym {.compileTime.} = ident "FieldType"
|
|
|
|
|
2018-11-11 11:40:19 +00:00
|
|
|
template dontSerialize* {.pragma.}
|
|
|
|
## Specifies that a certain field should be ignored for
|
|
|
|
## the purposes of serialization
|
|
|
|
|
2019-07-08 07:55:32 +00:00
|
|
|
template enumInstanceSerializedFields*(obj: auto,
|
|
|
|
fieldNameVar, fieldVar,
|
|
|
|
body: untyped) =
|
|
|
|
## Expands a block over all serialized fields of an object.
|
|
|
|
##
|
|
|
|
## Inside the block body, the passed `fieldNameVar` identifier
|
|
|
|
## will refer to the name of each field as a string. `fieldVar`
|
|
|
|
## will refer to the field value.
|
|
|
|
##
|
|
|
|
## The order of visited fields matches the order of the fields in
|
|
|
|
## the object definition unless `serialziedFields` is used to specify
|
|
|
|
## a different order. Fields marked with the `dontSerialize` pragma
|
|
|
|
## are skipped.
|
|
|
|
##
|
|
|
|
## If the visited object is a case object, only the currently active
|
|
|
|
## fields will be visited. During de-serialization, case discriminators
|
|
|
|
## will be read first and the iteration will continue depending on the
|
|
|
|
## value being deserialized.
|
|
|
|
##
|
2019-07-10 17:18:12 +00:00
|
|
|
type ObjType = type(obj)
|
|
|
|
|
2019-07-08 07:55:32 +00:00
|
|
|
for fieldNameVar, fieldVar in fieldPairs(obj):
|
2019-07-10 17:18:12 +00:00
|
|
|
when not hasCustomPragmaFixed(ObjType, fieldNameVar, dontSerialize):
|
2019-07-08 07:55:32 +00:00
|
|
|
body
|
|
|
|
|
2019-08-02 12:30:30 +00:00
|
|
|
macro enumAllSerializedFieldsImpl(T: type, body: untyped): untyped =
|
2019-07-08 07:55:32 +00:00
|
|
|
## Expands a block over all fields of a type
|
|
|
|
##
|
|
|
|
## Please note that the main difference between
|
|
|
|
## `enumInstanceSerializedFields` and `enumAllSerializedFields`
|
|
|
|
## is that the later will visit all fields of case objects.
|
|
|
|
##
|
2019-08-02 12:30:30 +00:00
|
|
|
## Inside the block body, the following symbols will be defined:
|
|
|
|
##
|
|
|
|
## * `fieldName`
|
|
|
|
## String literal for the field name
|
|
|
|
##
|
|
|
|
## * `FieldType`
|
|
|
|
## Type alias for the field type
|
|
|
|
##
|
|
|
|
## * `fieldCaseDisciminator`
|
|
|
|
## String literal denoting the name of the case object
|
|
|
|
## discrimator under which the visited field is nested.
|
|
|
|
## If the field is not nested in a specific case branch,
|
|
|
|
## this will be an empty string.
|
|
|
|
##
|
|
|
|
## * `fieldCaseBranches`
|
|
|
|
## A set literal node denoting the possible values of the
|
|
|
|
## case object discrimator which make this field accessible.
|
|
|
|
##
|
2019-07-08 07:55:32 +00:00
|
|
|
## The order of visited fields matches the order of the fields in
|
|
|
|
## the object definition unless `serialziedFields` is used to specify
|
|
|
|
## a different order. Fields marked with the `dontSerialize` pragma
|
|
|
|
## are skipped.
|
|
|
|
##
|
2019-07-30 22:52:17 +00:00
|
|
|
var typeAst = getType(T)[1]
|
2019-08-02 12:49:27 +00:00
|
|
|
var typeImpl: NimNode
|
|
|
|
let isSymbol = not typeAst.isTuple
|
|
|
|
|
|
|
|
|
|
|
|
if not isSymbol:
|
|
|
|
typeImpl = typeAst
|
|
|
|
else:
|
|
|
|
typeImpl = getImpl(typeAst)
|
2019-07-08 07:55:32 +00:00
|
|
|
result = newStmtList()
|
|
|
|
|
2019-08-02 12:49:27 +00:00
|
|
|
var i = 0
|
2019-07-30 22:52:17 +00:00
|
|
|
for field in recordFields(typeImpl):
|
2019-07-08 07:55:32 +00:00
|
|
|
if field.readPragma("dontSerialize") != nil:
|
|
|
|
continue
|
|
|
|
|
|
|
|
let
|
|
|
|
fieldType = field.typ
|
2019-08-02 12:30:30 +00:00
|
|
|
fieldIdent = field.name
|
|
|
|
fieldName = newLit($fieldIdent)
|
|
|
|
discrimator = newLit(if field.caseField == nil: ""
|
|
|
|
else: $field.caseField[0])
|
|
|
|
branches = field.caseBranch
|
2019-08-02 12:49:27 +00:00
|
|
|
fieldIndex = newLit(i)
|
|
|
|
|
|
|
|
let fieldNameVarTemplate =
|
|
|
|
if isSymbol:
|
|
|
|
quote:
|
|
|
|
template `fieldNameVar`: auto = `fieldName`
|
|
|
|
else:
|
|
|
|
quote:
|
|
|
|
template `fieldNameVar`: auto = $`fieldIndex`
|
|
|
|
# we can't access .Fieldn, so our helper knows
|
|
|
|
# to parseInt this
|
|
|
|
|
|
|
|
let field =
|
|
|
|
if isSymbol:
|
|
|
|
quote do: default(`T`).`fieldIdent`
|
|
|
|
else:
|
|
|
|
quote do: default(`T`)[`fieldIndex`]
|
2019-07-08 07:55:32 +00:00
|
|
|
|
|
|
|
result.add quote do:
|
|
|
|
block:
|
2019-08-02 12:49:27 +00:00
|
|
|
`fieldNameVarTemplate`
|
2019-08-02 12:30:30 +00:00
|
|
|
template fieldCaseDisciminator: auto = `discrimator`
|
|
|
|
template fieldCaseBranches: auto = `branches`
|
2019-08-02 12:49:27 +00:00
|
|
|
|
2019-07-30 22:52:17 +00:00
|
|
|
# type `fieldTypeVar` = `fieldType`
|
2019-07-10 17:18:12 +00:00
|
|
|
# TODO: This is a work-around for a classic Nim issue:
|
2019-08-02 12:49:27 +00:00
|
|
|
type `FieldTypeSym` = type(`field`)
|
2019-07-08 07:55:32 +00:00
|
|
|
`body`
|
2019-08-02 12:49:27 +00:00
|
|
|
|
|
|
|
i += 1
|
2019-07-08 07:55:32 +00:00
|
|
|
|
2019-08-02 12:30:30 +00:00
|
|
|
template enumAllSerializedFields*(T: type, body): untyped =
|
|
|
|
when T is ref|ptr:
|
|
|
|
type TT = type(default(T)[])
|
|
|
|
enumAllSerializedFieldsImpl(TT, body)
|
|
|
|
else:
|
|
|
|
enumAllSerializedFieldsImpl(T, body)
|
|
|
|
|
|
|
|
func isCaseObject*(T: type): bool {.compileTime.} =
|
|
|
|
genExpr:
|
|
|
|
enumAllSerializedFields(T):
|
|
|
|
if fieldCaseDisciminator != "":
|
|
|
|
return newLit(true)
|
|
|
|
|
|
|
|
newLit(false)
|
|
|
|
|
2018-12-17 23:00:00 +00:00
|
|
|
type
|
|
|
|
FieldMarkerImpl*[name: static string] = object
|
|
|
|
|
|
|
|
FieldReader*[RecordType, Reader] = tuple[
|
|
|
|
fieldName: string,
|
2019-07-30 22:52:17 +00:00
|
|
|
reader: proc (rec: var RecordType, reader: var Reader) {.gcsafe, nimcall.}
|
2018-12-17 23:00:00 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
FieldReadersTable*[RecordType, Reader] = openarray[FieldReader[RecordType, Reader]]
|
2018-11-11 11:40:19 +00:00
|
|
|
|
|
|
|
proc totalSerializedFieldsImpl(T: type): int =
|
2019-07-08 07:55:32 +00:00
|
|
|
mixin enumAllSerializedFields
|
2019-08-02 12:30:30 +00:00
|
|
|
enumAllSerializedFields(T): inc result
|
2018-11-11 11:40:19 +00:00
|
|
|
|
|
|
|
template totalSerializedFields*(T: type): int =
|
|
|
|
(static(totalSerializedFieldsImpl(T)))
|
|
|
|
|
2018-12-17 23:00:00 +00:00
|
|
|
macro customSerialization*(field: untyped, definition): untyped =
|
|
|
|
discard
|
|
|
|
|
2019-07-30 22:52:17 +00:00
|
|
|
template readFieldIMPL[Reader](field: type FieldTag,
|
|
|
|
reader: var Reader): untyped =
|
|
|
|
mixin readValue
|
|
|
|
reader.readValue(field.FieldType)
|
|
|
|
|
2019-08-06 22:19:32 +00:00
|
|
|
template writeFieldIMPL*[Writer](writer: var Writer,
|
|
|
|
fieldTag: type FieldTag,
|
|
|
|
fieldVal: auto,
|
|
|
|
holderObj: auto) =
|
|
|
|
mixin writeValue
|
|
|
|
writer.writeValue(fieldVal)
|
|
|
|
|
2019-07-08 07:55:32 +00:00
|
|
|
proc makeFieldReadersTable(RecordType, Reader: distinct type):
|
|
|
|
seq[FieldReader[RecordType, Reader]] =
|
2019-07-30 22:52:17 +00:00
|
|
|
mixin enumAllSerializedFields, readFieldIMPL
|
2018-12-17 23:00:00 +00:00
|
|
|
|
2019-08-02 12:30:30 +00:00
|
|
|
enumAllSerializedFields(RecordType):
|
2019-08-01 16:29:08 +00:00
|
|
|
proc readField(obj: var RecordType, reader: var Reader) {.gcsafe, nimcall.} =
|
2019-08-02 12:49:27 +00:00
|
|
|
when RecordType is tuple:
|
|
|
|
const i = fieldName.parseInt
|
2019-07-08 07:55:32 +00:00
|
|
|
try:
|
2019-08-02 12:49:27 +00:00
|
|
|
|
2019-07-30 22:52:17 +00:00
|
|
|
type F = FieldTag[RecordType, fieldName, type(FieldType)]
|
2019-08-02 12:49:27 +00:00
|
|
|
when RecordType is not tuple:
|
|
|
|
obj.field(fieldName) = readFieldIMPL(F, reader)
|
|
|
|
else:
|
|
|
|
obj[i] = readFieldIMPL(F, reader)
|
2019-07-08 07:55:32 +00:00
|
|
|
except SerializationError:
|
|
|
|
raise
|
|
|
|
except CatchableError as err:
|
2019-08-02 12:49:27 +00:00
|
|
|
when RecordType is not tuple:
|
|
|
|
let field = obj.field(fieldName)
|
|
|
|
else:
|
|
|
|
let field = obj[i]
|
2019-07-08 07:55:32 +00:00
|
|
|
reader.handleReadException(`RecordType`, fieldName,
|
2019-08-02 12:49:27 +00:00
|
|
|
field, err)
|
2019-07-08 07:55:32 +00:00
|
|
|
result.add((fieldName, readField))
|
2018-12-17 23:00:00 +00:00
|
|
|
|
|
|
|
proc fieldReadersTable*(RecordType, Reader: distinct type):
|
2019-07-30 22:52:17 +00:00
|
|
|
ptr seq[FieldReader[RecordType, Reader]] =
|
2018-12-17 23:00:00 +00:00
|
|
|
mixin readValue
|
2019-07-08 07:55:32 +00:00
|
|
|
var tbl {.global.} = makeFieldReadersTable(RecordType, Reader)
|
2018-12-17 23:00:00 +00:00
|
|
|
{.gcsafe.}:
|
|
|
|
return addr(tbl)
|
|
|
|
|
|
|
|
proc findFieldReader*(fieldsTable: FieldReadersTable,
|
|
|
|
fieldName: string,
|
|
|
|
expectedFieldPos: var int): auto =
|
|
|
|
for i in expectedFieldPos ..< fieldsTable.len:
|
|
|
|
if fieldsTable[i].fieldName == fieldName:
|
|
|
|
expectedFieldPos = i + 1
|
|
|
|
return fieldsTable[i].reader
|
|
|
|
|
|
|
|
for i in 0 ..< expectedFieldPos:
|
|
|
|
if fieldsTable[i].fieldName == fieldName:
|
|
|
|
return fieldsTable[i].reader
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
2019-07-08 07:55:32 +00:00
|
|
|
macro setSerializedFields*(T: typedesc, fields: varargs[untyped]): untyped =
|
|
|
|
var fieldsArray = newTree(nnkBracket)
|
|
|
|
for f in fields: fieldsArray.add newCall(bindSym"ident", newLit($f))
|
|
|
|
|
|
|
|
template payload(T: untyped, fieldsArray) {.dirty.} =
|
2019-08-02 12:30:30 +00:00
|
|
|
bind default, quote, add, getType, newStmtList,
|
|
|
|
ident, newLit, newDotExpr, `$`, `[]`, getAst
|
2019-07-08 07:55:32 +00:00
|
|
|
|
|
|
|
macro enumInstanceSerializedFields*(ins: T,
|
|
|
|
fieldNameVar, fieldVar,
|
|
|
|
body: untyped): untyped =
|
|
|
|
var
|
|
|
|
fields = fieldsArray
|
|
|
|
res = newStmtList()
|
|
|
|
|
|
|
|
for field in fields:
|
|
|
|
let
|
|
|
|
fieldName = newLit($field)
|
|
|
|
fieldAccessor = newDotExpr(ins, field)
|
|
|
|
|
2019-08-02 12:30:30 +00:00
|
|
|
# TODO replace with getAst once it's ready
|
2019-08-01 16:29:08 +00:00
|
|
|
template fieldPayload(fieldNameVar, fieldName, fieldVar,
|
|
|
|
fieldAccessor, body) =
|
2019-07-08 07:55:32 +00:00
|
|
|
block:
|
2019-08-01 16:29:08 +00:00
|
|
|
const fieldNameVar = fieldName
|
|
|
|
template fieldVar: auto = fieldAccessor
|
|
|
|
body
|
|
|
|
|
|
|
|
res.add getAst(fieldPayload(fieldNameVar, fieldName, fieldVar,
|
|
|
|
fieldAccessor, body))
|
2019-07-08 07:55:32 +00:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
2019-08-02 12:30:30 +00:00
|
|
|
macro enumAllSerializedFields*(typ: type T, body: untyped): untyped =
|
2019-07-08 07:55:32 +00:00
|
|
|
var
|
|
|
|
fields = fieldsArray
|
|
|
|
res = newStmtList()
|
|
|
|
typ = getType(typ)
|
|
|
|
|
|
|
|
for field in fields:
|
|
|
|
let
|
|
|
|
fieldName = newLit($field)
|
2019-08-02 12:30:30 +00:00
|
|
|
fieldNameVar = ident "fieldName"
|
|
|
|
FieldTypeSym = ident "FieldType"
|
2019-07-08 07:55:32 +00:00
|
|
|
|
2019-08-02 12:30:30 +00:00
|
|
|
# TODO replace with getAst once it's ready
|
2019-08-01 16:29:08 +00:00
|
|
|
template fieldPayload(fieldNameVar, fieldName,
|
|
|
|
fieldTypeVar, typ, field,
|
|
|
|
body) =
|
2019-07-08 07:55:32 +00:00
|
|
|
block:
|
2019-08-01 16:29:08 +00:00
|
|
|
const fieldNameVar = fieldName
|
|
|
|
type fieldTypeVar = type(default(typ).field)
|
2019-08-02 12:30:30 +00:00
|
|
|
|
|
|
|
template fieldCaseDisciminator: auto = ""
|
|
|
|
template fieldCaseBranches: auto = nil
|
|
|
|
|
2019-08-01 16:29:08 +00:00
|
|
|
body
|
|
|
|
|
|
|
|
res.add getAst(fieldPayload(fieldNameVar, fieldName,
|
2019-08-02 12:30:30 +00:00
|
|
|
FieldTypeSym, typ, field,
|
2019-08-01 16:29:08 +00:00
|
|
|
body))
|
2019-07-08 07:55:32 +00:00
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
return getAst(payload(T, fieldsArray))
|
|
|
|
|
2019-07-30 22:52:17 +00:00
|
|
|
proc getReaderAndWriter(customSerializationBody: NimNode): (NimNode, NimNode) =
|
|
|
|
template fail(n) =
|
|
|
|
error "useCustomSerialization expects a block with only `read` and `write` definitions", n
|
|
|
|
|
|
|
|
for n in customSerializationBody:
|
|
|
|
if n.kind in nnkCallKinds:
|
|
|
|
if eqIdent(n[0], "read"):
|
|
|
|
result[0] = n[1]
|
|
|
|
elif eqIdent(n[0], "write"):
|
|
|
|
result[1] = n[1]
|
|
|
|
else:
|
|
|
|
fail n[0]
|
|
|
|
elif n.kind == nnkCommentStmt:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
fail n
|
|
|
|
|
|
|
|
proc genCustomSerializationForField(Format, field,
|
|
|
|
readBody, writeBody: NimNode): NimNode =
|
|
|
|
var
|
|
|
|
RecordType = field[0]
|
|
|
|
fieldIdent = field[1]
|
|
|
|
fieldName = newLit $fieldIdent
|
|
|
|
FieldType = genSym(nskType, "FieldType")
|
|
|
|
|
|
|
|
result = newStmtList()
|
|
|
|
result.add quote do:
|
|
|
|
type `FieldType` = type default(`RecordType`).`fieldIdent`
|
|
|
|
|
|
|
|
if readBody != nil:
|
|
|
|
result.add quote do:
|
|
|
|
type Reader = ReaderType(`Format`)
|
|
|
|
proc readFieldIMPL*(F: type FieldTag[`RecordType`, `fieldName`, auto],
|
|
|
|
`readerVar`: var Reader): `FieldType` =
|
|
|
|
`readBody`
|
|
|
|
|
|
|
|
if writeBody != nil:
|
|
|
|
result.add quote do:
|
|
|
|
type Writer = WriterType(`Format`)
|
2019-08-06 22:19:32 +00:00
|
|
|
proc writeFieldIMPL*(`writerVar`: var Writer,
|
|
|
|
F: type FieldTag[`RecordType`, `fieldName`, auto],
|
|
|
|
`valueVar`: auto,
|
|
|
|
`holderVar`: `RecordType`) =
|
2019-07-30 22:52:17 +00:00
|
|
|
`writeBody`
|
|
|
|
|
|
|
|
proc genCustomSerializationForType(Format, typ: NimNode,
|
|
|
|
readBody, writeBody: NimNode): NimNode =
|
|
|
|
result = newStmtList()
|
|
|
|
|
|
|
|
if readBody != nil:
|
|
|
|
result.add quote do:
|
|
|
|
type Reader = ReaderType(`Format`)
|
|
|
|
proc readValue*(`readerVar`: var Reader, T: type `typ`): `typ` =
|
|
|
|
`readBody`
|
|
|
|
|
|
|
|
if writeBody != nil:
|
|
|
|
result.add quote do:
|
|
|
|
type Writer = WriterType(`Format`)
|
|
|
|
proc writeValue*(`writerVar`: var Writer, `valueVar`: `typ`) =
|
|
|
|
`writeBody`
|
|
|
|
|
|
|
|
macro useCustomSerialization*(Format: typed, field: untyped, body: untyped): untyped =
|
|
|
|
let (readBody, writeBody) = getReaderAndWriter(body)
|
|
|
|
if field.kind == nnkDotExpr:
|
|
|
|
result = genCustomSerializationForField(Format, field, readBody, writeBody)
|
|
|
|
elif field.kind in {nnkIdent, nnkAccQuoted}:
|
|
|
|
result = genCustomSerializationForType(Format, field, readBody, writeBody)
|
|
|
|
else:
|
|
|
|
error "useCustomSerialization expects a type name or a field of a type (e.g. MyType.myField)"
|
|
|
|
|
|
|
|
when defined(debugUseCustomSerialization):
|
|
|
|
echo result.repr
|
|
|
|
|