Merge pull request #101590 from smarterclayton/quantity

quantity: Allow a new quantity to be created directly from inf.Dec
This commit is contained in:
Kubernetes Prow Robot 2021-04-30 10:24:14 -07:00 committed by GitHub
commit 72b24d128b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -696,6 +696,15 @@ func (q *Quantity) UnmarshalJSON(value []byte) error {
return nil
}
// NewDecimalQuantity returns a new Quantity representing the given
// value in the given format.
func NewDecimalQuantity(b inf.Dec, format Format) *Quantity {
return &Quantity{
d: infDecAmount{&b},
Format: format,
}
}
// NewQuantity returns a new Quantity representing the given
// value in the given format.
func NewQuantity(value int64, format Format) *Quantity {

View File

@ -392,6 +392,16 @@ func TestQuantityParse(t *testing.T) {
got.AsDec()
}
for _, format := range []Format{DecimalSI, BinarySI, DecimalExponent} {
// ensure we are not simply checking pointer equality by creating a new inf.Dec
var copied inf.Dec
copied.Add(inf.NewDec(0, inf.Scale(0)), got.AsDec())
q := NewDecimalQuantity(copied, format)
if c := q.Cmp(got); c != 0 {
t.Errorf("%v: round trip from decimal back to quantity is not comparable: %d: %#v vs %#v", item.input, c, got, q)
}
}
// verify that we can decompose the input and get the same result by building up from the base.
positive, _, num, denom, suffix, err := parseQuantityString(item.input)
if err != nil {