Skip to content

Instantly share code, notes, and snippets.

@SupaHam
Created November 6, 2020 23:17
Show Gist options
  • Select an option

  • Save SupaHam/3afe982dc75039356723600ccc91ff77 to your computer and use it in GitHub Desktop.

Select an option

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
}
@76creates
Copy link

works amazing, thanks, just in case someone needs this:

mongoOptions := options.Client().ApplyURI(mongoURI).SetRegistry(mongoRegistry)
client, err := mongo.Connect(context.TODO(), mongoOptions)

@IamTyrone
Copy link

I'm pretty new to go lang and I am trying to create a microservice with a mongo database. I found this code linked from StackOverflow. I can see the code is then used to decode the "BSON Binary of subtype 0x00".

My question, dumb as it may be as a newbie to golang, is what data type do I put in my type definition in my model struct? Do I continue with the uuid.UUID from github.com/google/uuid?

@SupaHam
Copy link
Author

SupaHam commented Dec 19, 2022

I'm pretty new to go lang and I am trying to create a microservice with a mongo database. I found this code linked from StackOverflow. I can see the code is then used to decode the "BSON Binary of subtype 0x00".

My question, dumb as it may be as a newbie to golang, is what data type do I put in my type definition in my model struct? Do I continue with the uuid.UUID from github.com/google/uuid?

Hi Tyrone, when using your mongo client, please make sure you are using the mongo client after you call SetRegistry() on it. You can see the comment I left at the top of the file clarifying this.

EDIT: Usually when you see subtype 0x00, it means you've supplied a byte slice (or maybe a UUID type that is represented by a byte slice) that is not recognised by mongo as any particular subtype so it defaults to 0.

@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)
}

@fidalcastro
Copy link

Copied and replaced deprecated functions, it works like charm!! Hats off

@alexykot
Copy link

alexykot commented Nov 7, 2024

@SupaHam Would you kindly add an explicit license to this code snippet? It's great and really useful, and I see the // Use as you please comment, thanks for that :D

But I'm just concerned that for commercial development I'm doing that comment may not be enough to say that this is clean and permitted to include. A reference to BSD or MIT license would be enough. Something like:

// Available under MIT license, see details at https://opensource.org/license/MIT

@adadgoff
Copy link

adadgoff commented Mar 9, 2026

Big thanks, @SupaHam !

If anyone is interested in the MongoDB v2 implementation, here it is. =]

package mymongo

import (
	"context"
	"fmt"
	"reflect"

	"github.com/google/uuid"
	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.mongodb.org/mongo-driver/v2/mongo/options"
)

var uuidType = reflect.TypeFor[uuid.UUID]()

func NewMongoDBClient(
	ctx context.Context,
	host string,
	port uint,
	username string,
	password string,
) (*mongo.Client, error) {
	uri := fmt.Sprintf(
		"mongodb://%s:%d",
		host,
		port,
	)

	registry := bson.NewRegistry()
	registry.RegisterTypeEncoder(
		uuidType,
		bson.ValueEncoderFunc(uuidEncoder),
	)
	registry.RegisterTypeDecoder(
		uuidType,
		bson.ValueDecoderFunc(uuidDecoder),
	)

	client, err := mongo.Connect(
		options.Client().ApplyURI(
			uri,
		).SetAuth(
			options.Credential{
				Username: username,
				Password: password,
			},
		).SetRegistry(
			registry,
		),
	)

	if err != nil {
		return nil, err
	}
	if err := client.Ping(
		ctx,
		nil,
	); err != nil {
		return nil, err
	}

	return client, nil
}

func uuidEncoder(
	_ bson.EncodeContext,
	vw bson.ValueWriter,
	val reflect.Value,
) error {
	if !val.IsValid() || val.Type() != uuidType {
		return bson.ValueEncoderError{
			Name:     "uuidEncoder",
			Types:    []reflect.Type{uuidType},
			Received: val,
		}
	}

	bytes := val.Interface().(uuid.UUID)

	return vw.WriteBinaryWithSubtype(
		bytes[:],
		bson.TypeBinaryUUID,
	)
}

func uuidDecoder(
	_ bson.DecodeContext,
	vr bson.ValueReader,
	val reflect.Value,
) error {
	if !val.IsValid() || !val.CanSet() || val.Type() != uuidType {
		return bson.ValueDecoderError{
			Name:     "uuidDecoder",
			Types:    []reflect.Type{uuidType},
			Received: val,
		}
	}

	switch vrType := vr.Type(); vrType {
	case bson.TypeBinary:
		bytes, bytesType, err := vr.ReadBinary()
		if err != nil {
			return err
		}
		if bytesType != bson.TypeBinaryUUID {
			return fmt.Errorf(
				"Unsupported binary subtype %v for UUID",
				bytesType,
			)
		}

		uuid_, err := uuid.FromBytes(bytes)
		if err != nil {
			return err
		}

		val.Set(reflect.ValueOf(uuid_))
		return nil

	case bson.TypeNull:
		return vr.ReadNull()

	case bson.TypeUndefined:
		return vr.ReadUndefined()

	default:
		return fmt.Errorf(
			"Can not decode %v into UUID.",
			vrType,
		)
	}
}

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