@@ -116,10 +116,8 @@ func (ce *ClientEncryption) CreateDataKey(ctx context.Context, kmsProvider strin
116
116
return primitive.Binary {Subtype : subtype , Data : data }, nil
117
117
}
118
118
119
- // Encrypt encrypts a BSON value with the given key and algorithm. Returns an encrypted value (BSON binary of subtype 6).
120
- func (ce * ClientEncryption ) Encrypt (ctx context.Context , val bson.RawValue ,
121
- opts ... * options.EncryptOptions ) (primitive.Binary , error ) {
122
-
119
+ // transformExplicitEncryptionOptions creates explicit encryption options to be passed to libmongocrypt.
120
+ func transformExplicitEncryptionOptions (opts ... * options.EncryptOptions ) * mcopts.ExplicitEncryptionOptions {
123
121
eo := options .MergeEncryptOptions (opts ... )
124
122
transformed := mcopts .ExplicitEncryption ()
125
123
if eo .KeyID != nil {
@@ -135,13 +133,69 @@ func (ce *ClientEncryption) Encrypt(ctx context.Context, val bson.RawValue,
135
133
transformed .SetContentionFactor (* eo .ContentionFactor )
136
134
}
137
135
136
+ if eo .RangeOptions != nil {
137
+ var transformedRange mcopts.ExplicitRangeOptions
138
+ if eo .RangeOptions .Min != nil {
139
+ transformedRange .Min = & bsoncore.Value {Type : eo .RangeOptions .Min .Type , Data : eo .RangeOptions .Min .Value }
140
+ }
141
+ if eo .RangeOptions .Max != nil {
142
+ transformedRange .Max = & bsoncore.Value {Type : eo .RangeOptions .Max .Type , Data : eo .RangeOptions .Max .Value }
143
+ }
144
+ if eo .RangeOptions .Precision != nil {
145
+ transformedRange .Precision = eo .RangeOptions .Precision
146
+ }
147
+ transformedRange .Sparsity = eo .RangeOptions .Sparsity
148
+ transformed .SetRangeOptions (transformedRange )
149
+ }
150
+ return transformed
151
+ }
152
+
153
+ // Encrypt encrypts a BSON value with the given key and algorithm. Returns an encrypted value (BSON binary of subtype 6).
154
+ func (ce * ClientEncryption ) Encrypt (ctx context.Context , val bson.RawValue ,
155
+ opts ... * options.EncryptOptions ) (primitive.Binary , error ) {
156
+
157
+ transformed := transformExplicitEncryptionOptions (opts ... )
138
158
subtype , data , err := ce .crypt .EncryptExplicit (ctx , bsoncore.Value {Type : val .Type , Data : val .Value }, transformed )
139
159
if err != nil {
140
160
return primitive.Binary {}, err
141
161
}
142
162
return primitive.Binary {Subtype : subtype , Data : data }, nil
143
163
}
144
164
165
+ // EncryptExpression encrypts an expression to query a range index.
166
+ // On success, `result` is populated with the resulting BSON document.
167
+ // `expr` is expected to be a BSON document of one of the following forms:
168
+ // 1. A Match Expression of this form:
169
+ // {$and: [{<field>: {$gt: <value1>}}, {<field>: {$lt: <value2> }}]}
170
+ // 2. An Aggregate Expression of this form:
171
+ // {$and: [{$gt: [<fieldpath>, <value1>]}, {$lt: [<fieldpath>, <value2>]}]
172
+ // $gt may also be $gte. $lt may also be $lte.
173
+ // Only supported for queryType "rangePreview"
174
+ // NOTE(kevinAlbs): The Range algorithm is experimental only. It is not intended for public use. It is subject to breaking changes.
175
+ func (ce * ClientEncryption ) EncryptExpression (ctx context.Context , expr interface {}, result interface {}, opts ... * options.EncryptOptions ) error {
176
+ transformed := transformExplicitEncryptionOptions (opts ... )
177
+
178
+ exprDoc , err := transformBsoncoreDocument (bson .DefaultRegistry , expr , true , "expr" )
179
+ if err != nil {
180
+ return err
181
+ }
182
+
183
+ encryptedExprDoc , err := ce .crypt .EncryptExplicitExpression (ctx , exprDoc , transformed )
184
+ if err != nil {
185
+ return err
186
+ }
187
+ if raw , ok := result .(* bson.Raw ); ok {
188
+ // Avoid the cost of Unmarshal.
189
+ * raw = bson .Raw (encryptedExprDoc )
190
+ return nil
191
+ }
192
+ err = bson .Unmarshal ([]byte (encryptedExprDoc ), result )
193
+ if err != nil {
194
+ return err
195
+ }
196
+ return nil
197
+ }
198
+
145
199
// Decrypt decrypts an encrypted value (BSON binary of subtype 6) and returns the original BSON value.
146
200
func (ce * ClientEncryption ) Decrypt (ctx context.Context , val primitive.Binary ) (bson.RawValue , error ) {
147
201
decrypted , err := ce .crypt .DecryptExplicit (ctx , val .Subtype , val .Data )
0 commit comments