diff options
author | Laria Carolin Chabowski <laria@laria.me> | 2017-06-25 10:20:26 +0200 |
---|---|---|
committer | Laria Carolin Chabowski <laria@laria.me> | 2017-06-25 17:44:49 +0200 |
commit | fb14fb157c0835d61cbc7f8425d89b941bd0ab16 (patch) | |
tree | eb8a1d6e00cd4024f2f088326853be2174e7869d /objects/object.go | |
parent | 82dd9f540be12c4c93679d2a60c4d1950e57eec8 (diff) | |
download | petrific-fb14fb157c0835d61cbc7f8425d89b941bd0ab16.tar.gz petrific-fb14fb157c0835d61cbc7f8425d89b941bd0ab16.tar.bz2 petrific-fb14fb157c0835d61cbc7f8425d89b941bd0ab16.zip |
Implement object IDs
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) |