decimal/mongo.go

29 lines
678 B
Go
Raw Normal View History

package decimal
import (
"reflect"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsonrw"
)
type BsonDecoderEncoder struct {
}
func (BsonDecoderEncoder) EncodeValue(encodeContext bsoncodec.EncodeContext, writer bsonrw.ValueWriter, value reflect.Value) error {
return writer.WriteString(value.Interface().(Decimal).String())
}
func (BsonDecoderEncoder) DecodeValue(decodeContext bsoncodec.DecodeContext, reader bsonrw.ValueReader, value reflect.Value) error {
str, err := reader.ReadString()
if err != nil {
return err
}
dec, err := NewFromString(str)
if err != nil {
return err
}
value.Set(reflect.ValueOf(dec))
return nil
}