Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoding BSON values #20

Open
davidbayo10 opened this issue Mar 2, 2023 · 5 comments
Open

Decoding BSON values #20

davidbayo10 opened this issue Mar 2, 2023 · 5 comments

Comments

@davidbayo10
Copy link

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
Copy link
Owner

samber commented Mar 20, 2023

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 ?

@paulhobbel
Copy link

Any progression on this? If not I think I can work on an implementation since we need it as well

@samber
Copy link
Owner

samber commented Jan 28, 2025

please do!

@paulhobbel
Copy link

Okay so I don't fully understand how we could add support by doing this in a bson package. The v2 of the mongo-driver removed access to the global registry so you can't register encoders/decoders globally. This means one would have to create their own registry and then tell all the parts of the code where they marshal/unmarshal to use the custom registry.

How big of a problem would it be if we added the UnmarshalBSON/MarshalBSON to the main package?

@samber
Copy link
Owner

samber commented Feb 2, 2025

Yes, i think this is the way to go.

We already added some marshal/unmarshall methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@samber @davidbayo10 @paulhobbel and others