preserves-nim/src/preserves.nim

771 lines
22 KiB
Nim
Raw Normal View History

2021-06-02 13:51:36 +00:00
# SPDX-License-Identifier: ISC
import bigints
2021-07-16 17:11:19 +00:00
import std/[base64, endians, hashes, macros, sets, streams, strutils, tables, typetraits]
2021-07-15 12:24:30 +00:00
import json except `%`, `%*`
2021-06-02 13:51:36 +00:00
type
2021-06-08 10:14:56 +00:00
PreserveKind* = enum
2021-06-24 15:31:30 +00:00
pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkBigInteger, pkString,
pkByteString, pkSymbol, pkRecord, pkSequence, pkSet, pkDictionary, pkEmbedded
2021-06-02 13:51:36 +00:00
Preserve* {.acyclic.} = object
2021-06-08 10:14:56 +00:00
## Type that stores a Preserves value.
2021-06-02 13:51:36 +00:00
case kind*: PreserveKind
of pkBoolean:
bool*: bool
of pkFloat:
float*: float32
of pkDouble:
double*: float64
of pkSignedInteger:
int*: BiggestInt
of pkBigInteger:
bigint*: BigInt
of pkString:
string*: string
of pkByteString:
bytes*: seq[byte]
of pkSymbol:
symbol*: string
of pkRecord:
2021-06-24 15:31:30 +00:00
record*: seq[Preserve] # label is last
2021-06-02 13:51:36 +00:00
of pkSequence:
sequence*: seq[Preserve]
2021-06-02 13:51:36 +00:00
of pkSet:
set*: HashSet[Preserve]
2021-06-02 13:51:36 +00:00
of pkDictionary:
dict*: Table[Preserve, Preserve]
2021-06-02 13:51:36 +00:00
of pkEmbedded:
embedded*: pointer
2021-06-02 13:51:36 +00:00
2021-06-24 15:31:30 +00:00
proc assertValid*(prs: Preserve) =
case prs.kind
of pkBigInteger:
assert(BiggestInt.low.initBigInt < prs.bigint and prs.bigint < BiggestInt.high.initBigInt)
of pkRecord:
assert(prs.record.len > 0, "invalid Preserves record " & prs.repr)
assert(prs.record[prs.record.high].kind < pkRecord)
for v in prs.record: assertValid(v)
of pkSequence:
for v in prs.sequence: assertValid(v)
of pkSet:
for v in prs.set: assertValid(v)
of pkDictionary:
for key, val in prs.dict.pairs:
# assert(key.kind < pkRecord)
2021-06-24 15:31:30 +00:00
assertValid(key)
assertValid(val)
else:
discard
proc isNil*(prs: Preserve): bool =
## Check if ``prs`` is equivalent to the zero-initialized ``Preserve``.
prs.kind == pkBoolean and prs.bool == false
2021-06-02 13:51:36 +00:00
proc `<`(x, y: string | seq[byte]): bool =
for i in 0 .. min(x.high, y.high):
if x[i] < y[i]:
return true
x.len < y.len
proc `<`*(x, y: Preserve): bool =
2021-06-02 13:51:36 +00:00
if x.kind != y.kind:
if x.kind == pkSignedInteger and y.kind == pkBigInteger:
result = x.int.initBigInt < y.bigint
elif x.kind == pkBigInteger and y.kind == pkSignedInteger:
result = x.bigint < y.int.initBigInt
else:
result = x.kind < y.kind
2021-06-02 13:51:36 +00:00
else:
case x.kind
of pkBoolean:
result = (not x.bool) and y.bool
of pkSignedInteger:
result = x.int < y.int
of pkBigInteger:
result = x.bigint < y.bigint
of pkString:
result = x.string < y.string
of pkByteString:
result = x.bytes < y.bytes
of pkSymbol:
result = x.symbol < y.symbol
else:
discard
proc hash*(prs: Preserve): Hash =
var h = hash(prs.kind.int)
case prs.kind
of pkBoolean:
h = h !& hash(prs.bool)
of pkFloat:
h = h !& hash(prs.float)
of pkDouble:
h = h !& hash(prs.double)
of pkSignedInteger:
h = h !& hash(prs.int)
of pkBigInteger:
h = h !& hash(prs.bigint.flags)
h = h !& hash(prs.bigint)
of pkString:
h = h !& hash(prs.string)
of pkByteString:
h = h !& hash(prs.bytes)
of pkSymbol:
h = h !& hash(prs.symbol)
of pkRecord:
for val in prs.record:
h = h !& hash(val)
of pkSequence:
for val in prs.sequence:
h = h !& hash(val)
of pkSet:
for val in prs.set.items:
h = h !& hash(val)
of pkDictionary:
for (key, val) in prs.dict.pairs:
h = h !& hash(val)
of pkEmbedded:
h = h !& hash(prs.embedded)
!$h
proc `==`*(x, y: Preserve): bool =
2021-06-02 13:51:36 +00:00
if x.kind == y.kind:
case x.kind
of pkBoolean:
result = x.bool == y.bool
of pkFloat:
result = x.float == y.float
of pkDouble:
result = x.double == y.double
of pkSignedInteger:
result = x.int == y.int
of pkBigInteger:
result = x.bigint == y.bigint
of pkString:
result = x.string == y.string
of pkByteString:
result = x.bytes == y.bytes
of pkSymbol:
result = x.symbol == y.symbol
of pkRecord:
2021-07-16 17:11:19 +00:00
result = x.record == y.record
2021-06-02 13:51:36 +00:00
of pkSequence:
for i, val in x.sequence:
if y.sequence[i] != val: return false
2021-06-02 13:51:36 +00:00
result = true
of pkSet:
for val in x.set.items:
if not y.set.contains(val): return false
for val in y.set.items:
if not x.set.contains(val): return false
2021-06-02 13:51:36 +00:00
result = true
of pkDictionary:
for (key, val) in x.dict.pairs:
if y.dict[key] != val: return false
result = true
of pkEmbedded:
result = x.embedded == y.embedded
2021-06-02 13:51:36 +00:00
2021-07-16 17:11:19 +00:00
proc concat(result: var string; prs: Preserve) =
case prs.kind:
2021-06-02 13:51:36 +00:00
of pkBoolean:
case prs.bool
2021-07-16 17:11:19 +00:00
of false: result.add "#f"
of true: result.add "#t"
2021-06-02 13:51:36 +00:00
of pkFloat:
2021-07-16 17:11:19 +00:00
result.add($prs.float & "f")
2021-06-02 13:51:36 +00:00
of pkDouble:
2021-07-16 17:11:19 +00:00
result.add $prs.double
2021-06-02 13:51:36 +00:00
of pkSignedInteger:
2021-07-16 17:11:19 +00:00
result.add $prs.int
2021-06-02 13:51:36 +00:00
of pkBigInteger:
2021-07-16 17:11:19 +00:00
result.add $prs.bigint
2021-06-02 13:51:36 +00:00
of pkString:
2021-07-16 17:11:19 +00:00
result.add escapeJson(prs.string)
2021-06-02 13:51:36 +00:00
of pkByteString:
2021-07-16 17:11:19 +00:00
for b in prs.bytes:
if b.char notin {'\20'..'\21', '#'..'[', ']'..'~'}:
2021-08-09 23:41:18 +00:00
result.add("#[") #]#
2021-07-16 17:11:19 +00:00
result.add(base64.encode(prs.bytes))
result.add(']')
return
result.add("#\"")
result.add(cast[string](prs.bytes))
result.add('"')
2021-06-02 13:51:36 +00:00
of pkSymbol:
result.add(escapeJsonUnquoted(prs.symbol))
2021-06-02 13:51:36 +00:00
of pkRecord:
2021-06-24 15:31:30 +00:00
assert(prs.record.len > 0)
result.add('<')
2021-07-17 09:15:30 +00:00
result.concat(prs.record[prs.record.high])
2021-06-24 15:31:30 +00:00
for i in 0..<prs.record.high:
result.add(' ')
2021-07-16 17:11:19 +00:00
result.concat(prs.record[i])
result.add('>')
2021-06-02 13:51:36 +00:00
of pkSequence:
result.add('[')
for i, val in prs.sequence:
if i > 0:
result.add(' ')
2021-07-16 17:11:19 +00:00
result.concat(val)
result.add(']')
2021-06-02 13:51:36 +00:00
of pkSet:
result.add("#{")
for val in prs.set.items:
2021-07-16 17:11:19 +00:00
result.concat(val)
result.add(' ')
2021-07-16 17:11:19 +00:00
if prs.set.len > 1:
result.setLen(result.high)
result.add('}')
2021-06-02 13:51:36 +00:00
of pkDictionary:
result.add('{')
for (key, value) in prs.dict.pairs:
2021-07-16 17:11:19 +00:00
result.concat(key)
result.add(": ")
2021-07-16 17:11:19 +00:00
result.concat(value)
result.add(' ')
2021-07-16 17:11:19 +00:00
if prs.dict.len > 1:
result.setLen(result.high)
result.add('}')
2021-06-02 13:51:36 +00:00
of pkEmbedded:
result.add(prs.embedded.repr)
2021-07-16 17:11:19 +00:00
proc `$`*(prs: Preserve): string = concat(result, prs)
iterator items*(prs: Preserve): Preserve =
case prs.kind
of pkRecord:
for i in 0..prs.record.high.pred:
yield prs.record[i]
of pkSequence:
for e in prs.sequence.items: yield e
of pkSet:
for e in prs.set.items: yield e
of pkDictionary:
for k, v in prs.dict.pairs:
yield k # key can be an arbitrary Preserve
yield v
else: discard
2021-06-24 15:31:30 +00:00
func isRecord*(prs: Preserve): bool =
if prs.kind == pkRecord:
result = true
assert(prs.record.len > 0)
proc label*(prs: Preserve): Preserve {.inline.} =
## Return the label of a record value.
2021-06-24 15:31:30 +00:00
prs.record[prs.record.high]
proc arity*(prs: Preserve): int {.inline.} =
## Return the number of fields in a record value.
pred(prs.record.len)
proc fields*(prs: Preserve): seq[Preserve] {.inline.} =
## Return the fields of a record value.
2021-06-24 15:31:30 +00:00
prs.record[0..prs.record.high.pred]
iterator fields*(prs: Preserve): Preserve =
## Iterate the fields of a record value.
2021-06-24 15:31:30 +00:00
for i in 0..<prs.record.high: yield prs.record[i]
2021-06-02 13:51:36 +00:00
proc symbol*(s: string): Preserve {.inline.} =
## Symbol constructor.
Preserve(kind: pkSymbol, symbol: s)
2021-06-02 13:51:36 +00:00
proc writeVarint(s: Stream; n: int) =
var n = n
while true:
let c = int8(n and 0x7f)
n = n shr 7
if n == 0:
s.write((char)c.char)
break
else:
s.write((char)c or 0x80)
proc readVarint(s: Stream): int =
var shift: int
while shift < (9*8):
let c = s.readChar.int
result = result or ((c and 0x7f) shl shift)
if (c and 0x80) == 0:
break
shift.inc 7
proc write*(str: Stream; prs: Preserve) =
2021-06-02 13:51:36 +00:00
case prs.kind:
of pkBoolean:
case prs.bool
of false: str.write(0x80'u8)
of true: str.write(0x81'u8)
of pkFloat:
str.write(0x82'u8)
when system.cpuEndian == bigEndian:
str.write(prs.float)
else:
var be: float32
swapEndian32(be.addr, prs.float.unsafeAddr)
str.write(be)
of pkDouble:
str.write(0x83'u8)
when system.cpuEndian == bigEndian:
str.write(prs.double)
else:
var be: float64
swapEndian64(be.addr, prs.double.unsafeAddr)
str.write(be)
of pkSignedInteger:
if (-3 <= prs.int) and (prs.int <= 12):
str.write(0x90'i8 or int8(if prs.int < 0: prs.int + 16 else: prs.int))
else:
var bitCount = 1'u8
if prs.int < 0:
while ((not prs.int) shr bitCount) != 0:
inc(bitCount)
else:
while (prs.int shr bitCount) != 0:
inc(bitCount)
var byteCount = (bitCount + 8) div 8
str.write(0xa0'u8 or (byteCount - 1))
proc write(n: uint8; i: BiggestInt) =
2021-06-02 13:51:36 +00:00
if n > 0:
write(n.pred, i shr 8)
str.write(i.uint8)
write(byteCount, prs.int)
of pkBigInteger:
doAssert(Negative notin prs.bigint.flags, "negative big integers not implemented")
2021-06-02 13:51:36 +00:00
var bytes = newSeqOfCap[uint8](prs.bigint.limbs.len * 4)
var begun = false
for i in countdown(prs.bigint.limbs.high, 0):
let limb = prs.bigint.limbs[i]
for j in countdown(24, 0, 8):
let b = uint8(limb shr j)
begun = begun or (b != 0)
if begun:
bytes.add(b)
if bytes.len <= 16:
str.write(0xa0'u8 or bytes.high.uint8)
else:
str.write(0xb0'u8)
str.writeVarint(bytes.len)
str.write(cast[string](bytes))
of pkString:
str.write(0xb1'u8)
str.writeVarint(prs.string.len)
str.write(prs.string)
of pkByteString:
str.write(0xb2'u8)
str.writeVarint(prs.bytes.len)
2021-07-16 17:11:19 +00:00
str.write(cast[string](prs.bytes))
2021-06-02 13:51:36 +00:00
of pkSymbol:
str.write(0xb3'u8)
str.writeVarint(prs.symbol.len)
str.write(prs.symbol)
of pkRecord:
2021-06-24 15:31:30 +00:00
assert(prs.record.len > 0)
2021-06-02 13:51:36 +00:00
str.write(0xb4'u8)
2021-06-24 15:31:30 +00:00
str.write(prs.record[prs.record.high])
for i in 0..<prs.record.high:
str.write(prs.record[i])
2021-06-02 13:51:36 +00:00
str.write(0x84'u8)
of pkSequence:
str.write(0xb5'u8)
for e in prs.sequence:
2021-06-02 13:51:36 +00:00
str.write(e)
str.write(0x84'u8)
of pkSet:
str.write(0xb6'u8)
for val in prs.set.items:
str.write(val)
2021-06-02 13:51:36 +00:00
str.write(0x84'u8)
of pkDictionary:
str.write(0xb7'u8)
for (key, value) in prs.dict.pairs:
str.write(key)
str.write(value)
str.write(0x84'u8)
of pkEmbedded:
str.write(0x86'u8)
raiseAssert("binary representation of embedded values is undefined")
2021-06-02 13:51:36 +00:00
2021-07-16 17:11:19 +00:00
proc encode*(prs: Preserve): string =
let s = newStringStream()
s.write prs
s.setPosition 0
result = s.readAll
proc decodePreserves*(s: Stream): Preserve =
2021-06-02 13:51:36 +00:00
proc assertStream(check: bool) =
if not check:
raise newException(ValueError, "invalid Preserves stream")
const endMarker = 0x84
let tag = s.readUint8()
case tag
of 0x80: result = Preserve(kind: pkBoolean, bool: false)
of 0x81: result = Preserve(kind: pkBoolean, bool: true)
2021-06-02 13:51:36 +00:00
of 0x82:
when system.cpuEndian == bigEndian:
result = Preserve(kind: pkFloat, float: s.readFloat32())
2021-06-02 13:51:36 +00:00
else:
result = Preserve(kind: pkFloat)
2021-06-02 13:51:36 +00:00
var be = s.readFloat32()
swapEndian32(result.float.addr, be.addr)
of 0x83:
when system.cpuEndian == bigEndian:
result = Preserve(kind: pkDouble, double: s.readFloat64())
2021-06-02 13:51:36 +00:00
else:
result = Preserve(kind: pkDouble)
2021-06-02 13:51:36 +00:00
var be = s.readFloat64()
swapEndian64(result.double.addr, be.addr)
of 0x84:
assertStream(false)
of 0xb1:
result = Preserve(kind: pkString)
2021-06-02 13:51:36 +00:00
let len = s.readVarint()
result.string = s.readStr(len)
of 0xb2:
result = Preserve(kind: pkByteString)
2021-06-02 13:51:36 +00:00
let len = s.readVarint()
result.bytes = cast[seq[byte]](s.readStr(len))
of 0xb3:
let len = s.readVarint()
result = symbol(s.readStr(len))
2021-06-02 13:51:36 +00:00
of 0xb4:
result = Preserve(kind: pkRecord)
2021-07-16 17:11:19 +00:00
var label = s.decodePreserves()
2021-06-02 13:51:36 +00:00
while s.peekUint8() != endMarker:
2021-07-16 17:11:19 +00:00
result.record.add(s.decodePreserves())
2021-06-24 15:31:30 +00:00
result.record.add(label)
2021-06-02 13:51:36 +00:00
discard s.readUint8()
of 0xb5:
result = Preserve(kind: pkSequence)
2021-06-02 13:51:36 +00:00
while s.peekUint8() != endMarker:
2021-07-16 17:11:19 +00:00
result.sequence.add(s.decodePreserves())
2021-06-02 13:51:36 +00:00
discard s.readUint8()
of 0xb6:
result = Preserve(kind: pkSet)
2021-06-02 13:51:36 +00:00
while s.peekUint8() != endMarker:
2021-07-16 17:11:19 +00:00
result.set.incl(s.decodePreserves())
2021-06-02 13:51:36 +00:00
discard s.readUint8()
of 0xb7:
result = Preserve(kind: pkDictionary)
2021-06-02 13:51:36 +00:00
while s.peekUint8() != endMarker:
2021-07-16 17:11:19 +00:00
let key = s.decodePreserves()
let val = s.decodePreserves()
2021-06-02 13:51:36 +00:00
result.dict[key] = val
discard s.readUint8()
of 0xb0:
let len = s.readVarint()
2021-08-09 23:41:18 +00:00
result = Preserve(kind: pkBigInteger, bigint: initBigint 0)
for _ in 1..len:
2021-06-02 13:51:36 +00:00
result.bigint = (result.bigint shl 8) + s.readUint8().int32
else:
case 0xf0 and tag
of 0x90:
var n = tag.BiggestInt
result = Preserve(kind: pkSignedInteger,
2021-06-02 13:51:36 +00:00
int: n - (if n > 0x9c: 0xa0 else: 0x90))
of 0xa0:
let len = (tag.int and 0x0f) + 1
if len <= 8:
result = Preserve(kind: pkSignedInteger, int: s.readUint8().BiggestInt)
2021-06-02 13:51:36 +00:00
if (result.int and 0x80) != 0: result.int.dec(0x100)
for i in 1..<len:
result.int = (result.int shl 8) or s.readUint8().BiggestInt
else:
result = Preserve(kind: pkBigInteger)
2021-06-02 13:51:36 +00:00
for i in 0..<len:
result.bigint = (result.bigint shl 8) + s.readUint8().int32
else:
assertStream(false)
2021-07-16 17:11:19 +00:00
proc decodePreserves*(s: string): Preserve =
s.newStringStream.decodePreserves
proc decodePreserves*(s: seq[byte]): Preserve =
cast[string](s).newStringStream.decodePreserves
2021-07-15 12:24:30 +00:00
proc initDictionary*(): Preserve = Preserve(kind: pkDictionary)
proc `%`*(b: bool): Preserve =
Preserve(kind: pkBoolean, bool: b)
proc `%`*(f: float32): Preserve =
Preserve(kind: pkFloat, float: f)
proc `%`*(d: float64): Preserve =
Preserve(kind: pkDouble, double: d)
proc `%`*(n: SomeInteger): Preserve =
Preserve(kind: pkSignedInteger, int: n)
proc `%`*(b: Bigint): Preserve =
Preserve(kind: pkBigInteger, bigint: b)
proc `%`*(s: string): Preserve =
Preserve(kind: pkString, string: s)
proc `%`*(buf: openarray[byte]): Preserve =
Preserve(kind: pkByteString, bytes: @buf)
proc `%`*(e: enum): Preserve =
## Initialize a preserves symbol from the string
## representation of ``e``.
Preserve(kind: pkSymbol, symbol: $e)
template `%`*(p: Preserve): Preserve = p
proc `%`*[T](elems: openArray[T]): Preserve =
result = Preserve(kind: pkSequence,
sequence: newSeqOfCap[Preserve](elems.len))
for e in elems: result.sequence.add(%e)
proc `%`*[A,B](pairs: openArray[(A, B)]): Preserve =
result = Preserve(kind: pkDictionary)
for (k, v) in pairs:
when A is string:
result.dict[symbol k] = %v
else:
result.dict[%k] = %v
proc `%`*[T](set: HashSet[T]): Preserve =
result = Preserve(kind: pkSet #[, set: set.map(`%`)]#)
for e in set:
result.set.incl %e
proc `%`*[A,B](table: Table[A,B]|TableRef[A,B]): Preserve =
result = Preserve(kind: pkDictionary, dict: initTable[Preserve, Preserve](table.len))
for k, v in table.pairs: result.dict[%k] = %v
proc `%`*[T: object](o: T): Preserve =
## Construct JsonNode from tuples and objects.
result = initDictionary()
for k, v in o.fieldPairs: result.dict[symbol(k)] = %v
proc percentImpl(x: NimNode): NimNode {.compileTime.} =
case x.kind
of nnkBracket: # sequence
result = newNimNode(nnkBracket)
for i in 0 ..< x.len:
result.add(percentImpl(x[i]))
result = newCall(bindSym("%", brOpen), result)
of nnkTableConstr: # dictionary
if x.len == 0: return newCall(bindSym"initDictionary")
result = newNimNode(nnkTableConstr)
for i in 0 ..< x.len:
x[i].expectKind nnkExprColonExpr
result.add newTree(nnkExprColonExpr, x[i][0], percentImpl(x[i][1]))
result = newCall(bindSym("%", brOpen), result)
of nnkCurly: # set
result = newNimNode(nnkCurly)
for i in 0 ..< x.len:
result.add(percentImpl(x[i]))
result = newCall(bindSym("%", brOpen), result)
of nnkPar:
if x.len == 1: result = percentImpl(x[0])
else: result = newCall(bindSym("%", brOpen), x)
else:
result = newCall(bindSym("%", brOpen), x)
macro `%*`*(x: untyped): untyped =
result = percentImpl(x)
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>
## ```
2021-07-01 10:47:30 +00:00
template unpreservable*() {.pragma.}
## Pragma to forbid a type from being converted by `toPreserve`.
proc toPreserve*[T](x: T): Preserve =
## Serializes `x` to Preserves; uses `toPreserveHook(x: A)` if it's in scope to
## customize serialization.
when T is Preserve: result = x
2021-06-24 15:31:30 +00:00
elif T is Bigint:
result = Preserve(kind: pkBigInteger, bigint: x)
elif compiles(toPreserveHook(x)):
result = toPreserveHook(x)
elif T is array | seq:
result = Preserve(kind: pkSequence)
for v in x.items: result.sequence.add(toPreserve(v))
elif T is bool:
result = Preserve(kind: pkBoolean, bool: x)
elif T is distinct:
result = toPreserve(x.distinctBase)
elif T is float:
result = Preserve(kind: pkFloat, float: x)
elif T is float64:
result = Preserve(kind: pkDouble, double: x)
2021-06-24 15:31:30 +00:00
elif T is object | tuple:
when T.hasCustomPragma(unpreservable): {.fatal: "unpreservable type".}
2021-07-01 10:47:30 +00:00
elif T.hasCustomPragma(record):
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:
result = Preserve(kind: pkSignedInteger, int: x.ord.BiggestInt)
elif T is ptr | ref:
if system.`==`(x, nil): result = symbol"null"
else: result = toPreserve(x[])
elif T is string:
result = Preserve(kind: pkString, string: x)
elif T is SomeInteger:
result = Preserve(kind: pkSignedInteger, int: x.BiggestInt)
else:
2021-07-15 12:24:30 +00:00
result = %x
2021-06-02 13:51:36 +00:00
2021-06-24 15:31:30 +00:00
proc toPreserveHook*[T](set: HashSet[T]): Preserve =
Preserve(kind: pkSet, set: set.map(toPreserve))
proc toPreserveHook*(js: JsonNode): Preserve =
case js.kind
of JString:
result = Preserve(kind: pkString, string: js.str)
of JInt:
result = Preserve(kind: pkSignedInteger, int: js.num)
of JFloat:
result = Preserve(kind: pkDouble, double: js.fnum)
of JBool:
result = case js.bval
of false: symbol"false"
of true: symbol"true"
of JNull:
result = symbol"null"
of JObject:
result = Preserve(kind: pkDictionary)
for key, val in js.fields.pairs:
result.dict[Preserve(kind: pkString, string: key)] = toPreserveHook(val)
of JArray:
result = Preserve(kind: pkSequence,
sequence: newSeq[Preserve](js.elems.len))
for i, e in js.elems:
result.sequence[i] = toPreserveHook(e)
proc toJsonHook*(prs: Preserve): JsonNode =
2021-06-02 13:51:36 +00:00
case prs.kind:
of pkBoolean:
result = newJBool(prs.bool)
of pkFloat:
result = newJFloat(prs.float)
of pkDouble:
result = newJFloat(prs.double)
of pkSignedInteger:
result = newJInt(prs.int)
of pkBigInteger:
raise newException(ValueError, "cannot convert big integer to JSON")
of pkString:
result = newJString(prs.string)
of pkByteString:
raise newException(ValueError, "cannot convert bytes to JSON")
of pkSymbol:
case prs.symbol
of "false":
result = newJBool(false)
of "true":
result = newJBool(true)
of "null":
result = newJNull()
else:
raise newException(ValueError, "cannot convert symbol to JSON")
of pkRecord:
raise newException(ValueError, "cannot convert record to JSON")
of pkSequence:
result = newJArray()
for val in prs.sequence:
result.add(toJsonHook(val))
2021-06-02 13:51:36 +00:00
of pkSet:
raise newException(ValueError, "cannot convert set to JSON")
of pkDictionary:
result = newJObject()
for (key, val) in prs.dict.pairs:
if key.kind != pkString:
raise newException(ValueError, "cannot convert non-string dictionary key to JSON")
result[key.string] = toJsonHook(val)
2021-06-02 13:51:36 +00:00
of pkEmbedded:
raise newException(ValueError, "cannot convert embedded value to JSON")
2021-06-08 10:14:56 +00:00
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")
2021-06-24 15:31:30 +00:00
proc fromPreserve*[T](result: var T; prs: Preserve) =
# Inplace version of `preserveTo`.
when compiles(fromPreserveHook(result, prs)):
fromPreserveHook(result, prs)
elif T is Preserve:
result = prs
elif T is Bigint:
result = prs.bigint
elif T is bool:
result = prs.bool
elif T is SomeInteger:
result = T(prs.int)
elif T is float:
result = prs.float
elif T is seq:
result.setLen(prs.sequence.len)
for i, val in prs.sequence:
fromPreserve(result[i], val)
elif T is float64:
result = prs.double
elif T is object | tuple:
case prs.kind
of pkRecord:
checkRecordLabel(T, prs)
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
2021-06-24 15:31:30 +00:00
elif T is ptr | ref:
if system.`==`(x, nil): result = symbol"null"
else: result = toPreserve(x[])
elif T is string:
result = prs.string
elif T is distinct:
fromPreserve(result.distinctBase, prs)
2021-06-24 15:31:30 +00:00
else:
2021-07-15 12:24:30 +00:00
static: raiseAssert("cannot convert from Preserves: " & $T)
2021-06-24 15:31:30 +00:00
proc preserveTo*(prs: Preserve; T: typedesc): T =
## Reverse of `toPreserve`.
fromPreserve(result, prs)
proc len*(prs: Preserve): int =
## Return the number of values one level below ``prs``.
case prs.kind
of pkRecord: prs.record.len.pred
of pkSequence: prs.sequence.len
of pkSet: prs.set.len
of pkDictionary: prs.dict.len
else: 0
proc `[]`*(prs: Preserve; i: int): Preserve =
case prs.kind
2021-06-24 15:31:30 +00:00
of pkRecord: prs.record[i]
of pkSequence: prs.sequence[i]
else:
raise newException(ValueError, "`[]` is not valid for " & $prs.kind)