-
Notifications
You must be signed in to change notification settings - Fork 17
/
types.go
41 lines (33 loc) · 918 Bytes
/
types.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
package mongo
import (
"errors"
"strconv"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
type Uint64 uint64
func (u Uint64) MarshalBSONValue() (bsontype.Type, []byte, error) {
ui64Str := strconv.FormatUint(uint64(u), 10)
d128, err := primitive.ParseDecimal128(ui64Str)
return bsontype.Decimal128, bsoncore.AppendDecimal128(nil, d128), err
}
func (u *Uint64) UnmarshalBSONValue(t bsontype.Type, b []byte) error {
d128, _, ok := bsoncore.ReadDecimal128(b)
if !ok {
return errors.New("Uint64 UnmarshalBSONValue error")
}
ui64, err := strconv.ParseUint(d128.String(), 10, 64)
if err != nil {
return err
}
*u = Uint64(ui64)
return nil
}
// String inplements Stringer interface for type Uint64.
func (u *Uint64) String() string {
if u == nil {
return ""
}
return strconv.FormatUint(uint64(*u), 10)
}