forked from lytics/cloudstorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
74 lines (64 loc) · 2.22 KB
/
query.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
package cloudstorage
import (
"sort"
)
// Filter func type definition for filtering objects
type Filter func(objects Objects) Objects
// Query used to query the cloud source. The primary query is a prefix query like
// `ls /my-csv-files/baseball/*`. This is the Request, and includes the
// PageSize, cursor/next token as well.
type Query struct {
Delimiter string // Delimiter is most likely "/"
Prefix string // prefix (directory) to search for or object name if one file
StartOffset string // (gcs/localfs only) "bar/", Only list objects lexicographically >= "bar/"
EndOffset string // (gcs/localfs only) "foo/", Only list objects lexicographically < "foo/"
Marker string // Next Page Marker if provided is a start next page fetch bookmark.
ShowHidden bool // Show hidden files?
Filters []Filter // Applied to the result sets to filter out Objects (i.e. remove objects by extension)
PageSize int // PageSize defaults to global, or you can supply an override
}
// NewQuery create a query for finding files under given prefix.
func NewQuery(prefix string) Query {
return Query{
Prefix: prefix,
}
}
// NewQueryAll query for all objects/files.
func NewQueryAll() Query {
return Query{}
}
// NewQueryForFolders create a query for finding Folders under given path.
func NewQueryForFolders(folderPath string) Query {
return Query{
Delimiter: "/",
Prefix: folderPath,
}
}
// AddFilter adds a post prefix query, that can be used to alter results set
// from the prefix query.
func (q *Query) AddFilter(f Filter) *Query {
if q.Filters == nil {
q.Filters = make([]Filter, 0)
}
q.Filters = append(q.Filters, f)
return q
}
// Sorted added a sort Filter to the filter chain, if its not the last call
// while building your query, Then sorting is only guaranteed for the next
// filter in the chain.
func (q *Query) Sorted() *Query {
q.AddFilter(ObjectSortFilter)
return q
}
// ApplyFilters is called as the last step in store.List() to filter out the
// results before they are returned.
func (q *Query) ApplyFilters(objects Objects) Objects {
for _, f := range q.Filters {
objects = f(objects)
}
return objects
}
var ObjectSortFilter = func(objs Objects) Objects {
sort.Stable(objs)
return objs
}