Commit dc419056 authored by Kirill Smelkov's avatar Kirill Smelkov

go/zodb/zeo: Factor-out string|bytes handling into encoding

parent ffe0d6e9
...@@ -186,3 +186,33 @@ func (e encoding) asOid(xv interface{}) (zodb.Oid, bool) { ...@@ -186,3 +186,33 @@ func (e encoding) asOid(xv interface{}) (zodb.Oid, bool) {
v, ok := e.xuint64Unpack(xv) v, ok := e.xuint64Unpack(xv)
return zodb.Oid(v), ok return zodb.Oid(v), ok
} }
// asBytes tries to retrieve bytes from corresponding object decoded via encoding e.
func (e encoding) asBytes(xb interface{}) ([]byte, bool) {
switch e {
default:
panic("bug")
case 'Z':
// pickle: str|bytes
s, err := pickletools.Xstrbytes(xb)
if err != nil {
return nil, false
}
return mem.Bytes(s), true
}
}
// asString tries to retrieve string from corresponding object decoded via encoding e.
func (e encoding) asString(xs interface{}) (string, bool) {
switch e {
default:
panic("bug")
case 'Z':
// pickle: str
s, ok := xs.(string)
return s, ok
}
}
...@@ -91,7 +91,7 @@ func (z *zeo) Load(ctx context.Context, xid zodb.Xid) (buf *mem.Buf, serial zodb ...@@ -91,7 +91,7 @@ func (z *zeo) Load(ctx context.Context, xid zodb.Xid) (buf *mem.Buf, serial zodb
return nil, 0, rpc.ereplyf("got %#v; expect 3-tuple", res) return nil, 0, rpc.ereplyf("got %#v; expect 3-tuple", res)
} }
data, ok1 := res[0].(string) data, ok1 := enc.asBytes(res[0])
serial, ok2 := enc.asTid(res[1]) serial, ok2 := enc.asTid(res[1])
// next_serial (res[2]) - just ignore // next_serial (res[2]) - just ignore
...@@ -99,7 +99,7 @@ func (z *zeo) Load(ctx context.Context, xid zodb.Xid) (buf *mem.Buf, serial zodb ...@@ -99,7 +99,7 @@ func (z *zeo) Load(ctx context.Context, xid zodb.Xid) (buf *mem.Buf, serial zodb
return nil, 0, rpc.ereplyf("got (%T, %v, %T); expect (str, tid, .)", res...) return nil, 0, rpc.ereplyf("got (%T, %v, %T); expect (str, tid, .)", res...)
} }
return &mem.Buf{Data: mem.Bytes(data)}, serial, nil return &mem.Buf{Data: data}, serial, nil
} }
func (z *zeo) Iterate(ctx context.Context, tidMin, tidMax zodb.Tid) zodb.ITxnIterator { func (z *zeo) Iterate(ctx context.Context, tidMin, tidMax zodb.Tid) zodb.ITxnIterator {
...@@ -202,13 +202,14 @@ func (r rpc) excError(exc string, argv []interface{}) error { ...@@ -202,13 +202,14 @@ func (r rpc) excError(exc string, argv []interface{}) error {
// zeo5Error decodes arg of reply with msgExcept flag set and returns // zeo5Error decodes arg of reply with msgExcept flag set and returns
// corresponding error. // corresponding error.
func (r rpc) zeo5Error(arg interface{}) error { func (r rpc) zeo5Error(arg interface{}) error {
enc := r.zlink.enc
// ('type', (arg1, arg2, arg3, ...)) // ('type', (arg1, arg2, arg3, ...))
texc, ok := arg.(pickle.Tuple) texc, ok := arg.(pickle.Tuple)
if !ok || len(texc) != 2 { if !ok || len(texc) != 2 {
return r.ereplyf("except5: got %#v; expect 2-tuple", arg) return r.ereplyf("except5: got %#v; expect 2-tuple", arg)
} }
exc, ok1 := texc[0].(string) exc, ok1 := enc.asString(texc[0])
argv, ok2 := texc[1].(pickle.Tuple) argv, ok2 := texc[1].(pickle.Tuple)
if !(ok1 && ok2) { if !(ok1 && ok2) {
return r.ereplyf("except5: got (%T, %T); expect (str, tuple)", texc...) return r.ereplyf("except5: got (%T, %T); expect (str, tuple)", texc...)
......
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