-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
98 lines (77 loc) · 2.47 KB
/
api.go
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
package tagger
import (
"code.google.com/p/go-uuid/uuid"
"errors"
"io"
)
type (
// StorageProvider specifies the interface that must be implemented by tag storage backends.
StorageProvider interface {
io.Closer
GetFile(u uuid.UUID) (File, error)
GetFileForPath(path string) (File, error)
GetAllFiles() ([]File, error)
GetMatchingFiles(f Filter) ([]File, error)
UpdateTag(f File, t Tag) error
RemoveTag(f File, t Tag) error
GetTags(f File) ([]Tag, error)
// GetAllTags() ([]Tag, error) // TODO: Reconsider this method. Maybe split into two? (tags, values)
UpdateFile(f File, t []Tag) error
RemoveFile(f File) error
}
// File is a structure that represents a file in the database
File struct {
uuid uuid.UUID
path string
}
// Tag is an interface representing the needed methods on a tag
Tag interface {
Name() string
HasValue() bool
Value() int
}
// NamedTag is a tag with just a name
NamedTag struct {
name string
}
// ValueTag is a tag with both a name and a value
ValueTag struct {
name string
value int
}
)
// NewFile creates a new file struct an populates it's fields
func NewFile(uuid_ uuid.UUID, path string) File {
return File{uuid: uuid_, path: path}
}
// NewNamedTag creates a new NamedTag struct an populates it's fields
func NewNamedTag(name string) *NamedTag {
return &NamedTag{name: name}
}
// NewValueTag creates a new ValueTag struct an populates it's fields
func NewValueTag(name string, value int) *ValueTag {
return &ValueTag{name: name, value: value}
}
// UUID returns the UUID of a file
func (f File) UUID() uuid.UUID { return f.uuid }
// Path returns the path of a file
func (f File) Path() string { return f.path }
// Name returns the name of a tag
func (t NamedTag) Name() string { return t.name }
// Name returns the name of a tag
func (t ValueTag) Name() string { return t.name }
// HasValue returns whether the tag has a value
func (t NamedTag) HasValue() bool { return false }
// HasValue returns whether the tag has a value
func (t ValueTag) HasValue() bool { return true }
// Value returns -1 on a named tag
func (t NamedTag) Value() int { return -1 }
// Value returns the value of a value tag
func (t ValueTag) Value() int { return t.value }
// Errors
var (
ErrNoFile = errors.New("tagger: No such file in storage")
ErrNoTag = errors.New("tagger: No such tag on file")
ErrNoMatches = errors.New("tagger: No matching files in storage")
ErrInvalidValue = errors.New("tagger: Invalid tag value")
)