Skip to content

Commit a78cd34

Browse files
committed
Update IndexOptionsBuilder with correct options
GODRIVER-457 Change-Id: Iea7b371a31f0391c93deffaf8786f5ac2df4d015
1 parent 74a0e93 commit a78cd34

File tree

2 files changed

+82
-11
lines changed

2 files changed

+82
-11
lines changed

mongo/index_options_builder.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ func (iob *IndexOptionsBuilder) Background(background bool) *IndexOptionsBuilder
2020
return iob
2121
}
2222

23-
// ExpireAfter sets the expireAfter option
24-
func (iob *IndexOptionsBuilder) ExpireAfter(expireAfter int32) *IndexOptionsBuilder {
25-
iob.document.Append(bson.EC.Int32("expireAfter", expireAfter))
23+
// ExpireAfterSeconds sets the expireAfterSeconds option
24+
func (iob *IndexOptionsBuilder) ExpireAfterSeconds(expireAfterSeconds int32) *IndexOptionsBuilder {
25+
iob.document.Append(bson.EC.Int32("expireAfterSeconds", expireAfterSeconds))
2626
return iob
2727
}
2828

@@ -39,8 +39,8 @@ func (iob *IndexOptionsBuilder) Sparse(sparse bool) *IndexOptionsBuilder {
3939
}
4040

4141
// StorageEngine sets the storageEngine option
42-
func (iob *IndexOptionsBuilder) StorageEngine(storageEngine string) *IndexOptionsBuilder {
43-
iob.document.Append(bson.EC.String("storageEngine", storageEngine))
42+
func (iob *IndexOptionsBuilder) StorageEngine(storageEngine *bson.Document) *IndexOptionsBuilder {
43+
iob.document.Append(bson.EC.SubDocument("storageEngine", storageEngine))
4444
return iob
4545
}
4646

@@ -52,25 +52,25 @@ func (iob *IndexOptionsBuilder) Unique(unique bool) *IndexOptionsBuilder {
5252

5353
// Version sets the verison option
5454
func (iob *IndexOptionsBuilder) Version(version int32) *IndexOptionsBuilder {
55-
iob.document.Append(bson.EC.Int32("version", version))
55+
iob.document.Append(bson.EC.Int32("v", version))
5656
return iob
5757
}
5858

5959
// DefaultLanguage sets the defaultLanguage option
6060
func (iob *IndexOptionsBuilder) DefaultLanguage(defaultLanguage string) *IndexOptionsBuilder {
61-
iob.document.Append(bson.EC.String("defaultLanguage", defaultLanguage))
61+
iob.document.Append(bson.EC.String("default_language", defaultLanguage))
6262
return iob
6363
}
6464

6565
// LanguageOverride sets the languageOverride option
6666
func (iob *IndexOptionsBuilder) LanguageOverride(languageOverride string) *IndexOptionsBuilder {
67-
iob.document.Append(bson.EC.String("languageOverride", languageOverride))
67+
iob.document.Append(bson.EC.String("language_override", languageOverride))
6868
return iob
6969
}
7070

7171
// TextVersion sets the textVersion option
7272
func (iob *IndexOptionsBuilder) TextVersion(textVersion int32) *IndexOptionsBuilder {
73-
iob.document.Append(bson.EC.Int32("textVersion", textVersion))
73+
iob.document.Append(bson.EC.Int32("textIndexVersion", textVersion))
7474
return iob
7575
}
7676

@@ -82,7 +82,7 @@ func (iob *IndexOptionsBuilder) Weights(weights *bson.Document) *IndexOptionsBui
8282

8383
// SphereVersion sets the sphereVersion option
8484
func (iob *IndexOptionsBuilder) SphereVersion(sphereVersion int32) *IndexOptionsBuilder {
85-
iob.document.Append(bson.EC.Int32("sphereVersion", sphereVersion))
85+
iob.document.Append(bson.EC.Int32("2dsphereIndexVersion", sphereVersion))
8686
return iob
8787
}
8888

mongo/index_view_internal_test.go

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func TestIndexView_CreateOne(t *testing.T) {
129129
require.True(t, found)
130130
}
131131

132-
func TestIndexView_CreateOneWithIndexOptions(t *testing.T) {
132+
func TestIndexView_CreateOneWithNameOption(t *testing.T) {
133133
t.Parallel()
134134

135135
if testing.Short() {
@@ -174,6 +174,77 @@ func TestIndexView_CreateOneWithIndexOptions(t *testing.T) {
174174
require.True(t, found)
175175
}
176176

177+
// Omits collation option because it's incompatible with version option
178+
func TestIndexView_CreateOneWithAllOptions(t *testing.T) {
179+
t.Parallel()
180+
181+
if testing.Short() {
182+
t.Skip()
183+
}
184+
185+
_, coll := getIndexableCollection(t)
186+
indexView := coll.Indexes()
187+
188+
_, err := indexView.CreateOne(
189+
context.Background(),
190+
IndexModel{
191+
Keys: bson.NewDocument(
192+
bson.EC.String("foo", "text"),
193+
),
194+
Options: NewIndexOptionsBuilder().
195+
Background(false).
196+
ExpireAfterSeconds(10).
197+
Name("a").
198+
Sparse(false).
199+
Unique(false).
200+
Version(1).
201+
DefaultLanguage("english").
202+
LanguageOverride("english").
203+
TextVersion(1).
204+
Weights(bson.NewDocument()).
205+
SphereVersion(1).
206+
Bits(32).
207+
Max(10).
208+
Min(1).
209+
BucketSize(1).
210+
PartialFilterExpression(bson.NewDocument()).
211+
StorageEngine(bson.NewDocument(
212+
bson.EC.SubDocument("wiredTiger", bson.NewDocument(
213+
bson.EC.String("configString", "block_compressor=zlib"),
214+
)),
215+
)).
216+
Build(),
217+
},
218+
)
219+
require.NoError(t, err)
220+
}
221+
222+
func TestIndexView_CreateOneWithCollationOption(t *testing.T) {
223+
t.Parallel()
224+
225+
if testing.Short() {
226+
t.Skip()
227+
}
228+
229+
_, coll := getIndexableCollection(t)
230+
indexView := coll.Indexes()
231+
232+
_, err := indexView.CreateOne(
233+
context.Background(),
234+
IndexModel{
235+
Keys: bson.NewDocument(
236+
bson.EC.String("bar", "text"),
237+
),
238+
Options: NewIndexOptionsBuilder().
239+
Collation(bson.NewDocument(
240+
bson.EC.String("locale", "simple"),
241+
)).
242+
Build(),
243+
},
244+
)
245+
require.NoError(t, err)
246+
}
247+
177248
func TestIndexView_CreateMany(t *testing.T) {
178249
t.Parallel()
179250

0 commit comments

Comments
 (0)