aboutsummaryrefslogtreecommitdiff
path: root/objects/object.go
blob: ced78bda6f6bcc8edd1d1480d6aeda089855af05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package objects

import (
	"fmt"
	"io"
	"strconv"
	"strings"
)

type ObjectType string

const (
	OTBlob     ObjectType = "blob"
	OTFile     ObjectType = "file"
	OTTree     ObjectType = "tree"
	OTSnapshot ObjectType = "snapshot"
)

type RawObject struct {
	Type    ObjectType
	Payload []byte
}

// Serialize writes the binary representation of an object to a io.Writer
func (o RawObject) 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
}

func (o RawObject) SerializeAndId(w io.Writer, algo ObjectIdAlgo) (ObjectId, error) {
	gen := algo.Generator()

	if err := o.Serialize(io.MultiWriter(w, gen)); err != nil {
		return ObjectId{}, err
	}

	return gen.GetId(), nil
}

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
}

// Unserialize attempts to read an object from a stream.
// It is advisable to pass a buffered reader, if feasible.
func Unserialize(r io.Reader) (RawObject, error) {
	br := newBytewiseReader(r)

	line := []byte{}

	for {
		b, err := br.ReadByte()
		if err != nil {
			return RawObject{}, UnserializeError{err}
		}

		if b == '\n' {
			break
		}
		line = append(line, b)
	}

	parts := strings.SplitN(string(line), " ", 2)
	if len(parts) != 2 {
		return RawObject{}, UnserializeError{}
	}

	size, err := strconv.ParseUint(parts[1], 10, 64)
	if err != nil {
		return RawObject{}, UnserializeError{err}
	}

	o := RawObject{
		Type:    ObjectType(parts[0]),
		Payload: make([]byte, size),
	}

	if _, err := io.ReadFull(r, o.Payload); err != nil {
		return RawObject{}, UnserializeError{err}
	}

	return o, nil
}

type Object interface {
	Type() ObjectType
	Payload() []byte
	FromPayload([]byte) error
}