diff options
author | Laria Carolin Chabowski <laria@laria.me> | 2017-06-25 21:49:04 +0200 |
---|---|---|
committer | Laria Carolin Chabowski <laria@laria.me> | 2017-06-25 21:49:04 +0200 |
commit | f98b4e4b8165497b64d28d3e54babd1a43ad46d6 (patch) | |
tree | 56f6a34122d46a210d975849c7c599adb924f820 /objects/object_file.go | |
parent | 1d21280ec755525c6158cd18d01ec4e9ca73d7a0 (diff) | |
download | petrific-f98b4e4b8165497b64d28d3e54babd1a43ad46d6.tar.gz petrific-f98b4e4b8165497b64d28d3e54babd1a43ad46d6.tar.bz2 petrific-f98b4e4b8165497b64d28d3e54babd1a43ad46d6.zip |
File and Blob objects
Diffstat (limited to 'objects/object_file.go')
-rw-r--r-- | objects/object_file.go | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/objects/object_file.go b/objects/object_file.go new file mode 100644 index 0000000..7551193 --- /dev/null +++ b/objects/object_file.go @@ -0,0 +1,103 @@ +package objects + +import ( + "bufio" + "bytes" + "errors" + "strconv" +) + +type FileFragment struct { + Blob ObjectId + Size uint64 +} + +func (ff FileFragment) toProperties() properties { + return properties{"blob": ff.Blob.String(), "size": strconv.FormatUint(ff.Size, 10)} +} + +func (ff *FileFragment) fromProperties(p properties) error { + blob, ok := p["blob"] + if !ok { + return errors.New("Field `blob` is missing") + } + + var err error + ff.Blob, err = ParseObjectId(blob) + if err != nil { + return err + } + + size, ok := p["size"] + if !ok { + return errors.New("Field `size` is missing") + } + + ff.Size, err = strconv.ParseUint(size, 10, 64) + return err +} + +func (a FileFragment) Equals(b FileFragment) bool { + return a.Blob.Equals(b.Blob) && a.Size == b.Size +} + +type File []FileFragment + +func (f File) Type() ObjectType { + return OTFile +} + +func (f File) Payload() []byte { + out := []byte{} + + for _, ff := range f { + b, err := ff.toProperties().MarshalText() + if err != nil { + panic(err) + } + + out = append(out, b...) + out = append(out, '\n') + } + + return out +} + +func (f *File) FromPayload(payload []byte) error { + sc := bufio.NewScanner(bytes.NewReader(payload)) + + for sc.Scan() { + line := sc.Bytes() + if len(line) == 0 { + continue + } + + props := make(properties) + if err := props.UnmarshalText(line); err != nil { + return nil + } + + ff := FileFragment{} + if err := ff.fromProperties(props); err != nil { + return err + } + + *f = append(*f, ff) + } + + return sc.Err() +} + +func (a File) Equals(b File) bool { + if len(a) != len(b) { + return false + } + + for i := range a { + if !a[i].Equals(b[i]) { + return false + } + } + + return true +} |