-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyticsquery_options.go
83 lines (67 loc) · 2.17 KB
/
analyticsquery_options.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
package gocb
import (
"strings"
"time"
"github.com/google/uuid"
)
// AnalyticsScanConsistency indicates the level of data consistency desired for an analytics query.
type AnalyticsScanConsistency uint
const (
// AnalyticsScanConsistencyNotBounded indicates no data consistency is required.
AnalyticsScanConsistencyNotBounded = AnalyticsScanConsistency(1)
// AnalyticsScanConsistencyRequestPlus indicates that request-level data consistency is required.
AnalyticsScanConsistencyRequestPlus = AnalyticsScanConsistency(2)
)
// AnalyticsOptions is the set of options available to an Analytics query.
type AnalyticsOptions struct {
ClientContextID string
Priority bool
PositionalParameters []interface{}
NamedParameters map[string]interface{}
Readonly bool
ScanConsistency AnalyticsScanConsistency
Raw map[string]interface{}
Timeout time.Duration
RetryStrategy RetryStrategy
parentSpan requestSpanContext
}
func (opts *AnalyticsOptions) toMap() (map[string]interface{}, error) {
execOpts := make(map[string]interface{})
if opts.ClientContextID == "" {
execOpts["client_context_id"] = uuid.New().String()
} else {
execOpts["client_context_id"] = opts.ClientContextID
}
if opts.ScanConsistency != 0 {
if opts.ScanConsistency == AnalyticsScanConsistencyNotBounded {
execOpts["scan_consistency"] = "not_bounded"
} else if opts.ScanConsistency == AnalyticsScanConsistencyRequestPlus {
execOpts["scan_consistency"] = "request_plus"
} else {
return nil, makeInvalidArgumentsError("unexpected consistency option")
}
}
if opts.PositionalParameters != nil && opts.NamedParameters != nil {
return nil, makeInvalidArgumentsError("positional and named parameters must be used exclusively")
}
if opts.PositionalParameters != nil {
execOpts["args"] = opts.PositionalParameters
}
if opts.NamedParameters != nil {
for key, value := range opts.NamedParameters {
if !strings.HasPrefix(key, "$") {
key = "$" + key
}
execOpts[key] = value
}
}
if opts.Readonly {
execOpts["readonly"] = true
}
if opts.Raw != nil {
for k, v := range opts.Raw {
execOpts[k] = v
}
}
return execOpts, nil
}