diff options
Diffstat (limited to 'objects/object.go')
-rw-r--r-- | objects/object.go | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/objects/object.go b/objects/object.go index 2bc3a53..a1d2335 100644 --- a/objects/object.go +++ b/objects/object.go @@ -7,10 +7,6 @@ import ( "strings" ) -type ObjectId interface { - fmt.Stringer -} - type ObjectType string const ( @@ -25,6 +21,7 @@ type Object struct { Payload []byte } +// SerialiteObject writes the binary representation of an object to a io.Writer 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 @@ -34,6 +31,16 @@ func (o Object) Serialize(w io.Writer) error { return err } +func (o Object) 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 } @@ -65,6 +72,8 @@ func (br *bytewiseReader) ReadByte() (b byte, err error) { return } +// UnserializeObject attempts to read an object from a stream. +// It is advisable to pass a buffered reader, if feasible. func UnserializeObject(r io.Reader) (Object, error) { br := newBytewiseReader(r) |