forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds-selector.go
78 lines (68 loc) · 2.39 KB
/
ds-selector.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
package jobs
import (
"context"
"github.com/golang/protobuf/ptypes"
"github.com/micro/go-micro/client"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/proto/object"
service "github.com/pydio/cells/common/service/proto"
)
func (m *DataSourceSelector) Filter(ctx context.Context, input ActionMessage) (ActionMessage, *ActionMessage, bool) {
var passed, excluded []*object.DataSource
for _, ds := range input.DataSources {
if m.All || m.evaluate(ctx, m.Query, input, ds) {
passed = append(passed, ds)
} else {
excluded = append(passed, ds)
}
}
input.DataSources = passed
var x *ActionMessage
if len(excluded) > 0 {
filteredOutput := input
filteredOutput.DataSources = excluded
x = &filteredOutput
}
return input, x, len(passed) > 0
}
func (m *DataSourceSelector) Select(cl client.Client, ctx context.Context, input ActionMessage, objects chan interface{}, done chan bool) error {
defer func() {
done <- true
}()
for _, ds := range m.loadDSS() {
if m.All || m.evaluate(ctx, m.Query, input, ds) {
objects <- ds
}
}
return nil
}
func (m *DataSourceSelector) MultipleSelection() bool {
return m.Collect
}
func (m *DataSourceSelector) loadDSS() (sources []*object.DataSource) {
for _, ds := range config.ListSourcesFromConfig() {
sources = append(sources, ds)
}
return
}
func (m *DataSourceSelector) evaluate(ctx context.Context, query *service.Query, input ActionMessage, dsObject *object.DataSource) bool {
var bb []bool
for _, q := range query.SubQueries {
msg := &object.DataSourceSingleQuery{}
subQ := &service.Query{}
if e := ptypes.UnmarshalAny(q, msg); e == nil {
// Evaluate fields
msg.Name = EvaluateFieldStr(ctx, input, msg.Name)
msg.ObjectServiceName = EvaluateFieldStr(ctx, input, msg.ObjectServiceName)
msg.StorageConfigurationName = EvaluateFieldStr(ctx, input, msg.StorageConfigurationName)
msg.StorageConfigurationValue = EvaluateFieldStr(ctx, input, msg.StorageConfigurationValue)
msg.PeerAddress = EvaluateFieldStr(ctx, input, msg.PeerAddress)
msg.EncryptionKey = EvaluateFieldStr(ctx, input, msg.EncryptionKey)
msg.VersioningPolicyName = EvaluateFieldStr(ctx, input, msg.VersioningPolicyName)
bb = append(bb, msg.Matches(dsObject))
} else if e := ptypes.UnmarshalAny(q, subQ); e == nil {
bb = append(bb, m.evaluate(ctx, subQ, input, dsObject))
}
}
return service.ReduceQueryBooleans(bb, query.Operation)
}