Skip to content

Instantly share code, notes, and snippets.

@SupaHam
Created November 6, 2020 23:17
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save SupaHam/3afe982dc75039356723600ccc91ff77 to your computer and use it in GitHub Desktop.
Save SupaHam/3afe982dc75039356723600ccc91ff77 to your computer and use it in GitHub Desktop.
mongo-go-driver UUID decoder & encoder for Golang
// This is a value (de|en)coder for the github.com/google/uuid UUID type. For best experience, register
// mongoRegistry to mongo client instance via options, e.g.
// clientOptions := options.Client().SetRegistry(mongoRegistry)
//
// Only BSON binary subtype 0x04 is supported.
//
// Use as you please
package repository
import (
"fmt"
"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"
"reflect"
)
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
}
@angelsalascalvo
Copy link

Thank you very much for the code!
Any ideas on how to make the typing of the type skip when uuid is equal to uuid.Nil ?

I have tried with this code but then the document is not written to the database

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)

        if b == uuid.Nil {
		return nil
	}

	return vw.WriteBinaryWithSubtype(b[:], uuidSubtype)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment