Skip to content

Commit fa07c0b

Browse files
author
Divjot Arora
committed
Add Normalization to options.Collation
GODRIVER-758 Change-Id: I9db9f5956a97646c9d45ced7cf8abdeb88f8393c
1 parent 422ba92 commit fa07c0b

File tree

4 files changed

+55
-49
lines changed

4 files changed

+55
-49
lines changed

mongo/crud_spec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func aggregateTest(t *testing.T, db *Database, coll *Collection, test *testCase)
198198
}
199199

200200
if collation, found := test.Operation.Arguments["collation"]; found {
201-
opts = opts.SetCollation(newCollationFromMap(collation.(map[string]interface{})))
201+
opts = opts.SetCollation(collationFromMap(collation.(map[string]interface{})))
202202
}
203203

204204
out := false

mongo/crud_util_test.go

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func executeCount(sess *sessionImpl, coll *Collection, args map[string]interface
7272
case "limit":
7373
opts = opts.SetLimit(int64(opt.(float64)))
7474
case "collation":
75-
opts = opts.SetCollation(newCollationFromMap(opt.(map[string]interface{})))
75+
opts = opts.SetCollation(collationFromMap(opt.(map[string]interface{})))
7676
}
7777
}
7878

@@ -98,7 +98,7 @@ func executeDistinct(sess *sessionImpl, coll *Collection, args map[string]interf
9898
case "fieldName":
9999
fieldName = opt.(string)
100100
case "collation":
101-
opts = opts.SetCollation(newCollationFromMap(opt.(map[string]interface{})))
101+
opts = opts.SetCollation(collationFromMap(opt.(map[string]interface{})))
102102
}
103103
}
104104

@@ -317,7 +317,7 @@ func executeDeleteOne(sess *sessionImpl, coll *Collection, args map[string]inter
317317
case "filter":
318318
filter = opt.(map[string]interface{})
319319
case "collation":
320-
opts = opts.SetCollation(newCollationFromMap(opt.(map[string]interface{})))
320+
opts = opts.SetCollation(collationFromMap(opt.(map[string]interface{})))
321321
}
322322
}
323323

@@ -346,7 +346,7 @@ func executeDeleteMany(sess *sessionImpl, coll *Collection, args map[string]inte
346346
case "filter":
347347
filter = opt.(map[string]interface{})
348348
case "collation":
349-
opts = opts.SetCollation(newCollationFromMap(opt.(map[string]interface{})))
349+
opts = opts.SetCollation(collationFromMap(opt.(map[string]interface{})))
350350
}
351351
}
352352

@@ -380,7 +380,7 @@ func executeReplaceOne(sess *sessionImpl, coll *Collection, args map[string]inte
380380
case "upsert":
381381
opts = opts.SetUpsert(opt.(bool))
382382
case "collation":
383-
opts = opts.SetCollation(newCollationFromMap(opt.(map[string]interface{})))
383+
opts = opts.SetCollation(collationFromMap(opt.(map[string]interface{})))
384384
}
385385
}
386386

@@ -423,7 +423,7 @@ func executeUpdateOne(sess *sessionImpl, coll *Collection, args map[string]inter
423423
case "upsert":
424424
opts = opts.SetUpsert(opt.(bool))
425425
case "collation":
426-
opts = opts.SetCollation(newCollationFromMap(opt.(map[string]interface{})))
426+
opts = opts.SetCollation(collationFromMap(opt.(map[string]interface{})))
427427
}
428428
}
429429

@@ -463,7 +463,7 @@ func executeUpdateMany(sess *sessionImpl, coll *Collection, args map[string]inte
463463
case "upsert":
464464
opts = opts.SetUpsert(opt.(bool))
465465
case "collation":
466-
opts = opts.SetCollation(newCollationFromMap(opt.(map[string]interface{})))
466+
opts = opts.SetCollation(collationFromMap(opt.(map[string]interface{})))
467467
}
468468
}
469469

@@ -498,7 +498,7 @@ func executeAggregate(sess *sessionImpl, coll *Collection, args map[string]inter
498498
case "batchSize":
499499
opts = opts.SetBatchSize(int32(opt.(float64)))
500500
case "collation":
501-
opts = opts.SetCollation(newCollationFromMap(opt.(map[string]interface{})))
501+
opts = opts.SetCollation(collationFromMap(opt.(map[string]interface{})))
502502
}
503503
}
504504

@@ -824,46 +824,8 @@ func collationFromMap(m map[string]interface{}) *options.Collation {
824824
collation.MaxVariable = maxVariable.(string)
825825
}
826826

827-
if backwards, found := m["backwards"]; found {
828-
collation.Backwards = backwards.(bool)
829-
}
830-
831-
return &collation
832-
}
833-
834-
// Matt: bad name, I know; also, type-aliasing, I know.
835-
// The issue is, options.Collation has a ToDocument function and mongoopt.Collation has a convert function
836-
// type aliasing doesn't allow defining new functions on non-local types.
837-
// When all options are updated to the improved api, this function will be the only one and will simply be named "collationFromMap"
838-
func newCollationFromMap(m map[string]interface{}) *options.Collation {
839-
var collation options.Collation
840-
841-
if locale, found := m["locale"]; found {
842-
collation.Locale = locale.(string)
843-
}
844-
845-
if caseLevel, found := m["caseLevel"]; found {
846-
collation.CaseLevel = caseLevel.(bool)
847-
}
848-
849-
if caseFirst, found := m["caseFirst"]; found {
850-
collation.CaseFirst = caseFirst.(string)
851-
}
852-
853-
if strength, found := m["strength"]; found {
854-
collation.Strength = int(strength.(float64))
855-
}
856-
857-
if numericOrdering, found := m["numericOrdering"]; found {
858-
collation.NumericOrdering = numericOrdering.(bool)
859-
}
860-
861-
if alternate, found := m["alternate"]; found {
862-
collation.Alternate = alternate.(string)
863-
}
864-
865-
if maxVariable, found := m["maxVariable"]; found {
866-
collation.MaxVariable = maxVariable.(string)
827+
if normalization, found := m["normalization"]; found {
828+
collation.Normalization = normalization.(bool)
867829
}
868830

869831
if backwards, found := m["backwards"]; found {

mongo/options/collation_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package options
2+
3+
import (
4+
"github.com/mongodb/mongo-go-driver/x/bsonx"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func TestCollation(t *testing.T) {
10+
t.Run("TestCollationToDocument", func(t *testing.T) {
11+
c := &Collation{
12+
Locale: "locale",
13+
CaseLevel: true,
14+
CaseFirst: "first",
15+
Strength: 1,
16+
NumericOrdering: true,
17+
Alternate: "alternate",
18+
MaxVariable: "maxVariable",
19+
Normalization: true,
20+
Backwards: true,
21+
}
22+
23+
doc := c.ToDocument()
24+
expected := bsonx.Doc{
25+
{"locale", bsonx.String("locale")},
26+
{"caseLevel", bsonx.Boolean(true)},
27+
{"caseFirst", bsonx.String("first")},
28+
{"strength", bsonx.Int32(1)},
29+
{"numericOrdering", bsonx.Boolean(true)},
30+
{"alternate", bsonx.String("alternate")},
31+
{"maxVariable", bsonx.String("maxVariable")},
32+
{"normalization", bsonx.Boolean(true)},
33+
{"backwards", bsonx.Boolean(true)},
34+
}
35+
36+
if !reflect.DeepEqual(doc, expected) {
37+
t.Fatalf("collation did not match expected. got %v; wanted %v", doc, expected)
38+
}
39+
})
40+
}

mongo/options/mongooptions.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Collation struct {
2525
NumericOrdering bool `bson:",omitempty"` // Whether to order numbers based on numerical order and not collation order
2626
Alternate string `bson:",omitempty"` // Whether spaces and punctuation are considered base characters
2727
MaxVariable string `bson:",omitempty"` // Which characters are affected by alternate: "shifted"
28+
Normalization bool `bson:",omitempty"` // Causes text to be normalized into Unicode NFD
2829
Backwards bool `bson:",omitempty"` // Causes secondary differences to be considered in reverse order, as it is done in the French language
2930
}
3031

@@ -52,6 +53,9 @@ func (co *Collation) ToDocument() bsonx.Doc {
5253
if co.MaxVariable != "" {
5354
doc = append(doc, bsonx.Elem{"maxVariable", bsonx.String(co.MaxVariable)})
5455
}
56+
if co.Normalization {
57+
doc = append(doc, bsonx.Elem{"normalization", bsonx.Boolean(co.Normalization)})
58+
}
5559
if co.Backwards {
5660
doc = append(doc, bsonx.Elem{"backwards", bsonx.Boolean(true)})
5761
}

0 commit comments

Comments
 (0)