aboutsummaryrefslogtreecommitdiff
path: root/objects
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2017-06-24 21:37:21 +0200
committerLaria Carolin Chabowski <laria@laria.me>2017-06-24 21:50:25 +0200
commit82dd9f540be12c4c93679d2a60c4d1950e57eec8 (patch)
treefd4df66de9421d4d0895ac2409a7b72a8b077861 /objects
downloadpetrific-82dd9f540be12c4c93679d2a60c4d1950e57eec8.tar.gz
petrific-82dd9f540be12c4c93679d2a60c4d1950e57eec8.tar.bz2
petrific-82dd9f540be12c4c93679d2a60c4d1950e57eec8.zip
Add basic idea of objects
Diffstat (limited to 'objects')
-rw-r--r--objects/object.go105
-rw-r--r--objects/object_test.go88
2 files changed, 193 insertions, 0 deletions
diff --git a/objects/object.go b/objects/object.go
new file mode 100644
index 0000000..2bc3a53
--- /dev/null
+++ b/objects/object.go
@@ -0,0 +1,105 @@
+package objects
+
+import (
+ "fmt"
+ "io"
+ "strconv"
+ "strings"
+)
+
+type ObjectId interface {
+ fmt.Stringer
+}
+
+type ObjectType string
+
+const (
+ OTBlob ObjectType = "blob"
+ OTFile ObjectType = "file"
+ OTTree ObjectType = "tree"
+ OTSnapshot ObjectType = "snapshot"
+)
+
+type Object struct {
+ Type ObjectType
+ Payload []byte
+}
+
+func (o Object) Serialize(w io.Writer) error {
+ if _, err := fmt.Fprintf(w, "%s %d\n", o.Type, len(o.Payload)); err != nil {
+ return err
+ }
+
+ _, err := w.Write(o.Payload)
+ return err
+}
+
+type UnserializeError struct {
+ Reason error
+}
+
+func (err UnserializeError) Error() string {
+ if err.Reason == nil {
+ return "Invalid object"
+ } else {
+ return fmt.Sprintf("Invalid object: %s", err.Reason)
+ }
+}
+
+type bytewiseReader struct {
+ r io.Reader
+ buf []byte
+}
+
+func newBytewiseReader(r io.Reader) io.ByteReader {
+ return &bytewiseReader{
+ r: r,
+ buf: make([]byte, 1),
+ }
+}
+
+func (br *bytewiseReader) ReadByte() (b byte, err error) {
+ _, err = br.r.Read(br.buf)
+ b = br.buf[0]
+
+ return
+}
+
+func UnserializeObject(r io.Reader) (Object, error) {
+ br := newBytewiseReader(r)
+
+ line := []byte{}
+
+ for {
+ b, err := br.ReadByte()
+ if err != nil {
+ return Object{}, UnserializeError{err}
+ }
+
+ if b == '\n' {
+ break
+ }
+ line = append(line, b)
+ }
+
+ parts := strings.SplitN(string(line), " ", 2)
+ if len(parts) != 2 {
+ return Object{}, UnserializeError{}
+ }
+
+ size, err := strconv.ParseUint(parts[1], 10, 64)
+ if err != nil {
+ return Object{}, UnserializeError{err}
+ }
+
+ o := Object{
+ Type: ObjectType(parts[0]),
+ Payload: make([]byte, size),
+ }
+
+ if _, err := io.ReadFull(r, o.Payload); err != nil {
+ return Object{}, UnserializeError{err}
+ }
+
+ return o, nil
+}
diff --git a/objects/object_test.go b/objects/object_test.go
new file mode 100644
index 0000000..c596b39
--- /dev/null
+++ b/objects/object_test.go
@@ -0,0 +1,88 @@
+package objects
+
+import (
+ "bufio"
+ "bytes"
+ "testing"
+)
+
+func TestUnserializeSuccess(t *testing.T) {
+ r := bufio.NewReader(bytes.NewBuffer([]byte("blob 16\n0123456789abcdef")))
+
+ o, err := UnserializeObject(r)
+ if err != nil {
+ t.Fatalf("Unserialize failed: %s", err)
+ }
+
+ if o.Type != OTBlob {
+ t.Errorf("expected type %s, got %s", OTBlob, o.Type)
+ }
+
+ if !bytes.Equal([]byte("0123456789abcdef"), o.Payload) {
+ t.Errorf("Unexpected payload: (size %d) %v", len(o.Payload), o.Payload)
+ }
+}
+
+func TestUnserializeSuccess0Payload(t *testing.T) {
+ r := bufio.NewReader(bytes.NewBuffer([]byte("blob 0\n")))
+
+ o, err := UnserializeObject(r)
+ if err != nil {
+ t.Fatalf("Unserialize failed: %s", err)
+ }
+
+ if o.Type != OTBlob {
+ t.Errorf("expected type %s, got %s", OTBlob, o.Type)
+ }
+
+ if !bytes.Equal([]byte{}, o.Payload) {
+ t.Errorf("Unexpected payload: (size %d) %v", len(o.Payload), o.Payload)
+ }
+}
+
+func unserializeMustFail(b []byte, t *testing.T) {
+ r := bufio.NewReader(bytes.NewBuffer(b))
+
+ o, err := UnserializeObject(r)
+ if err == nil {
+ t.Fatalf("Expected an error, but object was successfully read: %#v", o)
+ }
+
+ _, ok := err.(UnserializeError)
+ if !ok {
+ t.Fatalf("Unknown error, expected an UnserializeError: %#v", err)
+ }
+}
+
+func TestUnserializeInvalidEmpty(t *testing.T) {
+ unserializeMustFail([]byte{}, t)
+}
+
+func TestUnserializeIncompleteHeader(t *testing.T) {
+ unserializeMustFail([]byte("foo\nbar"), t)
+}
+
+func TestUnserializeInvalidNumber(t *testing.T) {
+ unserializeMustFail([]byte("blob abc\nbar"), t)
+}
+
+func TestUnserializePayloadTooSmall(t *testing.T) {
+ unserializeMustFail([]byte("blob 10\nbar"), t)
+}
+
+func TestSerialize(t *testing.T) {
+ o := Object{
+ Type: OTBlob,
+ Payload: []byte("foo bar\nbaz"),
+ }
+
+ buf := new(bytes.Buffer)
+ if err := o.Serialize(buf); err != nil {
+ t.Fatalf("Serialization failed:%s", err)
+ }
+
+ b := buf.Bytes()
+ if !bytes.Equal(b, []byte("blob 11\nfoo bar\nbaz")) {
+ t.Errorf("Unexpected serialization result: %v", b)
+ }
+}