From 2a618231061ab4052940313e2cc54916677a6bfb Mon Sep 17 00:00:00 2001 From: Pieter Joost Date: Fri, 6 Dec 2019 22:25:32 +0100 Subject: [PATCH] iimplement fmt.Stringer interface on Subspace --- bindings/go/src/fdb/subspace/subspace.go | 10 ++++++++++ bindings/go/src/fdb/subspace/subspace_test.go | 15 +++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 bindings/go/src/fdb/subspace/subspace_test.go diff --git a/bindings/go/src/fdb/subspace/subspace.go b/bindings/go/src/fdb/subspace/subspace.go index 353d377e42..b621d058c7 100644 --- a/bindings/go/src/fdb/subspace/subspace.go +++ b/bindings/go/src/fdb/subspace/subspace.go @@ -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()...)} } diff --git a/bindings/go/src/fdb/subspace/subspace_test.go b/bindings/go/src/fdb/subspace/subspace_test.go new file mode 100644 index 0000000000..33a4164697 --- /dev/null +++ b/bindings/go/src/fdb/subspace/subspace_test.go @@ -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) + } +}