iimplement fmt.Stringer interface on Subspace

This commit is contained in:
Pieter Joost 2019-12-06 22:25:32 +01:00 committed by Vishesh Yadav
parent 16f3a2480b
commit 2a61823106
2 changed files with 25 additions and 0 deletions

View File

@ -35,6 +35,8 @@ package subspace
import (
"bytes"
"errors"
"fmt"
"github.com/apple/foundationdb/bindings/go/src/fdb"
"github.com/apple/foundationdb/bindings/go/src/fdb/tuple"
)
@ -42,6 +44,8 @@ import (
// Subspace represents a well-defined region of keyspace in a FoundationDB
// database.
type Subspace interface {
fmt.Stringer
// Sub returns a new Subspace whose prefix extends this Subspace with the
// encoding of the provided element(s). If any of the elements are not a
// valid tuple.TupleElement, Sub will panic.
@ -105,6 +109,12 @@ func FromBytes(b []byte) Subspace {
return subspace{s}
}
// String implements the fmt.Stringer interface and return the subspace
// as a human readable byte string provided by fdb.Printable.
func (s subspace) String() string {
return fdb.Printable(s.b)
}
func (s subspace) Sub(el ...tuple.TupleElement) Subspace {
return subspace{concat(s.Bytes(), tuple.Tuple(el).Pack()...)}
}

View File

@ -0,0 +1,15 @@
package subspace
import (
"fmt"
"testing"
)
func TestSubspaceString(t *testing.T) {
printed := fmt.Sprint(Sub([]byte("hello"), "world", 42, 0x99))
expected := "\\x01hello\\x00\\x02world\\x00\\x15*\\x15\\x99"
if printed != expected {
t.Fatalf("printed subspace result differs, expected %v, got %v", expected, printed)
}
}