Commit a552d9cd authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 58a07e39
// Copyright (C) 2017 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
// Copyright (C) 2017-2019 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
......@@ -25,7 +25,11 @@
package pickletools
import (
"encoding/binary"
"fmt"
"math/big"
pickle "github.com/kisielk/og-rek"
)
// Xint64 tries to convert unpickled value to int64.
......@@ -43,3 +47,34 @@ func Xint64(xv interface{}) (v int64, ok bool) {
return 0, false
}
// Xstrbytes verifies and extacts str|bytes from unpickled value.
func Xstrbytes(x interface{}) (string, error) {
var s string
switch x := x.(type) {
default:
return "", fmt.Errorf("expect str|bytes; got %T", x)
case string:
s = x
case pickle.Bytes:
s = string(x)
}
return s, nil
}
// Xstrbytes8 verifies and extracts [8](str|bytes) from unpickled value as big-endian u64.
func Xstrbytes8(x interface{}) (uint64, error) {
s, err := Xstrbytes(x)
if err != nil {
return 0, err
}
if len(s) != 8 {
return 0, fmt.Errorf("expect [8]bytes; got [%d]bytes", len(s))
}
return binary.BigEndian.Uint64([]byte(s)), nil
}
......@@ -22,11 +22,12 @@ package zodb
import (
"bytes"
"encoding/binary"
"fmt"
"strings"
pickle "github.com/kisielk/og-rek"
"lab.nexedi.com/kirr/neo/go/zodb/internal/pickletools"
"lab.nexedi.com/kirr/go123/xerr"
)
......@@ -241,7 +242,7 @@ func xoid(x interface{}) (_ Oid, err error) {
// ZODB >= 5.4 encodes oid as bytes; before - as str:
// https://github.com/zopefoundation/ZODB/commit/12ee41c473
v, err := xstrbytes8(x)
v, err := pickletools.Xstrbytes8(x)
if err != nil {
return InvalidOid, err
}
......@@ -249,39 +250,6 @@ func xoid(x interface{}) (_ Oid, err error) {
return Oid(v), nil
}
// -> pickletools
// xstrbytes verifies and extacts str|bytes from unpickled value.
func xstrbytes(x interface{}) (string, error) {
var s string
switch x := x.(type) {
default:
return "", fmt.Errorf("expect str|bytes; got %T", x)
case string:
s = x
case pickle.Bytes:
s = string(x)
}
return s, nil
}
// xstrbytes8 verifies and extracts [8](str|bytes) from unpickled value as big-endian u64.
func xstrbytes8(x interface{}) (uint64, error) {
s, err := xstrbytes(x)
if err != nil {
return 0, err
}
if len(s) != 8 {
return 0, fmt.Errorf("expect [8]bytes; got [%d]bytes", len(s))
}
return binary.BigEndian.Uint64([]byte(s)), nil
}
// pyclassPath returns full path for a python class.
//
// for example class "ABC" in module "wendelin.lib" has its full path as "wendelin.lib.ABC".
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment