Encode and Decode decimal.Big structure as slice of bytes in mongo

This commit is contained in:
TeoV
2021-01-07 17:03:38 +02:00
committed by Dan Christian Bogos
parent abb6507006
commit 029f84e5f2

View File

@@ -134,20 +134,24 @@ func DecimalEncoder(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val refle
if !ok {
return fmt.Errorf("cannot cast <%+v> to <utild.Decimal>", val.Interface())
}
return vw.WriteString(d.String())
sls, err := d.MarshalText()
if err != nil {
return err
}
return vw.WriteBinary(sls)
}
func DecimalDecoder(ec bsoncodec.DecodeContext, vw bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != decimalType {
return bsoncodec.ValueEncoderError{Name: "DecimalDecoder", Kinds: []reflect.Kind{reflect.Struct}, Received: val}
}
str, err := vw.ReadString()
data, _, err := vw.ReadBinary()
if err != nil {
return err
}
dBig, ok := new(decimal.Big).SetString(str)
if !ok {
return fmt.Errorf("cannot set string: <%s> to decimal.Big", str)
dBig := new(decimal.Big)
if err := dBig.UnmarshalText(data); err != nil {
return err
}
val.Set(reflect.ValueOf(utils.Decimal{dBig}))
return nil