-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
With MongoDB, how to convert between types of decimal128
and decimal.Decimal
?
#3734
Comments
Mongo Options 的原始碼在這裡:https://github.com/zeromicro/go-zero/blob/master/core/stores/mon/options.go import (
"reflect"
"go.mongodb.org/mongo-driver/bson"
// 引入 go mongo-driver
mopt "go.mongodb.org/mongo-driver/mongo/options"
"github.com/zeromicro/go-zero/core/stores/mon"
)
registry := bson.NewRegistry()
registry.RegisterTypeEncoder(reflect.TypeOf(decimal.Decimal{}), &mongo.MongoDecimal{})
// 使用 SetRegistry 註冊
opts := mon.Option(mopt.SetRegistry(registry)) // opts 的 type 會是 Option,這是 go-zero 自己訂的 type
conn := mon.MustNewModel(url, db, collection, opts) 資料來源: |
The source code of Mongo Options is here: https://github.com/zeromicro/go-zero/blob/master/core/stores/mon/options.go import (
"reflect"
"go.mongodb.org/mongo-driver/bson"
//Introduce go mongo-driver
mopt "go.mongodb.org/mongo-driver/mongo/options"
"github.com/zeromicro/go-zero/core/stores/mon"
)
registry := bson.NewRegistry()
registry.RegisterTypeEncoder(reflect.TypeOf(decimal.Decimal{}), &mongo.MongoDecimal{})
//Register using SetRegistry
opts := mon.Option(mopt.SetRegistry(registry)) // The type of opts will be Option, which is the type set by go-zero itself
conn := mon.MustNewModel(url, db, collection, opts) source: |
mongo-driver v1.12.1,里面没有这个mopt.SetRegistry(registry)这个方法 |
mongo-driver v1.12.1, there is no mopt.SetRegistry(registry) method in it |
@kevwan Hey! I have a problem with transfering mongo decimal128 to decimal.Decimal. Could I make a pull request to add the setRegistry function for the mongo client on https://github.com/zeromicro/go-zero/blob/master/core/stores/mon/options.go?
// WithRegistry the mon client can transfer mongo types to custom types.
func WithRegistry(registry *bsoncodec.Registry) Option {
return func(opts *options) {
opts.SetRegistry(registry)
}
} To use this function, follow the example below: registry := bson.NewRegistry()
registry.RegisterTypeEncoder(reflect.TypeOf(decimal.Decimal{}), MongoDecimal{})
// this code
opts := mon.WithRegistry(registry)
conn := mon.MustNewModel(url, db, collection, opts) |
试过了还是不行,错误为: cannot decode 128-bit decimal into a decimal.Decimal registry := bson.NewRegistry() func WithRegistry(registry *bsoncodec.Registry) mon.Option { |
mongo.mongoDecimal的代码为: import ( // MongoDecimal是一个允许十进制编码的ValueCodec,Decimal128到decimal.Decimal var _ bsoncodec.ValueCodec = &MongoDecimal{} func WithRegistry(registry *bsoncodec.Registry) mon.Option { func (dc *MongoDecimal) EncodeValue(ect bsoncodec.EncodeContext, w bsonrw.ValueWriter, value reflect.Value) error {
} func (dc *MongoDecimal) DecodeValue(ect bsoncodec.DecodeContext, r bsonrw.ValueReader, value reflect.Value) error {
} ` |
@shanweie You have to use BOTH so that you can transfering the |
decimal128
and decimal.Decimal
?
This issue is stale because it has been open for 30 days with no activity. |
registry := bson.NewRegistry()
registry.RegisterTypeEncoder(reflect.TypeOf(decimal.Decimal{}), &mongo.MongoDecimal{})
conn := mon.MustNewModel(url, db, collection, opts)
怎么设置opts,使用registry自动互转
The text was updated successfully, but these errors were encountered: