Esempio n. 1
0
// DecodeDecimalValue decodes a value encoded by EncodeDecimalValue.
func DecodeDecimalValue(b []byte) ([]byte, *inf.Dec, error) {
	if len(b) == 0 {
		return nil, nil, fmt.Errorf("array is empty")
	}
	tag := ValueType(b[0])
	b = b[1:]
	switch tag {
	case ValueType_DECIMAL:
		d, err := encoding.DecodeNonsortingDecimal(b, nil)
		return nil, d, err
	case ValueType_DELIMITED_DECIMAL:
		i, n := binary.Varint(b)
		if n <= 0 {
			return nil, nil, fmt.Errorf("int64 varint decoding failed: %d", n)
		}
		d, err := encoding.DecodeNonsortingDecimal(b[n:n+int(i)], nil)
		return b[n+int(i):], d, err
	}
	return nil, nil, fmt.Errorf("value type is not %s or %s: %s", ValueType_DECIMAL, ValueType_DELIMITED_DECIMAL, tag)
}
Esempio n. 2
0
// DecodeDecimalValue decodes a value encoded by EncodeDecimalValue.
func DecodeDecimalValue(b []byte) ([]byte, *inf.Dec, error) {
	if len(b) == 0 {
		return nil, nil, fmt.Errorf("array is empty")
	}
	tag := ValueType(b[0])
	b = b[1:]
	switch tag {
	case ValueType_DECIMAL:
		d, err := encoding.DecodeNonsortingDecimal(b, nil)
		return nil, d, err
	case ValueType_DELIMITED_DECIMAL:
		var i int64
		var err error
		b, _, i, err = encoding.DecodeNonsortingVarint(b)
		if err != nil {
			return nil, nil, err
		}
		d, err := encoding.DecodeNonsortingDecimal(b[:int(i)], nil)
		return b[int(i):], d, err
	}
	return nil, nil, fmt.Errorf("value type is not %s or %s: %s", ValueType_DECIMAL, ValueType_DELIMITED_DECIMAL, tag)
}
Esempio n. 3
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() (*inf.Dec, error) {
	if tag := v.GetTag(); tag != ValueType_DECIMAL {
		return nil, fmt.Errorf("value type is not %s: %s", ValueType_DECIMAL, tag)
	}
	return encoding.DecodeNonsortingDecimal(v.dataBytes(), nil)
}