/
uuid.go
76 lines (67 loc) · 1.97 KB
/
uuid.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
66
67
68
69
70
71
72
73
74
75
76
package bson
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"
)
// Update the default BSON registry to be able to handle UUID types as strings.
func init() {
rb := bson.NewRegistryBuilder()
var id uuid.UUID
uuidType := reflect.TypeOf(id)
rb.RegisterTypeEncoder(uuidType, bsoncodec.ValueEncoderFunc(
func(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
if !val.IsValid() || val.Type() != uuidType || val.Len() != 16 {
return bsoncodec.ValueEncoderError{
Name: "uuid.UUID",
Types: []reflect.Type{uuidType},
Received: val,
}
}
b := make([]byte, 16)
v := reflect.ValueOf(b)
reflect.Copy(v, val)
id, err := uuid.FromBytes(v.Bytes())
if err != nil {
return fmt.Errorf("could not parse UUID bytes (%x): %w", v.Bytes(), err)
}
return vw.WriteString(id.String())
},
))
rb.RegisterTypeDecoder(uuidType, bsoncodec.ValueDecoderFunc(
func(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.IsValid() || !val.CanSet() || val.Kind() != reflect.Array {
return bsoncodec.ValueDecoderError{
Name: "uuid.UUID",
Kinds: []reflect.Kind{reflect.Bool},
Received: val,
}
}
var s string
switch vr.Type() {
case bsontype.String:
var err error
if s, err = vr.ReadString(); err != nil {
return err
}
default:
return fmt.Errorf("received invalid BSON type to decode into UUID: %s", vr.Type())
}
id, err := uuid.Parse(s)
if err != nil {
return fmt.Errorf("could not parse UUID string: %s", s)
}
v := reflect.ValueOf(id)
if !v.IsValid() || v.Kind() != reflect.Array {
return fmt.Errorf("invalid kind of reflected UUID value: %s", v.Kind().String())
}
reflect.Copy(val, v)
return nil
},
))
bson.DefaultRegistry = rb.Build()
}