stealing
This commit is contained in:
26
storage/packable/packable.go
Normal file
26
storage/packable/packable.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package packable
|
||||
|
||||
type Packable interface {
|
||||
Encode() ([]byte, error)
|
||||
Decode([]byte) error
|
||||
}
|
||||
|
||||
type String string
|
||||
|
||||
func (s *String) String() string {
|
||||
return string(*s)
|
||||
}
|
||||
|
||||
func (s *String) Encode() ([]byte, error) {
|
||||
return []byte(*s), nil
|
||||
}
|
||||
|
||||
func (s *String) Decode(b []byte) error {
|
||||
*s = String(string(b))
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewString(s string) *String {
|
||||
w := String(s)
|
||||
return &w
|
||||
}
|
||||
23
storage/packable/packable_test.go
Normal file
23
storage/packable/packable_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package packable
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPackableString(t *testing.T) {
|
||||
raw := "hello"
|
||||
s := NewString(raw)
|
||||
if s.String() != raw {
|
||||
t.Errorf("cannot convert string to String: %v vs %v", s, raw)
|
||||
}
|
||||
|
||||
packed, err := s.Encode()
|
||||
if err != nil {
|
||||
t.Errorf("cannot encode String: %v", err)
|
||||
}
|
||||
|
||||
x := NewString("")
|
||||
if err := x.Decode(packed); err != nil {
|
||||
t.Errorf("cannot decode string: %v", err)
|
||||
} else if x.String() != raw {
|
||||
t.Errorf("wrong decoded string: %v vs %v", x, raw)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user