change fdb string representation to common format

This commit is contained in:
Pieter Joost van de Sande 2019-01-09 00:03:44 +01:00
parent 0401d04380
commit 4722fa0667
2 changed files with 36 additions and 1 deletions

View File

@ -34,6 +34,7 @@ import (
"fmt" "fmt"
"log" "log"
"runtime" "runtime"
"strings"
"sync" "sync"
"unsafe" "unsafe"
) )
@ -384,6 +385,23 @@ func (k Key) FDBKey() Key {
return k return k
} }
// String describes the key as a hexadecimal encoded string.
func (k Key) String() string {
var sb strings.Builder
for _, b := range k {
if b >= 32 && b < 127 && b != '\\' {
sb.WriteByte(b)
continue
}
if b == '\\' {
sb.WriteString("\\\\")
continue
}
sb.WriteString(fmt.Sprintf("\\x%x", b))
}
return sb.String()
}
func panicToError(e *error) { func panicToError(e *error) {
if r := recover(); r != nil { if r := recover(); r != nil {
fe, ok := r.(Error) fe, ok := r.(Error)

View File

@ -24,8 +24,9 @@ package fdb_test
import ( import (
"fmt" "fmt"
"github.com/apple/foundationdb/bindings/go/src/fdb"
"testing" "testing"
"github.com/apple/foundationdb/bindings/go/src/fdb"
) )
func ExampleOpenDefault() { func ExampleOpenDefault() {
@ -261,3 +262,19 @@ func ExampleRangeIterator() {
// banana is bar // banana is bar
// cherry is baz // cherry is baz
} }
func TestKeyToString(t *testing.T) {
cases := []struct {
key fdb.Key
expect string
}{
{fdb.Key("plain-text"), "plain-text"},
{fdb.Key("\xbdascii☻☺"), "\\xbdascii\\xe2\\x98\\xbb\\xe2\\x98\\xba"},
}
for i, c := range cases {
if s := c.key.String(); s != c.expect {
t.Errorf("got '%v', want '%v' at case %v", s, c.expect, i)
}
}
}