Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 629a35c905
Fetching contributors…

Cannot retrieve contributors at this time

file 80 lines (66 sloc) 1.674 kb
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
// Copyright (c) 2012, SoundCloud Ltd.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Source code and contact info at http://github.com/soundcloud/visor

package visor

import (
"fmt"
)

// File represents a coordinator file
// at a specific point in time.
type File struct {
Snapshot
Path string
Value interface{}
Codec Codec
}

func CreateFile(snapshot Snapshot, path string, value interface{}, codec Codec) (*File, error) {
file := &File{Path: path, Value: value, Codec: codec, Snapshot: snapshot}
return file.Create()
}

func (f *File) createSnapshot(rev int64) (file Snapshotable) {
tmp := *f
tmp.Snapshot = Snapshot{rev, f.conn}
return &tmp
}

// FastForward advances the file in time. It returns
// a new instance of File with the supplied revision.
func (f *File) FastForward(rev int64) *File {
if rev == -1 {
var err error
_, rev, err = f.conn.Stat(f.Path)
if err != nil {
return f
}
}
return f.Snapshot.fastForward(f, rev).(*File)
}

// Del deletes a file
func (f *File) Del() error {
return f.conn.Del(f.Path, f.Rev)
}

// Create creates a file from its Value attribute
func (f *File) Create() (*File, error) {
return f.Update(f.Value)
}

// Update sets the value at this file's path to a new value.
func (f *File) Update(value interface{}) (file *File, err error) {
evalue, err := f.Codec.Encode(value)
if err != nil {
return
}

rev, err := f.conn.Set(f.Path, f.Rev, evalue)

if rev > 0 {
file = f.FastForward(rev)
} else {
file = f
}

if err != nil {
return
}
file.Value = value

return
}

func (f *File) String() string {
return fmt.Sprintf("%#v", f)
}
Something went wrong with that request. Please try again.