Add a {.record: "label".} pragma
A pragma for serializing tuples and objects to or from records.
This commit is contained in:
parent
5aa5ccbd09
commit
376c0e04c5
|
@ -1,7 +1,8 @@
|
||||||
# SPDX-License-Identifier: ISC
|
# SPDX-License-Identifier: ISC
|
||||||
|
|
||||||
import base64, endians, json, hashes, sets, streams, tables
|
|
||||||
import bigints
|
import bigints
|
||||||
|
import std/[base64, endians, json, hashes, macros, sets, streams, tables]
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
PreserveKind* = enum
|
PreserveKind* = enum
|
||||||
|
@ -228,14 +229,6 @@ func isRecord*(prs: Preserve): bool =
|
||||||
result = true
|
result = true
|
||||||
assert(prs.record.len > 0)
|
assert(prs.record.len > 0)
|
||||||
|
|
||||||
proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
|
|
||||||
proc distinctBase(T: typedesc): typedesc {.magic: "TypeTrait".}
|
|
||||||
template distinctBase[T](a: T): untyped = distinctBase(type(a))(a)
|
|
||||||
|
|
||||||
proc symbol*(s: string): Preserve {.inline.} =
|
|
||||||
## Symbol constructor.
|
|
||||||
Preserve(kind: pkSymbol, symbol: s)
|
|
||||||
|
|
||||||
proc label*(prs: Preserve): Preserve {.inline.} =
|
proc label*(prs: Preserve): Preserve {.inline.} =
|
||||||
## Return the label of a record value.
|
## Return the label of a record value.
|
||||||
prs.record[prs.record.high]
|
prs.record[prs.record.high]
|
||||||
|
@ -252,6 +245,13 @@ iterator fields*(prs: Preserve): Preserve =
|
||||||
## Iterate the fields of a record value.
|
## Iterate the fields of a record value.
|
||||||
for i in 0..<prs.record.high: yield prs.record[i]
|
for i in 0..<prs.record.high: yield prs.record[i]
|
||||||
|
|
||||||
|
proc distinctBase(T: typedesc): typedesc {.magic: "TypeTrait".}
|
||||||
|
template distinctBase[T](a: T): untyped = distinctBase(type(a))(a)
|
||||||
|
|
||||||
|
proc symbol*(s: string): Preserve {.inline.} =
|
||||||
|
## Symbol constructor.
|
||||||
|
Preserve(kind: pkSymbol, symbol: s)
|
||||||
|
|
||||||
proc writeVarint(s: Stream; n: int) =
|
proc writeVarint(s: Stream; n: int) =
|
||||||
var n = n
|
var n = n
|
||||||
while true:
|
while true:
|
||||||
|
@ -453,6 +453,16 @@ proc parsePreserve*(s: Stream): Preserve =
|
||||||
else:
|
else:
|
||||||
assertStream(false)
|
assertStream(false)
|
||||||
|
|
||||||
|
template record*(label: string) {.pragma.}
|
||||||
|
## Serialize this object or tuple as a record.
|
||||||
|
## ```
|
||||||
|
## type Foo {.record: "foobar".} = tuple
|
||||||
|
## a, b: int
|
||||||
|
## let r: Foo = (1, 2)
|
||||||
|
## echo $(toPreserve(r))
|
||||||
|
## # <foobar 1 2>
|
||||||
|
## ```
|
||||||
|
|
||||||
proc toPreserve*[T](x: T): Preserve =
|
proc toPreserve*[T](x: T): Preserve =
|
||||||
## Serializes `x` to Preserves; uses `toPreserveHook(x: A)` if it's in scope to
|
## Serializes `x` to Preserves; uses `toPreserveHook(x: A)` if it's in scope to
|
||||||
## customize serialization.
|
## customize serialization.
|
||||||
|
@ -473,8 +483,13 @@ proc toPreserve*[T](x: T): Preserve =
|
||||||
elif T is float64:
|
elif T is float64:
|
||||||
result = Preserve(kind: pkDouble, double: x)
|
result = Preserve(kind: pkDouble, double: x)
|
||||||
elif T is object | tuple:
|
elif T is object | tuple:
|
||||||
result = Preserve(kind: pkDictionary)
|
when T.hasCustomPragma(record):
|
||||||
for k, v in x.fieldPairs: result.dict[symbol(k)] = toPreserve(v)
|
result = Preserve(kind: pkRecord)
|
||||||
|
for _, f in x.fieldPairs: result.record.add(toPreserve(f))
|
||||||
|
result.record.add(symbol(T.getCustomPragmaVal(record)))
|
||||||
|
else:
|
||||||
|
result = Preserve(kind: pkDictionary)
|
||||||
|
for k, v in x.fieldPairs: result.dict[symbol(k)] = toPreserve(v)
|
||||||
elif T is Ordinal:
|
elif T is Ordinal:
|
||||||
result = Preserve(kind: pkSignedInteger, int: x.ord.BiggestInt)
|
result = Preserve(kind: pkSignedInteger, int: x.ord.BiggestInt)
|
||||||
elif T is ptr | ref:
|
elif T is ptr | ref:
|
||||||
|
@ -557,6 +572,14 @@ proc toJsonHook*(prs: Preserve): JsonNode =
|
||||||
of pkEmbedded:
|
of pkEmbedded:
|
||||||
raise newException(ValueError, "cannot convert embedded value to JSON")
|
raise newException(ValueError, "cannot convert embedded value to JSON")
|
||||||
|
|
||||||
|
proc checkRecordLabel(T: typedesc; prs: Preserve) =
|
||||||
|
when T.hasCustomPragma(record):
|
||||||
|
const label = symbol(T.getCustomPragmaVal(record))
|
||||||
|
if prs.label != label:
|
||||||
|
raise newException(ValueError, $prs & " is not a record of class " & label.string)
|
||||||
|
else:
|
||||||
|
raise newException(Defect, $T & " lacks a {.record: \"…\".} annotation")
|
||||||
|
|
||||||
proc fromPreserve*[T](result: var T; prs: Preserve) =
|
proc fromPreserve*[T](result: var T; prs: Preserve) =
|
||||||
# Inplace version of `preserveTo`.
|
# Inplace version of `preserveTo`.
|
||||||
when compiles(fromPreserveHook(result, prs)):
|
when compiles(fromPreserveHook(result, prs)):
|
||||||
|
@ -577,21 +600,26 @@ proc fromPreserve*[T](result: var T; prs: Preserve) =
|
||||||
fromPreserve(result[i], val)
|
fromPreserve(result[i], val)
|
||||||
elif T is float64:
|
elif T is float64:
|
||||||
result = prs.double
|
result = prs.double
|
||||||
elif T is object:
|
elif T is object | tuple:
|
||||||
result = Preserve(kind: pkDictionary)
|
case prs.kind
|
||||||
for k, v in x.fieldPairs: result.dict[symbol(k)] = toPreserve(v)
|
of pkRecord:
|
||||||
elif T is Ordinal:
|
checkRecordLabel(T, prs)
|
||||||
result = Preserve(kind: pkSignedInteger, int: x.ord.BiggestInt)
|
var i: int
|
||||||
|
for k, v in result.fieldPairs:
|
||||||
|
fromPreserve(v, prs.record[i])
|
||||||
|
inc(i)
|
||||||
|
of pkDictionary:
|
||||||
|
for k, v in result.fieldPairs:
|
||||||
|
fromPreserve(v, prs.dict[symbol(k)])
|
||||||
|
else:
|
||||||
|
raise newException(ValueError, "cannot convert to Preserves value to type " & $T)
|
||||||
|
elif T is Ordinal | SomeInteger:
|
||||||
|
result = (T)prs.int
|
||||||
elif T is ptr | ref:
|
elif T is ptr | ref:
|
||||||
if system.`==`(x, nil): result = symbol"null"
|
if system.`==`(x, nil): result = symbol"null"
|
||||||
else: result = toPreserve(x[])
|
else: result = toPreserve(x[])
|
||||||
elif T is string:
|
elif T is string:
|
||||||
result = Preserve(kind: pkString, string: x)
|
result = prs.string
|
||||||
elif T is SomeInteger:
|
|
||||||
result = Preserve(kind: pkSignedInteger, int: x.BiggestInt)
|
|
||||||
elif T is tuple:
|
|
||||||
for i in 0..<prs.record.high:
|
|
||||||
fromPreserve(result[i], prs.record[i])
|
|
||||||
else:
|
else:
|
||||||
raiseAssert("cannot convert from Preserves: " & $T)
|
raiseAssert("cannot convert from Preserves: " & $T)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue