/
client_operation_execution.go
169 lines (151 loc) · 5.04 KB
/
client_operation_execution.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package unified
import (
"context"
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/internal/testutil/helpers"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
// This file contains helpers to execute client operations.
func executeCreateChangeStream(ctx context.Context, operation *operation) (*operationResult, error) {
var watcher interface {
Watch(context.Context, interface{}, ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error)
}
var err error
watcher, err = entities(ctx).client(operation.Object)
if err != nil {
watcher, err = entities(ctx).database(operation.Object)
}
if err != nil {
watcher, err = entities(ctx).collection(operation.Object)
}
if err != nil {
return nil, fmt.Errorf("no client, database, or collection entity found with ID %q", operation.Object)
}
var pipeline []interface{}
opts := options.ChangeStream()
elems, _ := operation.Arguments.Elements()
for _, elem := range elems {
key := elem.Key()
val := elem.Value()
switch key {
case "batchSize":
opts.SetBatchSize(val.Int32())
case "collation":
collation, err := createCollation(val.Document())
if err != nil {
return nil, fmt.Errorf("error creating collation: %v", err)
}
opts.SetCollation(*collation)
case "comment":
commentString, err := createCommentString(val)
if err != nil {
return nil, fmt.Errorf("error creating comment: %v", err)
}
opts.SetComment(commentString)
case "fullDocument":
switch fd := val.StringValue(); fd {
case "default":
opts.SetFullDocument(options.Default)
case "required":
opts.SetFullDocument(options.Required)
case "updateLookup":
opts.SetFullDocument(options.UpdateLookup)
case "whenAvailable":
opts.SetFullDocument(options.WhenAvailable)
default:
return nil, fmt.Errorf("unrecognized fullDocument value %q", fd)
}
case "fullDocumentBeforeChange":
switch fdbc := val.StringValue(); fdbc {
case "off":
opts.SetFullDocumentBeforeChange(options.Off)
case "required":
opts.SetFullDocumentBeforeChange(options.Required)
case "whenAvailable":
opts.SetFullDocumentBeforeChange(options.WhenAvailable)
}
case "maxAwaitTimeMS":
opts.SetMaxAwaitTime(time.Duration(val.Int32()) * time.Millisecond)
case "pipeline":
pipeline = helpers.RawToInterfaces(helpers.RawToDocuments(val.Array())...)
case "resumeAfter":
opts.SetResumeAfter(val.Document())
case "showExpandedEvents":
opts.SetShowExpandedEvents(val.Boolean())
case "startAfter":
opts.SetStartAfter(val.Document())
case "startAtOperationTime":
t, i := val.Timestamp()
opts.SetStartAtOperationTime(&primitive.Timestamp{T: t, I: i})
default:
return nil, fmt.Errorf("unrecognized createChangeStream option %q", key)
}
}
if pipeline == nil {
return nil, newMissingArgumentError("pipeline")
}
stream, err := watcher.Watch(ctx, pipeline, opts)
if err != nil {
return newErrorResult(err), nil
}
// createChangeStream is sometimes used with no corresponding saveResultAsEntity field. Return an
// empty result in this case.
if operation.ResultEntityID != nil {
if err := entities(ctx).addCursorEntity(*operation.ResultEntityID, stream); err != nil {
return nil, fmt.Errorf("error storing result as cursor entity: %v", err)
}
}
return newEmptyResult(), nil
}
func executeListDatabases(ctx context.Context, operation *operation, nameOnly bool) (*operationResult, error) {
client, err := entities(ctx).client(operation.Object)
if err != nil {
return nil, err
}
// We set a default filter rather than erroring if the Arguments doc doesn't have a "filter" field because the
// spec says drivers should support this field, not must.
filter := emptyDocument
opts := options.ListDatabases().SetNameOnly(nameOnly)
elems, _ := operation.Arguments.Elements()
for _, elem := range elems {
key := elem.Key()
val := elem.Value()
switch key {
case "authorizedDatabases":
opts.SetAuthorizedDatabases(val.Boolean())
case "filter":
filter = val.Document()
case "nameOnly":
opts.SetNameOnly(val.Boolean())
default:
return nil, fmt.Errorf("unrecognized listDatabases option %q", key)
}
}
res, err := client.ListDatabases(ctx, filter, opts)
if err != nil {
return newErrorResult(err), nil
}
specsArray := bsoncore.NewArrayBuilder()
for _, spec := range res.Databases {
rawSpec := bsoncore.NewDocumentBuilder().
AppendString("name", spec.Name).
AppendInt64("sizeOnDisk", spec.SizeOnDisk).
AppendBoolean("empty", spec.Empty).
Build()
specsArray.AppendDocument(rawSpec)
}
raw := bsoncore.NewDocumentBuilder().
AppendArray("databases", specsArray.Build()).
AppendInt64("totalSize", res.TotalSize).
Build()
return newDocumentResult(raw, nil), nil
}