Improve the marshal module (#16777)

* Improve marshal

Use runnableExamples
Refactor tests

* Readd {.inheritable.} test

Apply suggestions
This commit is contained in:
konsumlamm 2021-01-22 19:52:34 +01:00 • committed by GitHub
commit d2b218b80a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 106 additions and 193 deletions

View file

@ -10,39 +10,32 @@
## This module contains procs for `serialization`:idx: and `deserialization`:idx:
## of arbitrary Nim data structures. The serialization format uses `JSON`:idx:.
##
## **Restriction**: For objects their type is **not** serialized. This means
## **Restriction**: For objects, their type is **not** serialized. This means
## essentially that it does not work if the object has some other runtime
## type than its compiletime type.
##
##
## Basic usage
## ===========
##
## .. code-block:: nim
##
## type
## A = object of RootObj
## B = object of A
## f: int
##
## var
## a: ref A
## b: ref B
##
## new(b)
## a = b
## echo($$a[]) # produces "{}", not "{f: 0}"
##
## # unmarshal
## let c = to[B]("""{"f": 2}""")
## assert typeof(c) is B
## assert c.f == 2
##
## # marshal
## let s = $$c
## assert s == """{"f": 2}"""
##
## **Note**: The ``to`` and ``$$`` operations are available at compile-time!
runnableExamples:
type
A = object of RootObj
B = object of A
f: int
let a: ref A = new(B)
assert $$a[] == "{}" # not "{f: 0}"
# unmarshal
let c = to[B]("""{"f": 2}""")
assert typeof(c) is B
assert c.f == 2
# marshal
assert $$c == """{"f": 2}"""
## **Note**: The `to` and `$$` operations are available at compile-time!
##
##
## See also
@ -61,7 +54,7 @@ Please use alternative packages for serialization.
It is possible to reimplement this module using generics and type traits.
Please contribute a new implementation.""".}
import streams, typeinfo, json, intsets, tables, unicode
import std/[streams, typeinfo, json, intsets, tables, unicode]
proc ptrToInt(x: pointer): int {.inline.} =
result = cast[int](x) # don't skip alignment
@ -279,7 +272,8 @@ proc loadAny(s: Stream, a: Any, t: var Table[BiggestInt, pointer]) =
proc load*[T](s: Stream, data: var T) =
## Loads `data` from the stream `s`. Raises `IOError` in case of an error.
runnableExamples:
import marshal, streams
import std/streams
var s = newStringStream("[1, 3, 5]")
var a: array[3, int]
load(s, a)
@ -291,7 +285,8 @@ proc load*[T](s: Stream, data: var T) =
proc store*[T](s: Stream, data: T) =
## Stores `data` into the stream `s`. Raises `IOError` in case of an error.
runnableExamples:
import marshal, streams
import std/streams
var s = newStringStream("")
var a = [1, 3, 5]
store(s, a)
@ -306,7 +301,8 @@ proc store*[T](s: Stream, data: T) =
proc `$$`*[T](x: T): string =
## Returns a string representation of `x` (serialization, marshalling).
##
## **Note:** to serialize `x` to JSON use `$(%x)` from the ``json`` module.
## **Note:** to serialize `x` to JSON use `%x` from the `json` module
## or `jsonutils.toJson(x)`.
runnableExamples:
type
Foo = object
@ -325,7 +321,7 @@ proc `$$`*[T](x: T): string =
result = s.data
proc to*[T](data: string): T =
## Reads data and transforms it to a type ``T`` (deserialization, unmarshalling).
## Reads data and transforms it to a type `T` (deserialization, unmarshalling).
runnableExamples:
type
Foo = object
@ -341,77 +337,3 @@ proc to*[T](data: string): T =
var tab = initTable[BiggestInt, pointer]()
loadAny(newStringStream(data), toAny(result), tab)
when not defined(testing) and isMainModule:
template testit(x: untyped) = echo($$to[typeof(x)]($$x))
var x: array[0..4, array[0..4, string]] = [
["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
["test", "1", "2", "3", "4"]]
testit(x)
var test2: tuple[name: string, s: uint] = ("tuple test", 56u)
testit(test2)
type
TE = enum
blah, blah2
TestObj = object
test, asd: int
case test2: TE
of blah:
help: string
else:
nil
PNode = ref Node
Node = object
next, prev: PNode
data: string
proc buildList(): PNode =
new(result)
new(result.next)
new(result.prev)
result.data = "middle"
result.next.data = "next"
result.prev.data = "prev"
result.next.next = result.prev
result.next.prev = result
result.prev.next = result
result.prev.prev = result.next
var test3: TestObj
test3.test = 42
test3 = TestObj(test2: blah)
testit(test3)
var test4: ref tuple[a, b: string]
new(test4)
test4.a = "ref string test: A"
test4.b = "ref string test: B"
testit(test4)
var test5 = @[(0, 1), (2, 3), (4, 5)]
testit(test5)
var test6: set[char] = {'A'..'Z', '_'}
testit(test6)
var test7 = buildList()
echo($$test7)
testit(test7)
type
A {.inheritable.} = object
B = object of A
f: int
var
a: ref A
b: ref B
new(b)
a = b
echo($$a[]) # produces "{}", not "{f: 0}"