samber / mo

🦄 Monads and popular FP abstractions, powered by Go 1.18+ Generics (Option, Result, Either...)
https://pkg.go.dev/github.com/samber/mo
MIT License
2.47k stars 80 forks source link

Decoding BSON values #20

Open davidbayo10 opened 1 year ago

davidbayo10 commented 1 year ago

Hello,

I would like to know if decoding bson values from MongoDB could be a feature of this amazing lib.

type User struct {
    Name  mo.Option[string] `bson:"name"`
}

error decoding key name: cannot decode string into a mo.Option[string]

I would to suggest to decode an empty string or none present key to None.

Thank you in advance.

samber commented 1 year ago

Hi @davidbayo10

Sure. I suppose implementing BSON marshalers must be simple. Feel free to propose a PR.

I just checked the bson library from MongoDB driver. It seems the provide a codec registry: https://pkg.go.dev/go.mongodb.org/mongo-driver/bson/bsoncodec

I think we should create a sub-package under github.com/samber/mo/bson in order to not embed go.mongodb.org/mongo-driver into global go.mod and keep it lightweight. That package would later be imported using import _ "github.com/samber/mo/bson". WDYT ?