示例#1
0
// Reset implements the protobuf marshalling interface.
func (q *Quantity) Unmarshal(data []byte) error {
	p := QuantityProto{}
	if err := p.Unmarshal(data); err != nil {
		return err
	}
	q.Format = p.Format
	b := big.NewInt(0)
	b.SetBytes(p.Bigint)
	q.Amount = inf.NewDecBig(b, inf.Scale(p.Scale))
	return nil
}
示例#2
0
文件: marshal.go 项目: RobinUS2/gocql
func unmarshalDecimal(info TypeInfo, data []byte, value interface{}) error {
	switch v := value.(type) {
	case Unmarshaler:
		return v.UnmarshalCQL(info, data)
	case *inf.Dec:
		scale := decInt(data[0:4])
		unscaled := decBigInt2C(data[4:], nil)
		*v = *inf.NewDecBig(unscaled, inf.Scale(scale))
		return nil
	}
	return unmarshalErrorf("can not unmarshal %s into %T", info, value)
}
示例#3
0
文件: marshal.go 项目: qtabot/gocql
func unmarshalDecimal(info *TypeInfo, data []byte, value interface{}) error {
	switch v := value.(type) {
	case Unmarshaler:
		return v.UnmarshalCQL(info, data)
	case **inf.Dec:
		if len(data) > 4 {
			scale := binary.BigEndian.Uint32(data[0:4])
			unscaled := new(big.Int).SetBytes(data[4:])
			*v = inf.NewDecBig(unscaled, inf.Scale(scale))
		}
		return nil
	}
	return unmarshalErrorf("can not unmarshal %s into %T", info, value)
}
示例#4
0
文件: marshal.go 项目: Gibheer/gocql
func unmarshalDecimal(info *TypeInfo, data []byte, value interface{}) error {
	switch v := value.(type) {
	case Unmarshaler:
		return v.UnmarshalCQL(info, data)
	case **inf.Dec:
		if len(data) > 4 {
			scale := decInt(data[0:4])
			unscaled := decBigInt2C(data[4:])
			*v = inf.NewDecBig(unscaled, inf.Scale(scale))
			return nil
		} else if len(data) == 0 {
			*v = nil
			return nil
		} else {
			return unmarshalErrorf("can not unmarshal %s into %T", info, value)
		}
	}
	return unmarshalErrorf("can not unmarshal %s into %T", info, value)
}
示例#5
0
		arg = decArgZZ{a.z, a.y, a.x}
		testDecFunZZ(t, "MulZZ symmetric", MulZZ, arg)
	}
}

var decUnscaledTests = []struct {
	d  *inf.Dec
	u  int64 // ignored when ok == false
	ok bool
}{
	{new(inf.Dec), 0, true},
	{inf.NewDec(-1<<63, 0), -1 << 63, true},
	{inf.NewDec(-(-1<<63 + 1), 0), -(-1<<63 + 1), true},
	{new(inf.Dec).Neg(inf.NewDec(-1<<63, 0)), 0, false},
	{new(inf.Dec).Sub(inf.NewDec(-1<<63, 0), inf.NewDec(1, 0)), 0, false},
	{inf.NewDecBig(new(big.Int).Lsh(big.NewInt(1), 64), 0), 0, false},
}

func TestDecUnscaled(t *testing.T) {
	for i, tt := range decUnscaledTests {
		u, ok := tt.d.Unscaled()
		if ok != tt.ok {
			t.Errorf("#%d Unscaled: got %v, expected %v", i, ok, tt.ok)
		} else if ok && u != tt.u {
			t.Errorf("#%d Unscaled: got %v, expected %v", i, u, tt.u)
		}
	}
}

var decRoundTests = [...]struct {
	in  *inf.Dec