Commit ef5d4305 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 1fb615dc
......@@ -66,6 +66,16 @@ func (xid Xid) String() string {
return xid.XTid.String() + ":" + xid.Oid.String() // XXX use "·" instead of ":" ?
}
/* TODO reenable
func (xtid XTid) XFmtString(b []byte) []byte {
b .C("=<"[bint(xtid.TidBefore)]) .V(xtid.Tid)
}
func (xid Xid) XFmtString(b xfmt.Buffer) xfmt.Buffer {
b .V(xid.XTid) .C(':') .V(xid.Oid)
}
*/
// parseHex64 decode 16-character-wide hex-encoded string into uint64
// XXX -> xfmt ?
......@@ -93,6 +103,55 @@ func ParseOid(s string) (Oid, error) {
return Oid(x), err
}
func ParseXTid(s string) (XTid, error) {
if len(s) < 1 {
goto Error
}
{
var tidBefore bool
switch s[0] {
case '<':
tidBefore = true
case '=':
tidBefore = false
default:
goto Error
}
tid, err := ParseTid(s[1:])
if err != nil {
goto Error
}
return XTid{tid, tidBefore}, nil
}
Error:
return XTid{}, fmt.Errorf("xtid %q invalid", s)
}
func ParseXid(s string) (Xid, error) {
xtids, oids, err := xstrings.Split2(s, ":")
if err != nil {
goto Error
}
{
xtid, err1 := ParseXTid(xtids)
oid, err2 := ParseOid(oids)
if err1 != nil || err2 != nil {
goto Error
}
return Xid{xtid, oid}, nil
}
Error:
return Xid{}, fmt.Errorf("xid %q invalid", s)
}
// ParseTidRange parses string of form "<tidmin>..<tidmax>" into tidMin, tidMax pair
// both <tidmin> and <tidmax> can be empty, in which case defaults 0 and TidMax are returned
// XXX also check tidMin < tidMax here? or allow reverse ranges ?
......
......@@ -19,6 +19,14 @@ package zodb
import "testing"
// estr returns string corresponding to error or "" for nil
func estr(err error) string {
if err == nil {
return ""
}
return err.Error()
}
func TestParseHex64(t *testing.T) {
var testv = []struct {in string; out uint64; estr string} {
{"", 0, `tid "" invalid`},
......@@ -29,13 +37,52 @@ func TestParseHex64(t *testing.T) {
for _, tt := range testv {
x, err := parseHex64("tid", tt.in)
estr := ""
if err != nil {
estr = err.Error()
if !(x == tt.out && estr(err) == tt.estr) {
t.Errorf("parsehex64: %v: test error:\nhave: %v %q\nwant: %v %q", tt.in, x, err, tt.out, tt.estr)
}
}
}
func TestParseXTid(t *testing.T) {
var testv = []struct {in string; xtid XTid; estr string} {
{"", XTid{}, `xtid "" invalid`},
{"a", XTid{}, `xtid "a" invalid`},
{"0123456789abcdef", XTid{}, `xtid "0123456789abcdef" invalid`}, // XXX or let it be < by default ?
{"z0123456789abcdef", XTid{}, `xtid "z0123456789abcdef" invalid`},
{"=0123456789abcdef", XTid{0x0123456789abcdef, false}, ""},
{"<0123456789abcdef", XTid{0x0123456789abcdef, true}, ""},
}
for _, tt := range testv {
xtid, err := ParseXTid(tt.in)
if !(xtid == tt.xtid && estr(err) == tt.estr) {
t.Errorf("parsextid: %v: test error:\nhave: %v %q\nwant: %v %q",
tt.in, xtid, err, tt.xtid, tt.estr)
}
}
}
if !(x == tt.out && estr == tt.estr) {
t.Errorf("parsehex64: %v: test error:\nhave: %v %q\nwant: %v %q", tt.in, x, estr, tt.out, tt.estr)
func TestParseXid(t *testing.T) {
var testv = []struct {in string; xid Xid; estr string} {
{"", Xid{}, `xid "" invalid`},
{"a", Xid{}, `xid "a" invalid`},
{"0123456789abcdef", Xid{}, `xid "0123456789abcdef" invalid`}, // XXX or let it be < by default ?
{"z0123456789abcdef", Xid{}, `xid "z0123456789abcdef" invalid`},
{"=0123456789abcdef", Xid{}, `xid "=0123456789abcdef" invalid`},
{"<0123456789abcdef", Xid{}, `xid "<0123456789abcdef" invalid`},
{"=0123456789abcdef|fedcba9876543210", Xid{}, `xid "=0123456789abcdef|fedcba9876543210" invalid`},
{"<0123456789abcdef|fedcba9876543210", Xid{}, `xid "<0123456789abcdef|fedcba9876543210" invalid`},
{"=0123456789abcdef:fedcba9876543210", Xid{XTid{0x0123456789abcdef, false}, 0xfedcba9876543210}, ""},
{"<0123456789abcdef:fedcba9876543210", Xid{XTid{0x0123456789abcdef, true}, 0xfedcba9876543210}, ""},
}
for _, tt := range testv {
xid, err := ParseXid(tt.in)
if !(xid == tt.xid && estr(err) == tt.estr) {
t.Errorf("parsexid: %v: test error:\nhave: %v %q\nwant: %v %q",
tt.in, xid, err, tt.xid, tt.estr)
}
}
}
......
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