forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collation.go
56 lines (51 loc) · 1.72 KB
/
collation.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
// 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 option
import "github.com/mongodb/mongo-go-driver/bson"
// Collation allows users to specify language-specific rules for string comparison, such as
// rules for lettercase and accent marks.
type Collation struct {
Locale string `bson:",omitempty"`
CaseLevel bool `bson:",omitempty"`
CaseFirst string `bson:",omitempty"`
Strength int `bson:",omitempty"`
NumericOrdering bool `bson:",omitempty"`
Alternate string `bson:",omitempty"`
MaxVariable string `bson:",omitempty"`
Backwards bool `bson:",omitempty"`
}
func (co *Collation) toDocument() *bson.Document {
doc := bson.NewDocument()
if co.Locale != "" {
doc.Append(bson.EC.String("locale", co.Locale))
}
if co.CaseLevel {
doc.Append(bson.EC.Boolean("caseLevel", true))
}
if co.CaseFirst != "" {
doc.Append(bson.EC.String("caseFirst", co.CaseFirst))
}
if co.Strength != 0 {
doc.Append(bson.EC.Int32("strength", int32(co.Strength)))
}
if co.NumericOrdering {
doc.Append(bson.EC.Boolean("numericOrdering", true))
}
if co.Alternate != "" {
doc.Append(bson.EC.String("alternate", co.Alternate))
}
if co.MaxVariable != "" {
doc.Append(bson.EC.String("maxVariable", co.MaxVariable))
}
if co.Backwards {
doc.Append(bson.EC.Boolean("backwards", true))
}
return doc
}
// MarshalBSONDocument implements the bson.DocumentMarshaler interface.
func (co *Collation) MarshalBSONDocument() (*bson.Document, error) {
return co.toDocument(), nil
}