Created
November 19, 2024 20:27
-
-
Save prestonvasquez/bfc16d10b1f13ca3df61e819bcce2f8d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package repository | |
import ( | |
"fmt" | |
"reflect" | |
"github.com/google/uuid" | |
"go.mongodb.org/mongo-driver/v2/bson" | |
) | |
var ( | |
tUUID = reflect.TypeOf(uuid.UUID{}) | |
uuidSubtype = byte(0x04) | |
mongoRegistry = bson.NewRegistry() | |
) | |
func init() { | |
mongoRegistry.RegisterTypeEncoder(tUUID, bson.ValueEncoderFunc(uuidEncodeValue)) | |
mongoRegistry.RegisterTypeDecoder(tUUID, bson.ValueDecoderFunc(uuidDecodeValue)) | |
} | |
func uuidEncodeValue(ec bson.EncodeContext, vw bson.ValueWriter, val reflect.Value) error { | |
if !val.IsValid() || val.Type() != tUUID { | |
return bson.ValueEncoderError{Name: "uuidEncodeValue", Types: []reflect.Type{tUUID}, Received: val} | |
} | |
b := val.Interface().(uuid.UUID) | |
return vw.WriteBinaryWithSubtype(b[:], uuidSubtype) | |
} | |
func uuidDecodeValue(dc bson.DecodeContext, vr bson.ValueReader, val reflect.Value) error { | |
if !val.CanSet() || val.Type() != tUUID { | |
return bson.ValueDecoderError{Name: "uuidDecodeValue", Types: []reflect.Type{tUUID}, Received: val} | |
} | |
var data []byte | |
var subtype byte | |
var err error | |
switch vrType := vr.Type(); vrType { | |
case bson.TypeBinary: | |
data, subtype, err = vr.ReadBinary() | |
if subtype != uuidSubtype { | |
return fmt.Errorf("unsupported binary subtype %v for UUID", subtype) | |
} | |
case bson.TypeNull: | |
err = vr.ReadNull() | |
case bson.TypeUndefined: | |
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment