forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
72 lines (62 loc) · 1.72 KB
/
registry.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
package stores
import (
"context"
"fmt"
"github.com/pydio/cells/common/etl/models"
"github.com/pydio/cells/common/proto/jobs"
)
// Options passes params and merge options when initializing stores
type Options struct {
Params map[string]string
MergeOptions *models.MergeOptions
ActionInput jobs.ActionMessage
Context context.Context
}
// CreateOptions initialize an empty Options object
func CreateOptions(ctx context.Context, param map[string]string, input jobs.ActionMessage) *Options {
m := &Options{
Context: ctx,
Params: param,
ActionInput: input,
MergeOptions: &models.MergeOptions{},
}
return m
}
// StoreLoader must return an initialized store
type StoreLoader func(options *Options) (interface{}, error)
var (
r map[string]StoreLoader
)
func init() {
r = make(map[string]StoreLoader)
}
// RegisterStore registers an available store
func RegisterStore(name string, store StoreLoader) {
r[name] = store
}
// LoadReadableStore finds an available readable store by name
func LoadReadableStore(name string, options *Options) (models.ReadableStore, error) {
if loader, ok := r[name]; ok {
i, e := loader(options)
if e != nil {
return nil, e
}
if readableStore, o := i.(models.ReadableStore); o {
return readableStore, nil
}
}
return nil, fmt.Errorf("cannot find readable store " + name)
}
// GetWritableStore finds a writable store by its name
func LoadWritableStore(name string, options *Options) (models.WritableStore, error) {
if loader, ok := r[name]; ok {
i, e := loader(options)
if e != nil {
return nil, e
}
if writableStore, o := i.(models.WritableStore); o {
return writableStore, nil
}
}
return nil, fmt.Errorf("cannot find writeable store " + name)
}