-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP, generic coders & sample access functions
- Loading branch information
Showing
3 changed files
with
179 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import pkg/questionable | ||
import pkg/questionable/results | ||
import pkg/stew/endians2 | ||
import pkg/stew/byteutils | ||
|
||
proc autoencode*[T: tuple | object](tup: T): seq[byte] = | ||
var buffer = newSeq[byte]() | ||
for f in fields(tup): | ||
when (compiles do: | ||
let _: seq[byte] = encode(f)): | ||
let fBytes = encode(f) | ||
buffer.add(cast[uint64](fBytes.len).toBytesBE) # TODO user varint | ||
buffer.add(fBytes) | ||
else: | ||
{.error: "please provide `proc encode(a: " & $typeof(a) & "): seq[byte]`".} | ||
|
||
return buffer | ||
|
||
proc autodecode*(T: typedesc[tuple | object], bytes: seq[byte]): ?!T = | ||
var | ||
res = default(T) | ||
offset = 0 | ||
for f in fields(res): | ||
let fLen = cast[int](uint64.fromBytesBE(bytes[offset..<offset + sizeof(uint64)])) # TODO user varint | ||
offset.inc(sizeof(uint64)) | ||
f = ? decode(typeof(f), bytes[offset..<offset + fLen]) | ||
offset.inc(fLen) | ||
|
||
return success(res) |
Submodule nim-datastore
updated
from 0cde8a to 4c5ced