/
registry.go
65 lines (57 loc) · 1.81 KB
/
registry.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
57
58
59
60
61
62
63
64
65
// BSON registry to encode/decode uuid.UUID to be saved/read in/from mongo
// source: https://gist.github.com/SupaHam/3afe982dc75039356723600ccc91ff77
package mongo
import (
"fmt"
"reflect"
"github.com/google/uuid"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsonrw"
"go.mongodb.org/mongo-driver/bson/bsontype"
)
var (
tUUID = reflect.TypeOf(uuid.UUID{})
uuidSubtype = byte(0x04)
mongoRegistry = bson.NewRegistryBuilder().
RegisterTypeEncoder(tUUID, bsoncodec.ValueEncoderFunc(uuidEncodeValue)).
RegisterTypeDecoder(tUUID, bsoncodec.ValueDecoderFunc(uuidDecodeValue)).
Build()
)
func uuidEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
if !val.IsValid() || val.Type() != tUUID {
return bsoncodec.ValueEncoderError{Name: "uuidEncodeValue", Types: []reflect.Type{tUUID}, Received: val}
}
b := val.Interface().(uuid.UUID)
return vw.WriteBinaryWithSubtype(b[:], uuidSubtype)
}
func uuidDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != tUUID {
return bsoncodec.ValueDecoderError{Name: "uuidDecodeValue", Types: []reflect.Type{tUUID}, Received: val}
}
var data []byte
var subtype byte
var err error
switch vrType := vr.Type(); vrType {
case bsontype.Binary:
data, subtype, err = vr.ReadBinary()
if subtype != uuidSubtype {
return fmt.Errorf("unsupported binary subtype %v for UUID", subtype)
}
case bsontype.Null:
err = vr.ReadNull()
case bsontype.Undefined:
err = vr.ReadUndefined()
default:
return fmt.Errorf("cannot decode %v into a UUID", vrType)
}
if err != nil {
return err
}
uuid2, err := uuid.FromBytes(data)
if err != nil {
return err
}
val.Set(reflect.ValueOf(uuid2))
return nil
}