decimal icon indicating copy to clipboard operation
decimal copied to clipboard

Please support UnmarshalBSON and MarshalBSON serialization

Open myxtype opened this issue 4 years ago • 5 comments

Useful when using mongodb databases

myxtype avatar Mar 16 '20 07:03 myxtype

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

myxtype avatar Mar 16 '20 07:03 myxtype

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.

mwoss avatar Mar 17 '20 00:03 mwoss

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.

epelc avatar Aug 05 '20 21:08 epelc

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

mwoss avatar Aug 05 '20 21:08 mwoss

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
}

waghcwb avatar Feb 15 '21 21:02 waghcwb