Remove % operator

Use toPreserves, % is not compatible with a parameterized Preserve
type.
This commit is contained in:
Emery Hemingway 2021-08-28 11:17:23 +02:00
parent 338a468466
commit 5aaac95ca1
3 changed files with 6 additions and 84 deletions

View File

@ -2,10 +2,12 @@
# SPDX-License-Identifier: ISC
import bigints
import std/[base64, endians, hashes, macros, options, sets, streams, strutils, tables, typetraits]
import std/[base64, endians, hashes, options, sets, streams, strutils, tables, typetraits]
import json except `%`, `%*`
from std/macros import hasCustomPragma, getCustomPragmaVal
type
PreserveKind* = enum
pkBoolean, pkFloat, pkDouble, pkSignedInteger, pkBigInteger, pkString,
@ -495,85 +497,6 @@ proc decodePreserves*(s: seq[byte]): Preserve =
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 `%`*[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.
##

View File

@ -39,7 +39,7 @@ proc classOf*(T: typedesc[tuple]): RecordClass =
proc init*(rec: RecordClass; fields: varargs[Preserve, toPreserve]): Preserve =
## Initialize a new record value.
assert(fields.len == rec.arity, $(%fields) & " (arity " & $fields.len & ") is not of arity " & $rec.arity)
assert(fields.len == rec.arity, $(fields.toPreserve) & " (arity " & $fields.len & ") is not of arity " & $rec.arity)
result = initRecord(rec.label, fields)
proc init*(T: typedesc[tuple]; fields: varargs[Preserve, toPreserve]): Preserve =

View File

@ -34,6 +34,5 @@ suite "conversions":
suite "%":
template check(p: Preserve; s: string) =
test s: check($p == s)
check %false, "#f"
check %[0, 1, 2, 3], "[0 1 2 3]"
#check %*{0, 1, 2, 3}, """#{ #t #f }"""
check false.toPreserve, "#f"
check [0, 1, 2, 3].toPreserve, "[0 1 2 3]"