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

Please support UnmarshalBSON and MarshalBSON serialization #168

Open
myxtype opened this issue Mar 16, 2020 · 5 comments
Open

Please support UnmarshalBSON and MarshalBSON serialization #168

myxtype opened this issue Mar 16, 2020 · 5 comments

Comments

@myxtype
Copy link

myxtype commented Mar 16, 2020

Useful when using mongodb databases

@myxtype
Copy link
Author

myxtype commented Mar 16, 2020

// Unmarshaler is an interface implemented by types that can unmarshal a BSON
// document representation of themselves. The BSON bytes can be assumed to be
// valid. UnmarshalBSON must copy the BSON bytes if it wishes to retain the data
// after returning.
type Unmarshaler interface {
	UnmarshalBSON([]byte) error
}
// Marshaler is an interface implemented by types that can marshal themselves
// into a BSON document represented as bytes. The bytes returned must be a valid
// BSON document if the error is nil.
type Marshaler interface {
	MarshalBSON() ([]byte, error)
}

@mwoss
Copy link
Member

mwoss commented Mar 17, 2020

Hi @myxtype. Thanks for pointing that. The corresponding PR that already implement the mentioned issue was created some time ago #92, but we haven't decided to merge it to the master branch as it introduces external dependence - we aim for dependency-free lib. We track this issue in our issue board and we will try to comes with a solution in v.1.3 release.

@epelc
Copy link

epelc commented Aug 5, 2020

You can work around this by registering a codec for encoding/decoding decimals as strings or some other format(mongodb's decimal128?) without adding a dependency or explicit support to this lib.

https://pkg.go.dev/go.mongodb.org/mongo-driver/bson?tab=doc#MarshalWithRegistry
https://pkg.go.dev/go.mongodb.org/[email protected]/bson/bsoncodec?tab=doc#Registry

You can even add it to the default registry if it is safe for your project.

NOTE this requires the new official mongodb driver. I don't think mgo or the globalsign fork have any option for registering types/codecs.

@mwoss
Copy link
Member

mwoss commented Aug 5, 2020

This can might be a valid workaround if we aiming for a dependency-free library.
Thanks a lot for the idea @epelc. :D

@waghcwb
Copy link

waghcwb commented Feb 15, 2021

My temporary (and dirty) solution was to create another type extending the default, and update the value when insert in db

type Product struct {
	Price            decimal.Decimal  `bson:"price" json:"price"`
}

type MongoProduct struct {
	Product
	Price string `bson:"price" json:"price"`
}

func CreateProduct(p Product) {
	var product = MongoProduct{}
	product.Price = p.Price.String()
	// insert in db
}

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