initial commits
parent
0e9afc8511
commit
43b4f22894
32
serialize.go
32
serialize.go
|
@ -5,6 +5,11 @@ import (
|
|||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
serializationprefix = "x"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -19,25 +24,42 @@ var (
|
|||
|
||||
func serialize(value uint64) string {
|
||||
|
||||
var storage bytes.Buffer
|
||||
var binstorage bytes.Buffer
|
||||
{
|
||||
|
||||
err := binary.Write(&storage, binary.BigEndian, value)
|
||||
err := binary.Write(&binstorage, binary.BigEndian, value)
|
||||
if nil != err {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
var encoded string
|
||||
var encoded strings.Builder
|
||||
{
|
||||
encoded = base64encoding.EncodeToString(storage.Bytes())
|
||||
encoded.WriteString(serializationprefix)
|
||||
}
|
||||
{
|
||||
var wc io.WriteCloser = base64.NewEncoder(base64encoding, &encoded)
|
||||
if nil == wc {
|
||||
return ""
|
||||
}
|
||||
wc.Write(binstorage.Bytes())
|
||||
wc.Close()
|
||||
|
||||
}
|
||||
|
||||
return encoded
|
||||
return encoded.String()
|
||||
}
|
||||
|
||||
func unserialize(value string) (uint64, bool) {
|
||||
|
||||
{
|
||||
if !strings.HasPrefix(value, serializationprefix) {
|
||||
return badvalue, false
|
||||
}
|
||||
|
||||
value = value[len(serializationprefix):]
|
||||
}
|
||||
|
||||
var p []byte
|
||||
{
|
||||
var err error
|
||||
|
|
|
@ -13,6 +13,7 @@ func TestSerialize(t *testing.T) {
|
|||
actual, successful := unserialize(serialized)
|
||||
if !successful {
|
||||
t.Errorf("For test #%d, expected unserialization of serialized data to be successful but wasn't." , testNumber)
|
||||
t.Logf("SERIALIZED: %s", serialized)
|
||||
t.Logf("SUCCESSFUL: %t", successful)
|
||||
t.Logf("VALUE: %064b", test.Value)
|
||||
continue
|
||||
|
@ -21,6 +22,7 @@ func TestSerialize(t *testing.T) {
|
|||
|
||||
if expected := test.Value; expected != actual {
|
||||
t.Errorf("For test #%d, ", testNumber)
|
||||
t.Logf("SERIALIZED: %s", serialized)
|
||||
t.Logf("SUCCESSFUL: %t", successful)
|
||||
t.Logf("EXPECTED: %064b", expected)
|
||||
t.Logf("ACTUAL: %064b", actual)
|
||||
|
|
Loading…
Reference in New Issue