示例#1
0
// GetDecimal decodes a decimal value from the bytes of the receiver. If the
// tag is not DECIMAL an error will be returned.
func (v Value) GetDecimal() (decimal.Decimal, error) {
	if tag := v.GetTag(); tag != ValueType_DECIMAL {
		return decimal.Decimal{}, fmt.Errorf("value type is not %s: %s", ValueType_DECIMAL, tag)
	}
	data := v.dataBytes()
	i, n := binary.Varint(data)
	if n <= 0 {
		return decimal.Decimal{}, fmt.Errorf("int64 varint decoding failed: %d", n)
	}
	bi := new(big.Int)
	err := bi.GobDecode(data[n:])
	if err != nil {
		return decimal.Decimal{}, err
	}
	return decimal.NewFromBigInt(bi, int32(i)), nil
}